0

I'm trying to retrieve the name of my newly created Invoice record. This record is being created with LDS, so I can easily retrieve the newly created record's id. I have no issues using this id for other purposes such as creating other records and relating them to this Invoice. What I can't seem to figure out is how to retrieve the Name so I can display it in a Toast. I've used Apex (as seen below) to query the created Invoice record just to retrieve the name.

I must be missing something as I can't get this to work. Please see my code below:

JS

import getInvoiceName from '@salesforce/apex/ApexController.getInvoiceName';


handleSave() {
    invId;

    // CREATE THE INVOICE RECORD (Lds)
    createRecord(recordInput)
    .then(invoice => {
          this.message = invoice;
          this.error = undefined;  
          if(this.message !== undefined) {
             this.invId = invoice.id;

          // CREATE THE INVOICE LINE RECORDS (Apex)
          createInvLines({ lstConIds : this.selectedRecords, invoiceId : this.invId})
          .then(result => {

              // ATTEMPT TO RETRIEVE THE INVOICE NAME (Apex)
              getInvoiceName({ invoiceId : this.invId})
              .then(result => {let invName = result.Name;}).catch(error => {this.error = error;});

                    // ATTEMPT TO SHOW SUCCESS TOAST
                    this.dispatchEvent(new ShowToastEvent({
                    title: 'Invoice "'+this.invName+'" was created.', 
                    variant: 'success'}),
                    );

     });
}

Controller

@AuraEnabled
public static Invoice__c getInvoiceName(string invoiceId){
     return [SELECT Id, Name
             FROM Invoice__c
             WHERE Id =: invoiceId][0];
     }

With this current setup, I get the toast 'Invoice "undefined'" was created' instead of having the Invoice name. What do I need to do to correctly retrieve the Name from a result?

Thank you!

Updated code:

Finally got it to work. Messy, as I still call the Apex just to get the name. I really like the idea of getting the record back through @wire and firing the toast (or navigation) from there.

import { getRecord } from 'lightning/uiRecordApi';

.......

savedRecordId;

@wire(getRecord, { recordId: '$savedRecordId', fields: [ 'id' ] })
savedRecordIdWire({data,error}) {
    if (data) {
        // call the apex here
        getInvoiceName({ invoiceId : data.id})
        .then(result => {
            // show toast here
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Invoice "'+result.Name+'" was created.',
                    variant: 'success'
                    }),
                );
            // NAVIGATE TO INVOICE RECORD PAGE
            this[NavigationMixin.Navigate]({
                type: 'standard__recordPage',
                attributes: {
                recordId: data.id,
                objectApiName: 'Invoice__c',
                actionName: 'view'
            }});
        })
        .catch(error => {
            this.error = error;
        });
        this.error = undefined;    
    } else if (error) {
        this.error = error;
        this.record = undefined;
    }    
}

.......

// CREATE THE INVOICE LINE RECORDS
createInvLines({ lstConIds : this.selectedRecords, invoiceId : this.invId})
.then(result => this.savedRecordId = this.invId)
1

You don't get the name back by default when you create a record, but you could use the LDS (Lightning Data Service) to get the record back.

import { getRecord } from 'lightning/uiRecordApi';

...

savedRecordId;

@wire(getRecord, { recordId: '$savedRecordId', fields: [ 'Name' ] })
savedRecordIdWire({data,error}) {
  // show toast here //
}

...

createRecord(data)
.then(result => this.savedRecordId = result.id)
| improve this answer | |
  • I really like this answer but I think OPs real question is about calling apex from LWC js controllers. I think the answer could be improved by also providing some information on this topic. – gNerb May 7 at 17:28
  • Hi, I'm confused. Once a record is created, it passes the id reactively but how would I call my Toast function from there? And I'll be using "data.Name" for my value right? – Sid Fortunato May 8 at 3:25
  • @SidFortunato In the wire handler, call the ShowToast function. And yes, it should be data.Name. – sfdcfox May 8 at 15:18
  • Hi, @sfdcfox. I still cannot get anything to execute under the "savedRecordIdWire" wire handler. I've updated my post with code based on your suggestion. Can you please help me point out anything I'm missing? Thanks. – Sid Fortunato May 9 at 5:02
  • Never mind my last comment. It turns out I was referencing the wrong id to get the record back. I finally was able to execute toast under the wire handler but sadly everything else except for id still returns as undefined. In the end, I still used the Apex just to get the name. I really like this suggestion but I don't know if I did it correctly. But hey, it works. Thanks for the help! – Sid Fortunato May 9 at 8:52
0

Try this.

handleSave() {
    invId;
    invName;

    // CREATE THE INVOICE RECORD (Lds)
    createRecord(recordInput)
    .then(invoice => {
          this.message = invoice;
          this.error = undefined;  
          if(this.message !== undefined) {
             this.invId = invoice.id;

          // CREATE THE INVOICE LINE RECORDS (Apex)
          createInvLines({ lstConIds : this.selectedRecords, invoiceId : this.invId})
          .then(result => {

              // ATTEMPT TO RETRIEVE THE INVOICE NAME (Apex)
              getInvoiceName({ invoiceId : this.invId})
              .then(result => {this.invName = result.Name;}).catch(error => {this.error = error;});

                    // ATTEMPT TO SHOW SUCCESS TOAST
                    this.dispatchEvent(new ShowToastEvent({
                    title: 'Invoice "'+this.invName+'" was created.', 
                    variant: 'success'}),
                    );

     });
}
| improve this answer | |
  • Sadly, this version shows an undefined value as well. – Sid Fortunato May 8 at 3:26

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.