1

I'm successfully creating a new record from my LWC, but I can't seem to access the content of the new record in the 'then' part of the code. I would like to display the Name of new record (which is an auto-generated number) as part of the success message. What am I missing? I thought the newly created record was accessable?

createRecord(recordInput)
.then(cert => {
    alert('certificate: ' + cert);
    this.certref = cert.Name;
    alert('certificate Id + Name : ' + cert.Id + ' ' + cert.Name);
    this.dispatchEvent(
        new ShowToastEvent({
            title: 'Success',
            message: 'Certificate created - Ref No: ' + this.certref,
            variant: 'success',
        }),
    );
})

The first alert:

enter image description here

The second alert:

enter image description here

5
  • 2
    Have you tried cert.id and/or cert.name (lowercase field names)? The examples I see in the docs seems to access the field names in lowercase. – Jayant Das Apr 14 '20 at 2:23
  • Hey that works for the id, which it now returns, but not the name. Does this mean the name is not accessible? – Irene Apr 14 '20 at 4:33
  • Do you have the name field set in fields attribute for createRecord? – Jayant Das Apr 14 '20 at 4:36
  • No I didn't, so I added it but it's not making a difference. It still comes back as undefined. It is an Auto Number so it's generated on creation. – Irene Apr 14 '20 at 4:53
  • you may can try to use a @wire getRecord using the returned Id to get the Name of the created record. – ivan Apr 14 '20 at 8:20
1

Taking a look at Create a Record documentation, the field's values are returned in the fields attribute and that every field in that map itself is a JSON string.

With a working example as on the LWC docs for Create Records, I could see that every field returned in the fields attribute have two attributes namely displayValue and value (print from the console for this test)

enter image description here

So to get the Name field's value, I had to parse the Proxy object account that was returned from server as JSON, and then get the Name field's value.

createRecord(recordInput)
        .then(account => {
            ...
            const retAccount = JSON.parse(JSON.stringify(account));
            console.log(retAccount.fields.Name.value); // this is the field's value
            ...
)

In your case, you will need to do the same thing, just by replacing the account in the above example with your attribute name cert should yield the result.

1
  • 1
    Yes, it all works now - great thanks! – Irene Apr 14 '20 at 21:50

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.