0

I'm currently stuck at a problem and was hoping someone here could help me. I also certainly hope this is the right place to ask it.

I'm trying to create a custom Invoice record with its corresponding Invoice Line records upon firing an event. I already have some logic in place to gather ID of selected rows in the JS.

I've gone so far as to be able to create the Invoice record (using LDS) and the Invoice Line records (using Apex), but can't seem to pass the Invoice ID for the Invoice Line records. I know I'm able to create the records because it works when I tested this with a hardcoded Invoice ID.

Would it be possible to pass multiple parameters of List and String to an Apex method in LWC?

I would appreciate any help. Thanks in advance!

JS

    selectedRowsEvent(event) {
    
    // Some codes here...
    
    this.selectedRecords = Array.from(conIds);
    }
    
    
    handleSave() {
    invId;
    const fields = {};
    fields[INVOICE_ACCOUNTMD_FIELD.fieldApiName] = this.wiredRecord.data.fields.AccountId__c.value;
    fields[INVOICE_OPPLKP_FIELD.fieldApiName] = this.wiredRecord.data.fields.Id.value;
    const recordInput = { apiName: INVOICE_OBJECT.objectApiName, fields };
   
    // CREATE THE INVOICE RECORD
    createRecord(recordInput)
        .then(invoice => {
            this.message = invoice;
            this.error = undefined;  
            if(this.message !== undefined) {
                this.invId = invoice.Id;   
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Success',
                        message: 'Invoice created',
                        variant: 'success',
                    }),
                );
            }
            console.log(JSON.stringify(invoice));
            console.log("invoice", this.message);

            // CREATE THE INVOICE LINE RECORDS
            createInvLines({ lstConIds : this.selectedRecords, invoiceId : this.invId})
            .then(result => {
                this.message = result;
                this.error = undefined;
                if(this.message !== undefined) {
                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: 'Success',
                            message: 'Invoice Line records created',
                            variant: 'success',
                        }),
                    );
                }
                console.log(JSON.stringify(result));
                console.log("invoice line", this.message);
            })
        })
        .catch(error => {
            this.message = undefined;
            this.error = error;
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'We encountered an error creating the record(s).',
                    message: error.body.message,
                    variant: 'error',
                }),
            );
            console.log("error", JSON.stringify(this.error));
        }); 
    }

Controller

@AuraEnabled
    public static void createInvLines(list<Id> lstConIds, string invoiceId){
        if(lstConIds.size() > 0){
            List<OpportunityLine__c> oppLst = new List<OpportunityLine__c>([SELECT Id, Description__c FROM OpportunityLine__c WHERE Id = :lstConIds]);
            try {
                List<InvoiceLine__c> lstInvLinesToInsert = new List<InvoiceLine__c>();
                for(OpportunityLine__c idCon : oppLst) {
                    lstInvLinesToInsert.add(new InvoiceLine__c(**InvoiceId__c = invoiceId**, Description__c = idCon.Description__c));
                }
                if(!lstInvLinesToInsert.isEmpty()) {
                    insert lstInvLinesToInsert;
                }
            }
            catch(Exception ex) {
                throw new AuraHandledException(ex.getMessage());
            }
        }
    }  

UPDATED: Included a screenshot of the Browser Console debugging below. I've also posted the complete handleSave() functionality from the JS. Please take a look.

Network Console Logs

5
  • This should work, please post the error(s) you are getting if any. The full JS Controller would be much appreciated. Apr 29, 2020 at 9:41
  • The code seems to be correct. Please make sure that there is a value for this.invId. You want to go in debugging what is sent to the server, open the browser console Network tab before this call is made, and check the request parameters.
    – manjit5190
    Apr 29, 2020 at 9:41
  • Thanks. I'm not exactly sure what I'm looking at but I've posted the debugging results and it does look like the Invoice Id is empty. Here is a complete message for the error: "Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Invoice]: [Invoice]". I've included more info on my JS as well. Please feel free to take a look. Apr 29, 2020 at 10:31
  • It looks like the Id property of the invoice is being returned with the lowercase label in the result object. Try invoice.id, javascript is cap sensitive Apr 29, 2020 at 11:46
  • Thanks for all the help, you guys. It was indeed a case sensitivity issue. Using the browser as a debugging tool was pretty helpful too! Apr 29, 2020 at 13:20

1 Answer 1

1

I guess, invoice id is coming as undefined because of case sensitive issue. Can you try to change the line this.invId = invoice.Id; to this.invId = invoice.id;

Let me know if it is not working...

2
  • Brilliant. I would have never thought it was because of a case sensitive issue. Thanks for the help! Apr 29, 2020 at 13:18
  • Welcome sid! I think it is working now. Apr 29, 2020 at 13:21

You must log in to answer this question.

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