Add a Toast Message
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import { ShowToastEvent } from 'lightning/platformShowToastEvent'; this.showToast('error', 'Error', error.body.message); showToast(type, title, message) { this.dispatchEvent( new ShowToastEvent({ title: title, variant: type, message: message }) ); } |
Apex Imperative Calling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import fetchRecords from '@salesforce/apex/ApexClass.fetchRecords'; connectedCallback() { this.showSpinner = true; fetchRecords({ 'recordId': this.recordId }) .then(result => { console.log(result); if (result) { } }).catch(error => { if (error && error.body && error.body.message) { this.showToast('error', 'Error', error.body.message); } }).finally(() => this.showSpinner = false); } |
Find the element Index in List
1 2 3 4 5 6 |
let index = this.loanStages.findIndex(x => x.label === 'test'); if (index != -1) { this.loanStages[index] } |
Loops in JS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// FOR LOOP for(let ind = 0; ind < this.recordList.length; ind++) { // } // FOR IN LOOP const person = {fname:"John", age:25}; for (let x in person) { console.log(x, person[x]); //fname John //age 25 } // FOR OF LOOP const cars = ["BMW", "Volvo", "Mini"]; for (let x of cars) { console.log(x); } |
Set in JS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
const letters = new Set(); letters.add("a"); letters.add("b"); console.log(letters.has(5)); //false letters.delete("a"); console.log(letters.size); letters.forEach (function(value) { console.log(value); }); for (const item of letters) { console.log(item); } for (const item of letters.keys()) { console.log(item); } for (const item of letters.values()) { console.log(item); }; |
Map in JS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
const fruits = new Map(); fruits.set("apples", 500); fruits.set("bananas", 300); fruits.set("oranges", 200); console.log(fruits.get("apples")); fruits.delete("apples"); console.log(fruits.size); console.log(fruits.has("apples")); fruits.forEach (function(value, key) { console.log(key, value); }); for (const [key, value] of fruits) { console.log(`${key} = ${value}`); } for (const [key, value] of fruits.entries()) { console.log(`${key} = ${value}`); } for (const key of fruits.keys()) { console.log(key); } for (const value of fruits.values()) { console.log(value); } |