2

I've got these two helper methods:

submitBookingInformation: function(component, event, helper) {
    helper.createLead(component, event, helper);
    helper.saveRecord(component, event, helper);
}

helper.createLead simply creates a lead with Lightning Data Service (no server-side apex to create the lead) helper.createLead needs to finish first, otherwise helper.saveRecord will give an error since it's expecting a record ID (from that lead) just created. Can I somehow use Promises here to kick off "helper.saveRecord" once helper.createLead successfully finished?

These two helper methods are on one button.

Thanks a lot!

<force:recordData aura:id="accountRecordCreator"
                  layoutType="FULL"
                  targetRecord="{!v.newAccount}"
                  targetFields="{!v.simpleNewAccount}"
                  targetError="{!v.newAccountError}" />

creating the lead:

({
    saveBaseAccount: function(component, event, helper) {
        component.find("accountRecordCreator").saveRecord(function(saveResult) {
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                // record is saved successfully
                var customerAccountId = saveResult.recordId; // Gets the record ID of the account created
                var appEvent = $A.get("e.c:CustomerSubmitForm");
                        appEvent.setParams({
                            customerAccountId: customerAccountId // customerAccountId in the CustomerSubmitForm event is now set to the ID that was just created
                        });
                appEvent.fire(); // Fires the event to store customerAccountId in the Event "CustomerSubmitForm.evt"
                console.log(customerAccountId);
                var resultsToast = $A.get("e.force:showToast");

                resultsToast.setParams({
                    "title": "Saved",
                    "message": "The record was saved."
                });
                resultsToast.fire();
            }
            else if (saveResult.state === "INCOMPLETE") {
                // handle the incomplete state
                console.log("User is offline, device doesn't support drafts.");
            }
            else if (saveResult.state === "ERROR") {
                // handle the error state
                console.log('Problem saving contact, error: ' + JSON.stringify(saveResult.error));
                var resultsToast = $A.get("e.force:showToast");
                resultToast.setParams({
                    "title": "Error",
                    "message": "Please complete the form"
                })
                resultsToast.fire();
            }
            else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        });
    }
})

Loading the appointment record

({
    saveRecord: function(component, event, helper) {
        var event = $A.get("event.force:createRecord");

                if (event) {
                    event.setParams({
                        entityApiName: "Sales_Appointment__c",
                        defaultFieldValues: {
                            Resource__c: component.get("v.resourceId"),
                            Dealership__c: component.get("v.accountId"),
                            Lead__c: component.get("v.customerAccountId"),

                        }
                    })
                    event.fire();
                }
                else {
                    alert("Not available")
                }
    }

})
4

That would look like this:

 new Promise((resolve, reject) =>
   helper.createLead(component, event, helper, resolve, reject))
 .then(value =>
   helper.saveRecord(component, event, helper, value)
 );

You'll need to modify createLead to accept the resolve and reject callbacks, and when the initial process is done in createLead, you call resolve(record.Id) or some such, which will be the value passed to the then value.

Also see this answer for an additional demonstration.

13
  • Not really sure how this is done. I'm creating the lead using Lightning Data Service.. should I wrap that in a callback? It's using component.find("accountRecordCreator").saveRecord(function(saveResult) ....{ – erikvm Apr 19 '18 at 6:50
  • @erikvm Does this help? You don't normally need Promises for your use case, as far as I can tell. – sfdcfox Apr 19 '18 at 6:56
  • 1
    @erikvm Yeah, you don't need a Promise for this; you can display the sales appointment record in createLead's callback function. – sfdcfox Apr 19 '18 at 7:48
  • 1
    @erikvm Yes, if you need to, we can chat. Off the top of my head, it looks like you're not seeing the toast because you forgot $A.getCallback, as mentioned in the documentation. Why don't you try that, first? – sfdcfox Apr 20 '18 at 14:11
  • 1
    @erikvm based on what you wrote, looks like you can just change this to helper, since you're already passing the helper as a parameter. – sfdcfox Apr 22 '18 at 11:42

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.