1

I have a Quick Action using Lightning Component on Opportunity. Based on some scenario when a service return Case ID then through Page Reference I would like to return to Case Record Page.

Code :

enter image description here

ResCode has Case Id, but Page reference method is not directing, although URl is getting created in Logs as below: enter image description here

Please help

1
  • I am afraid PageReference do only works if those are returned as result of Visualforce Action method execution, and do not work in other apex (i.e. AuraEnabled methods, batch, queueable, triggers and so on) – kurunve Aug 22 '19 at 8:41
3

I think it would be better understood by going to basics - by understanding difference between lightning and visualforce.

  • Visualforce to apex is synchronous transaction, thus when pageReference object from apex is returned to a visualforce action, it will perform navigation.
  • Lightning to apex is asynchronous transaction, thus all the data transfer takes place through serialization and deserialization.

Reason for your code not navigating:

Apex PageReference object is not serializable and so it cannot be returned in @AuraEnabled methods. If you want to test it, you can try System.debug(JSON.serialize(Page.MyPageName)); in anonymous apex - this will throw the same error.

Solution:

Lightning javascript has its own pageReference object api which can be used along with lightning:navigation for navigation purposes.

You can use below JS for your navigation:

component.find("navService").navigate({    
        "type": "standard__recordPage",
        "attributes": {
            "recordId": "001xx000003DGg0AAG",
            "objectApiName": "PersonAccount",
            "actionName": "view"
        }
    })

and in aura cmp file:

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

As an alternative, you can return url string from apex:

@AuraEnabled
public static String redirect(String recordId) {
    // create pagereference 
    return pageRef.getUrl();
}

Then in controller.js:

component.find("navService").navigate({    
        "type": "standard__webPage",
        "attributes": {
            "url": "/../..." // url returned from apex
        }
    })

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.