0

I am trying to create a record in an object and on record creation I want to display the salesforce id of that particular record on the toast message. If I comment out the "message: "New School Salesforce ID: "+this.event.id," line from .js file, the toast message is coming up as expected but if I include that line trying to display the id, I face the below outcome:

enter image description here

My code is as shown below:

HTML ->

<template>
<lightning-card title="SCHOOL RECORD CREATION FORM">
<lightning-record-form 
object-api-name={objectApiName}
fields={fields}
onsuccess={handleSuccess}>
</lightning-record-form>
</lightning-card>
</template>

JS ->

import { LightningElement } from 'lwc';
import SCHOOL_OBJECT from '@salesforce/schema/School__c';
import NAME_FIELD from '@salesforce/schema/School__c.Name';
import PHONE_FIELD from '@salesforce/schema/School__c.School_Phone__c';
import EMAIL_FIELD from '@salesforce/schema/School__c.School_Email__c';
import FEES_FIELD from '@salesforce/schema/School__c.Fees__c';
import ADDRESS_FIELD from '@salesforce/schema/School__c.School_Address__c';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class CreateSchoolRecord extends LightningElement {

objectApiName = SCHOOL_OBJECT;
fields =[NAME_FIELD, PHONE_FIELD, EMAIL_FIELD, FEES_FIELD, ADDRESS_FIELD];

handleSuccess(event){
    const evt = new ShowToastEvent({
    title: "School record has been created",
    message: "New School Salesforce ID: "+this.event.id,
    variant: "success"
});
this.dispatchEvent(evt);   

}
}

XML ->

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<isExposed>true</isExposed>
<targets>
    <target>lightning__RecordPage</target>
    <target>lightning__AppPage</target>
    <target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>

3 Answers 3

2

Are you sure your event.id represents the Salesforce ID? In any case - you want to remove "this" (because your event variable is local):

message: "New School Salesforce ID: " + event.id,

Update in answering your comment:

Have a look at the documentation... - ops, is your code based on just this documentation? This is what I can read there:

message: 'Record ID: ' + event.detail.id,

Still doesn't work?

1
  • This did not work. Now the toast message shows "New School Salesforce ID: undefined" and not the Salesforce ID. Since I am creating the records using lightning record form, I am not sure how to fetch the id.
    – Night Guy
    Commented Feb 27, 2022 at 10:59
0

JavaScript has template literals you can use for this.

message: `New School Salesforce ID: ${event.id}`,

Also you create a link to the record in the toast message as well. See Displaying Links in Toasts section here.

3
  • Even after using message: New School Salesforce ID: ${event.id}, the toast message shows "New School Salesforce ID: undefined" and not the Salesforce ID.
    – Night Guy
    Commented Feb 28, 2022 at 7:58
  • Then the event.id must be undefined. Can you post the html for the LWC.
    – Andrew L.
    Commented Feb 28, 2022 at 14:16
  • I have already posted the html, js and xml in the question.
    – Night Guy
    Commented Feb 28, 2022 at 14:31
0

Use : message: "New School Salesforce ID: "+ event.detail.id instead of event.detail.Id ( small letter "i" for id)

Worked for me.

2
  • 1
    OP already appears to be using id with the lower case "i". Is this a comment on one of the existing answers?
    – Derek F
    Commented Dec 5, 2022 at 15:18
  • I added this as an answer to @Night Guy as he mentioned that previous solutions didn't work for him. And I faced the similar issue before the solution I posted...
    – PSS
    Commented Jan 3, 2023 at 8:13

You must log in to answer this question.

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