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")
}
}
})