8

I have implemented a Custom Lightning Lookup component which includes creation of a new record capability using force:createRecord. For this new record creation, I have a default option(New) at the bottom of the lookup results. So clicking on it gets me the creation layout. But once the record gets created, I am unable to append that newly created record in the lookup as the flow is redirecting me to new record detail page.

How can I implement a possible callback which gets me the newly created record and append back to the Lookup field?

8
+25

Documentation of force:createRecord states:

This event is handled by the one.app container. ... This event presents a standard page to create a record. That is, it doesn’t respect overrides on the object’s create action.

Even though it doesn't say anything about a callback, I assume that Salesforce does not provide a way to override any standard behavior. In fact, there is a suggestion on Success community to introduce a callback for force:createRecord.

So, if you would like to get the behavior you desire, force:createRecord is not an option.

A custom component is probably the way to go. Create a new component that contains a form that has all the required fields to create a record. Create a custom Apex class with AuraEnabled method that returns the new record id to the callback. For a detailed example see this trailhead.

|improve this answer|||||
  • 1
    I am already heading in the custom component path. – Arnold Jr. Jan 30 '18 at 1:12
1

I have a workaround for this based on Dima's answer here. There's an undocumented parameter called panelOnDestroyCallback that gets called after the new form action buttons are clicked (I assume the actual use of the callback is for when the new form is destroyed). From the callback we can get the Id of the newly created record using the URL and use window.history.back() to bring you back to the original page.

var windowHref = window.location.href;
var sObjectName = "Account";
var recTypeID = "xxxxxxxxxxxxxx";
var createRecordEvent = $A.get("e.force:createRecord");
createRecordEvent.setParams({
        "entityApiName": sObjectName,
        "recordTypeId": recTypeID,
        "panelOnDestroyCallback": function(event) {
            /** Check the url to know if "cancel" or "save" was pressed (if 
            the url changed then save was pressed because we would have been 
            redirected to the newly created record).**/

            if(windowHref !== window.location.href){
                let splitPath = window.location.pathname.split(sObjectName +"/");
                let newId = "";
                if(splitPath.length > 1){
                    newId = splitPath[1].split("/")[0];
                }   

                window.history.back();

                /** component.set() and get() won't work for some cases (e.g. if your 
                component is a quick action) and you'll have to get creative like 
                pass the id through an application event and create another component 
                to handle the it. **/
                component.set("v.newAccountId", newId);
            }
        }
});
createRecordEvent.fire();

It's a dirty alternative to creating a custom new form component but, as far I as I know, the only way to create new records that follows the assigned page layout until they give us a proper callback for force:createRecord.

|improve this answer|||||

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.