0

How would you create a relatedList record and stay on the same page instead of redirecting to the newly created record?

the below code works fine but the problem is that it's redirecting.

here is my code:

createRecord : function (component, event, helper) {
    var createRecordEvent = $A.get("e.force:createRecord");
    createRecordEvent.setParams({
        "entityApiName": "Contact"
    });
    createRecordEvent.fire();
}


<div class="c-container">
            <aura:set attribute="actions">
                <lightning:button label="New" onclick="{!c.createRecord}"/>
            </aura:set>     
            <aura:set attribute="title">
                <b><a class="slds-card__header-link" onclick="{!c.handleGotoRelatedList}">{!(v.sobjectLabelPlural) + ' (' + (v.numberOfRecordsForTitle) + ')'}</a></b>
            </aura:set>             
            <aura:set attribute="footer">
                <aura:if isTrue="{!not(empty(v.records))}">  
                    <a onclick="{!c.handleGotoRelatedList}">View All</a>
                </aura:if>
            </aura:set>                
            <aura:if isTrue="{!not(empty(v.records))}">    
                <p class="slds-p-horizontal_small">                  
                    <lightning:datatable class="related-list"
                                         columns="{! v.columnsWithActions }"
                                         data="{! v.records }"
                                         keyField="id"
                                         onrowaction="{! c.handleRowAction }"
                                         hideCheckboxColumn="true"
                                         resizeColumnDisabled="true"/>                     
                </p>               
            </aura:if> 
    </div>
1

By using "panelOnDestroyCallback" param we can control what happens when recordCreate success. We used it to refresh related list and navigation also stopped.

var createRecord = $A.get("e.force:createRecord");
createRecord.setParam("entityApiName", 'Contact');
createRecord.setParam("navigationLocation", "LOOKUP");
createRecord.setParam("panelOnDestroyCallback", function callbackWrapper() {
   // refresh related list 
});
createRecord.fire();

P.S. - i am very new on stack so i am noob to explaning things.

2
  • I try to refresh the page by doing $A.get("e.force:refreshView").fire(); but it does not refreshing the page, any idea?
    – Nick Kahn
    Oct 25 '21 at 22:12
  • 1
    Custom component doesn't catch standard event until you catch them specially. <aura:handler event="force:refreshView" action="{!c.doInit}"/> . But in your case you just need to refresh datatable list variable v.records. For that You can call same method which fetch records from apex server in panelOnDestroyCallback callback function. Thanks. Oct 26 '21 at 3:49
0

There is no way to modify the behaviour of the force:createRecord event to prevent the redirection after creating the record.

You can achieve something like that by leveraging the lightning:recordForm component (https://developer.salesforce.com/docs/component-library/bundle/lightning:recordForm/specification). This component can be configured to show the exact form that you are using in the modal when using the force:createRecord event. You can use it to create the record and customize the after-creation logic.

I have included an example of this in your component (I have also modified a little bit the component just to make it compile as I don't have the rest of your logic). You can see how I use the "New" button to show the form. Also as an example, on the success event I'm calling a getRecords method that could just retrieve the records again so the related table is updated with the new value:

({
  getRecords: function (component, event, helper) {
    //Include here the logic for obtaining the records for the related list
    //Maybe also set the v.showContactForm attribute to false to hide the form after creating the Record
  },

  showContactForm: function (component, event, helper) {
    component.set("v.showContactForm", true);
  }
});


  <lightning:card>
    <aura:set attribute="actions">
      <lightning:button label="New" onclick="{!c.showContactForm}" />
    </aura:set>
    <aura:set attribute="title">
      <b
        ><a class="slds-card__header-link" onclick="{!c.handleGotoRelatedList}"
          >{!(v.sobjectLabelPlural) + ' (' + (v.numberOfRecordsForTitle) +
          ')'}</a
        ></b
      >
    </aura:set>

    <aura:if isTrue="{!v.showContactForm}">
      <lightning:recordForm
        objectApiName="Contact"
        layoutType="Full"
        onsuccess="{!c.getRecords}"
      ></lightning:recordForm>
    </aura:if>

    <aura:set attribute="footer">
      <aura:if isTrue="{!not(empty(v.records))}">
        <p class="slds-p-horizontal_small">Something Here</p>
      </aura:if>
    </aura:set>
  </lightning:card>

To improve the usability, you could include the lightning:recordForm inside a modal so it would resemble even more to what you achieve with the force:createRecord event.

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.