Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (13.1k points)

I want to create a new lead in Salesforce for that I have created a new custom lightning component and a controller. But the lead is not created. I hope that, this is because of controller so herewith I have attached the controller.

({

    createLead : function(component, event, helper) {

        var action = component.get("c.saveLead");

        action.setParams({

            "leaRec":component.get("v.lea")

        });

        action.setCallback(this, function(response){

            if(response.getState()==='SUCCESS'){

                var leaId = response.getReturnValue();

                var toastEvent = $A.get("e.force:showToast");

                toastEvent.setParams({

                    "title": "Success!",

                    "type":"Success",

                    "message": "Lead created successfully."

                });

                 toastEvent.fire();

              var navEvt = $A.get("e.force:navigateToSObject");

                navEvt.setParams({

                    "recordId": leaId,

                    "slideDevName": "related"

                });

                navEvt.fire();

            }

        });

        $A.enqueueAction(action);

    },

})

Can anyone help me with this?

1 Answer

0 votes
by (26.7k points)

So the main reason behind this is your controller method saveLead is fail to run. Also, you are returning null to your component, you can't do that.

You can try this below code.

action.setCallback(this, function(response){

    var toastEvent = $A.get("e.force:showToast");

    if(response.getState()==='SUCCESS'){

        var leaId = response.getReturnValue();

        toastEvent.setParams({

            "title": "Success!",

            "type":"Success",

            "message": "Lead created successfully."

        });

        toastEvent.fire();

        var navEvt = $A.get("e.force:navigateToSObject");

        navEvt.setParams({

            "recordId": leaId,

            "slideDevName": "related"

        });

        navEvt.fire();

    } else {

        let errors = response.getError();

        let message = 'Unknown error'; // Default error message

        // Retrieve the error message sent by the server

        if (errors && Array.isArray(errors) && errors.length > 0) {

            message = errors[0].message;

        }

        toastEvent.setParams({

            "title": "Error!",

            "type":"Error",

            "message": message

        });

        toastEvent.fire();

    }

});

I hope this will help.

Want to become a Salesforce expert? join Salesforce Training now !!

Browse Categories

...