1

Background: There's a form where you select an id from a list, fill in some text input fields then hit save, this submits 1 record. The enhancement to this form is a multiselect LWC component which allows the selection of multiple Ids, for each Id selected they want a record submitted with the same text input fields filling out those records....

Here's the handleSubmit event

    handleSubmit(event){
    event.preventDefault();
    this.getProduct();
    this.isSubmitting = !this.isSubmitting;
    for(let i=0; i<this._assetList.length; i++){
        let fields = event.detail.fields; 
        fields.Theme__c = this._assetList[i];
        fields.Toolkit__c = this.recordId;
        fields.Quantity__c = -1;
        this.template.querySelector('lightning-record-edit-form').submit(fields);
    }     
}

See how we're creating multiple records for all the values within _assetList.

Now the obvious problem where using a forloop to insert records again and again instead of a bulk insert. I was thinking of of modifying what i send as an argument to the submit() but i'm guessing its expecting a single sobject not a list of sobjects so before spending hours coding that out i was wondering if anyone here knew if it was possible.

If that isn't possible what suggestions does anyone have.

EDIT: update from conversion with the man himself below from what i know you can create one record using apex doing this

   createRecord(event){
recList = [];
for(let i=0; i<this._assetList.length; i++){
        let rec= { 'sobjectType' : 'Toolkit_LineItem__c'};
        let fields = event.detail.fields; 
            rec.Price__c = fields.Price__c.value;
            rec.Quantity__c = fields.Quantity__c.value;
            rec.Theme__c  = this._assetList[i]
            rec.Toolkit__c = this.recordId;
     recList.push(rec); 
}
            createRecLineItem({ newRecLine : recList});           
    }

passing to an apex method that takes an argument of sobject.....but how would i get an array of SOBJECT? edit: maybe the above will work, i'll find out.

1 Answer 1

1

Now the obvious problem where using a forloop to insert records again and again instead of a bulk insert.

That's only really a problem in Apex. Governor limits are per transaction, and Lightning doesn't even consume API calls.

I was thinking of of modifying what i send as an argument to the submit() but

Correct, one record per submit.

[...] if it was possible.

Yes, it is possible to use one form, or even several forms, to submit multiple records at once, but note that Lightning automatically rate-limits the API usage, and multiple calls are processed in serial on the server once you exceed the limit of 6 open connections. This will be painfully slow for your users. Don't do it. Just use Apex to save your data.

2
  • So would you say the solution is create an sobject in js and pass it to apex like let oli = { 'sobjectType' : 'ObjectName' }; then a bunch of oli.fieldname = event.detail.fields.fieldname.value; --> pass to apex...now i know that will create 1 record how would you create multiple. i'll update my main post.
    – ensir
    Commented Nov 17, 2022 at 18:40
  • @ensir Creating multiple just means creating a list of records, then pass those in as: @AuraEnabled public static void createRecords(Object__c[] records) { ... }
    – sfdcfox
    Commented Nov 17, 2022 at 19:34

You must log in to answer this question.

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