0

I've built a lightning component that runs when the user clicks the standard 'New' button on the Opportunities related list on Account. My component invokes e.force.createRecord meaning it shows the Opportunity record creation modal when ran.

The Opportunity record creation modal has 3 buttons (Cancel, Save and New, and Save). Is there a way to define separate redirects for Cancel and Save? Currently I can define one redirect that gets applied to both the Cancel and Save button, via the following code:

var createRecordEvent = $A.get("e.force:createRecord");
        createRecordEvent.setParams({
            "entityApiName": "Opportunity",
            // Prepopulate values:
            "defaultFieldValues": {
                 "AccountId" : recordId //,
                 // Add more default values here if desired:
            },
            "panelOnDestroyCallback": function(event) {
                window.location.href = "https://www.google.com";
            },
            "recordTypeId":rtDet.id
        });
        createRecordEvent.fire();

Here's an image to better illustrate my dilemma: enter image description here

2

I would suggest using the lightning:recordEditForm

<aura:component>
<lightning:recordEditForm aura:id="recordEditForm" recordTypeId="0123XXXXX"
                       objectApiName="Opportunity">
    <lightning:messages />
    <div class="slds-grid">
        <div class="slds-col slds-size_1-of-2">
            <!-- Your lightning:inputField components here -->
        </div>
        <div class="slds-col slds-size_1-of-2">
            <!-- More lightning:inputField components here -->
        </div>
    </div>
    <lightning:button class="slds-m-top_small" type="submit" label="Create new" />
    <lightning:button class="slds-m-top_small" type="Cancel" label="Cancel" onclick="{!c.customAction}" />
</lightning:recordEditForm>

and then leverage the events provided by the component such as :

onsuccess --> The action triggered when the form is saved.

To do your custom action and for your cancel button, you can define your own method.

0

The following solution was given by Dmytro Kryvyi: which actually in the post suggested by @kurunve.

*"There is an undocumented solution for this. Below is the js controller function that creates a new Contact record and redirects back from the record page to the initial page after a new record is created.

createNewContact: function(component, event, helper) {
        var windowHash = window.location.hash;
        var createEvent = $A.get("e.force:createRecord");
        createEvent.setParams({
            "entityApiName": "Contact",
            "panelOnDestroyCallback": function(event) {
                window.location.hash = windowHash;
            }
        });
        createEvent.fire();
    }

Where "panelOnDestroyCallback" is the hidden force:createRecord attribute. I know that's an ugly hack, but hey, if I had known this before I wouldn't have spent 50+ hours implementing a fully-custom multi-object 'createRecord' component with all the schema tricks, custom lookups, multipicklists etc."*

  • windowHash is undefined when i tried this. – user3430245 Jul 20 '18 at 17:26
  • Do you have anything after '#' in the url, if you dont have anything after hash you will get undefined. you could also do window.location.hash = "someIdinsfdc"; then it will take you to that record. – naruto-sfdc Jul 20 '18 at 17:35
  • Here is the URL: ronny555-dev-ed.lightning.force.com/lightning/o/Opportunity/… As you can see, no #. Even if there was, I don't see a way of setting one URL for Cancel, and a totally different one for Save. – user3430245 Jul 20 '18 at 17:39
0

Sorry to be the bringer of bad news, but this is not possible at the time of posting this answer. I am afraid you must resort to using force:recordData as described here and customize the entire modal to fit your requirement.

EDIT: You can set defaults upon init like so:

COMPONENT

<aura:attribute name="record" type="Object"/>
<aura:attribute name="newRecord" type="Object"/>
<aura:attribute name="recordError" type="String"/>

<aura:attribute name="recordId" type="String"/>
<aura:attribute name="sObjectName" type="String"/>

<force:recordData aura:id="myOpportunity"
                              layoutType="FULL"
                              targetRecord="{!v.newRecord}"
                              targetFields="{!v.record}"
                              targetError="{!v.recordError}"
                              />

<aura:handler name="init" value="{!this}" action="{!c.initializeRecord}"/>

CONTROLLER JS

initializeRecord: function(component, event, helper) {
        try{

            var sObjectName = component.get("v.sObjectName");

            var recordDataAuraId = 'myOpportunity';

                component.find(recordDataAuraId).getNewRecord(
                    sObjectName,
                    null,
                    false,
                    $A.getCallback(function() {
                        var newRecord = component.get("v.newRecord");
                        var error = component.get("v.recordError");
                        if(error || (newRecord === null)) {
                            console.log("-------> Error initializing record template: " + error);
                            return;
                        }
                        else{
                            var parentRecordId = component.get("v.recordId"); //set parent record Id here
                            newRecord.fields.Parent_Lookup_Field_API_Name_Here__c.value = parentRecordId;
                            // add more defaults as necessary
                        }
                    })
                );
        }catch(err) {
            console.log('-------> ERROR: ' + err + ' ** MESSAGE: ' + err.message + ' ** STACK: ' + err.stack);
        }
    },
  • I'd use force:recordData, but it doesn't appear there's a way to default lookup fields on the record creation modal. – user3430245 Jul 20 '18 at 17:25
  • You can default lookup fields. Please see updated answer. – Lightning Evangelist Jul 20 '18 at 17:47
  • Can force:recordData be combined with force:recordForm? – user3430245 Jul 20 '18 at 20:38
0

Add the param "navigationLocation": "RELATED_LIST" to prevent the event from navigating away after saving.

$A.get("e.force:createRecord").setParams({
       "entityApiName" : "Contact",
       "navigationLocation": "RELATED_LIST"
}).fire();

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.