4

I have a Lightning Datatable containing possible Opportunities. The user can select which records they wish to create Opportunities and click a button (handleContinue below) which creates new Opportunity. I loop through the selected rows and call createRecord one at a time.

This code works just fine, but I'm wondering if it possible to wrap those createRecord calls in a "parent" promise? We want to show a Lightning Spinner while those Opportunities are being created, then show a result of the entire process (successes, failures)

I'm still pretty new at this, so any help is greatly appreciated. Thanks.

handleContinue() {

  var selectedBeneficiaries = this.template
      .querySelector("lightning-datatable")
      .getSelectedRows();

  selectedBeneficiaries.forEach(function(bene) {

    const fields = {};
    fields[OPP_CLOSE_DATE_FIELD.fieldApiName] = '2020-03-31';
    fields[OPP_NAME_FIELD.fieldApiName] = bene.AccountName + ' Opportunity';
    fields[OPP_STAGE_FIELD.fieldApiName] = "New";

    const recordInput = { apiName: OPP_OBJECT.objectApiName, fields };
    createRecord(recordInput)
      .then(success => {
        bene.success = true;
      })
      .catch(error => {
        bene.success = false;
      });
});
4
  • Why not use apex and do a bulk insert? It would be faster than individual inserts Commented Mar 2, 2020 at 19:42
  • @PranayJaiswal Thanks for reply - I did start down that route, but read using recordAPI is the recommended route to take?
    – Mike G.
    Commented Mar 2, 2020 at 19:53
  • 1
    For single records yeh... I won't recommend for bulk as apex gives you transaction control, roll back, and more Commented Mar 2, 2020 at 19:54
  • Awesome, thanks that makes a lot of sense. I appreciate your time.
    – Mike G.
    Commented Mar 2, 2020 at 19:58

1 Answer 1

6

You could try do to this in bulk:

Promise.all(
selectedBeneficiaries.map(bene => {
    const fields = {};
    fields[OPP_CLOSE_DATE_FIELD.fieldApiName] = '2020-03-31';
    fields[OPP_NAME_FIELD.fieldApiName] = bene.AccountName + ' Opportunity';
    fields[OPP_STAGE_FIELD.fieldApiName] = "New";
    const recordInput = { apiName: OPP_OBJECT.objectApiName, fields };
    return createRecord(recordInput);
})).then(results => {
    // ... //
}).catch(error => {
    // ... //
});

This would be okay for a relatively small number of records, but Apex would have better performance for large lists.

4
  • Thanks for providing the JS promise way of doing it. This should work for my purposes as I'm only creating 5 opportunities max. I did code the Apex method as well, but it seems like this gives you more control over the events (success, failures, etc.)
    – Mike G.
    Commented Mar 3, 2020 at 14:10
  • 1
    @MikeG. Yes, I feel like it's always good to have different options available. Apex would be overkill for small updates, but necessary for large updates. Having multiple tools available makes a developer more flexible.
    – sfdcfox
    Commented Mar 3, 2020 at 14:13
  • @sfdcfox What if I want to to all or nothing? Would there be a mechanism to achieve that using createRecord() without making subsequent calls to the database to erase any successful inserts?
    – Arthlete
    Commented Mar 17, 2020 at 19:28
  • 1
    @Arthlete Nope, unfortunately not. That would be a limitation of this design. If you need that level of transaction control, use Apex.
    – sfdcfox
    Commented Mar 17, 2020 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .