Navigate to created record

Hello! Today I am gonna show you how to navigate to created record. Of course, it is not hard when you use some custom functionality, but what if you choose a standard quick action to create a record? Then it is not so easy, so let’s check out how you can achieve that.

Before we start a development we need to do some config work, so let’s create a quick action on the Account object and configure it like on the screenshot below.
Important! You need to leave the field “Success Message” blank here, in another way this trick won’t work.

On action layout, leave fields as default and click “Save”. Now we need to put our quick action on a page, so to do it let’s click on “Page Layouts”, choose default layout for “Account” records and drop newly created quick action on the section “Salesforce Mobile and Lightning Experience Actions” and once again click “Save”.

Now go to one of your “Account” records and create an “Opportunity” record with your quick action (If you want to learn more about building fully customized and validated quick action-click here). If everything works fine you should get a success toast message, notice that message contains clickable link of your opportunity and that’s exactly where we can get information about “Opportunity” from, to navigate to created record.

So, to do it, we need to create a component without a body. All that we need here is to implement some interfaces which let us put our component on Lightning Page and handler which listen on “force:showToast” event and then fire method from the controller.

redirectComponent.cmp

<aura:component implements="flexipage:availableForAllPageTypes">
	<aura:handler event="force:showToast" action="{!c.redirectToNewRecord}"/>
</aura:component>

The next thing which we have to implement is the javascript controller method which one really lets us navigate to created record, so let’s do it!

redirectComponentController.js

({
    redirectToNewRecord: function(component, event, helper) {
        var messageData = event.getParam('messageTemplateData');
        if (!$A.util.isEmpty(messageData)) {
            var executionComponent = messageData[1].executionComponent;
            if (!$A.util.isEmpty(executionComponent)) {
                var recordId = executionComponent.attributes.recordId;
                var navEvt = $A.get("e.force:navigateToSObject");
                navEvt.setParams({
                    "recordId": recordId,
                    "slideDevName": "related"
                });
                navEvt.fire();
            }
        }
    }
})

So what’s going on here is:

3-6 lines: We extract data from force:showToast event and check, if they are not empty.
8-13 lines: We are getting here aura event “force:navigateToSObject” , setting up parameters for it and fire it.

All we need now is to put our custom component on a page, so go to our “Account” record as before, click a gear icon and select “Edit Page”. Here we need to grab our component from the custom on the left side and put it on any place on the page. Activate and Save.

Okay, that’s test time. so go to your “Account” record as before and create Opportunity with quick action and….. boom we you are on the “Opportunity” page. Awesome!

As we are done here, you need to remember that’s an only tricky workaround and it’s always better to use functionality supported by Salesforce, anyway I think it’s something that’s good to know.

Resource

5 2 votes
Article Rating
Subscribe
Notify of
guest
4 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Michelle
Michelle
1 year ago

I implemented this into my sandbox, but it is not working. I followed the directions to a T! Any suggestions

Ev H
Ev H
1 year ago

Thanks for coming up with this, it’s very helpful! I’ve run into 2 issues though, if anyone can help it’d be appreciated: 1.if(!$A.util.isEmpty(messageData)) doesn’t protect against an array of only 1 element, which throws an exception here messageData[1].executionComponent whenever this occurs. I added a length check after isEmpty() in the if statement to prevent this 2.The event is firing on pages that this LWC hasn’t been added to – this is where I encounter the first issue. I’m not sure if there is a bug in /components/force/toastManager.js, if this may be some sort of caching issue, or if navEvt.fire(); isn’t… Read more »

Last edited 1 year ago by Ev H
Close Menu
4
0
Would love your thoughts, please comment.x
()
x