1

I have created a component which is creating opportunity record and I want after successfull creation it redirects to the newly created opportunity detail page.

Opportunity creation is working fine but after creation it is not redirecting to the detail page. It throws the error

This page has an error. You might just need to refresh it. Error in $A.getCallback() [Cannot read property 'setParams' of undefined] Callback failed: apex://createOpp/ACTION$CreateOpportunity Failing descriptor: {c:createNewOppCmp}

Controller.JS:

({
save : function(component, event, helper) {
    var OpptyObj = component.get("v.Oppty");

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

    action.setParams({
        Oppty : OpptyObj 
    });

    action.setCallback(this,function(res){

        var oppId = res.getReturnValue();
        alert(oppId);
          var urlEvent = $A.get("e.force:navigateToURL");
            urlEvent.setParams({
           //   "url": "https://ashuforce-dev-ed.my.salesforce.com/"+oppId+" "
                "url" : "/006/o"+oppId
            });
            urlEvent.fire();
    });

  $A.enqueueAction(action);

} })

Not sure why e.force:navigateToURL is not working properly, what I am missing here?

  • apex controller returning the id of newly created record, I can see in the alert. – Ashutosh Jun 12 '18 at 11:33
  • Nopes, that's the complete one which I am getting everytime. – Ashutosh Jun 12 '18 at 11:36
  • if you completely remove the navigation event related code, do you still get the error? – glls Jun 12 '18 at 11:38
  • No, if I remove then it works fine without any error. – Ashutosh Jun 12 '18 at 11:39
  • alert(oppId); This part is working? So do you recieve oppId and alert shows it? – user1974566 Jun 12 '18 at 12:14
3

Use new lightning:navigation component for that. Just add it to your component:

<lightning:navigation aura:id="navigation"/>

and in method navigate to detail page:

    component.find("navigation")
    .navigate({
        "type" : "standard__recordPage",
        "attributes": {
            "recordId"      : recordId,
            "actionName"    : actionName ? actionName : "view"   //clone, edit, view
        }
    }, true);

ps. you need to change API version to 43 in your component.

| improve this answer | |
0

First of all: are you sure that you're recieving response with id of newly created opportunity?

I would recommend to check the state of the response before trying to navigate to created record, and then use e.force:navigateToSObject

var state = res.getState();
if(state === 'SUCCESS'){
    var oppId = res.getReturnValue();
    var navEvt = $A.get("e.force:navigateToSObject");
    navEvt.setParams({
        "recordId": oppId
    });
    navEvt.fire();
}
| improve this answer | |
  • Yes, opportunity is creating successfully also I am getting the Id of the newly created opportunity record in the JS controller but getting the same error in navigating. – Ashutosh Jun 13 '18 at 4:07
  • Could you please provide your apex backend code? From the description of the error I think something is wrong there and not in the navigation itself. – user1974566 Jun 13 '18 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.