How to get RecordTypeId Based On RecordType Name In Apex Salesforce
Get RecordTypeId Based On RecordType Name In Apex
String egRecordTypeID = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Record Type Label').getRecordTypeId();
String egRecordTypeID = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Record Type Label').getRecordTypeId();
The disconnectedCallback() is one of the life cycle functions of modern web components.
It gets invoked automatically when the corresponding web component gets removed from DOM. If we need to perform any logic when the component is removed from DOM, that logic can be added in disconnectedCallback(). This hook flows from Parent component to child. We use disconnectedCallback() to purging caches or removing event listeners that are created on connectedCallback().
In this example, we have two components. Those are 'disconnectedCallbackParentLwc' and 'disconnectedCallbackChildLwc'. Here disconnectedCallbackChildLwc referred in parent component. In the parent component, we have a button 'Show/Hide' to show and hiding the child component. Initially, the child component displayed, once users click on the button 'disconnectedCallbackChildLwc' the child component removed from DOM. Since the component getting removed from DOM, disconnectedCallback function on the child component gets invoked and an alert will appear.
import { LightningElement } from 'lwc';
export default class DisConnectedCallbackChildLwc extends LightningElement {
disconnectedCallback() {
console.log('child disconnected callback')
}
}
<template>
<p>I am child LWC component</p>
</template>
import { LightningElement } from 'lwc';
export default class DisConnectedCallbackParentLwc extends LightningElement {
show = true;
handleShowHide() {
this.show = !this.show;
}
}
<template>
<lightning-card title="disconnectedCallback Example">
<p>Parent LWC component</p>
<lightning-button variant="brand" slot="actions" label="Show/Hide" onclick={handleShowHide}
class="slds-m-left_x-small"></lightning-button>
<template if:true={show}>
<c-dis-connected-callback-child-lwc></c-dis-connected-callback-child-lwc>
</template>
</lightning-card>
</template>
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class LwcShowToast extends LightningElement {
//Sample Success Toast message code
showSuccessNotification() {
const evt = new ShowToastEvent({
title: "Success",
message: "This is sample success message",
variant: "success",
});
this.dispatchEvent(evt);
}
//Sample code for showing error message in Toast
showErrorNotification() {
const evt = new ShowToastEvent({
title: "Error",
message: "This is sample error message",
variant: "error",
});
this.dispatchEvent(evt);
}
//Sample code for showing warning message in Toast
showWarningNotification() {
const evt = new ShowToastEvent({
title: "Warning",
message: "This is sample warning message",
variant: "warning",
mode: "sticky"
});
this.dispatchEvent(evt);
}
//Sample code for showing Info message in Toast
showInfoNotification() {
const evt = new ShowToastEvent({
title: "Info",
message: "This is sample info message",
variant: "info",
mode: "pester"
});
this.dispatchEvent(evt);
}
}
<template>
<lightning-card title="Toast Messages" icon-name="custom:custom19">
<div class="slds-p-around_medium">
<lightning-button variant="success" label="Show Success" onclick={showSuccessNotification}></lightning-button>
<lightning-button variant="destructive" label="Show Error" onclick={showErrorNotification}></lightning-button>
<lightning-button variant="destructive-text" label="Show Warning" onclick={showWarningNotification}></lightning-button>
<lightning-button label="Show Info" onclick={showInfoNotification}></lightning-button>
</div>
</lightning-card>
</template>
import { LightningElement, api } from 'lwc';
export default class LwcGetRecordId extends LightningElement {
@api recordId;
}
lwcGetRecordId.html
<template>
Current Record Id : {recordId}
</template>
<aura:registerEvent name="sampleComponentEvent" type="c:compEvent"/>
var compEvent = component.getEvent("sampleComponentEvent");
compEvent.setParams({"message" : "Static Text" });
compEvent.fire();
<aura:handler name="sampleComponentEvent" event="c:compEvent"
action="{!c.handleComponentEvent}"/>
<aura:event type="COMPONENT">
<aura:attribute name="message" type="String"/>
</aura:event>
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global" >
<aura:attribute name="enteredText" type="String"/>
<aura:registerEvent name="sampleComponentEvent" type="c:compEvent"/>
<div class="slds-box slds-p-around_medium">
<h2>This is Child Component</h2>
<lightning:input label="Enter Name" aura:id="name" value ="{!v.enteredText}" />
<lightning:button class="slds-m-top_small" variant="brand" label="Pass Value To Parent" title="Click Here" onclick="{! c.handleClick }"/>
</div>
</aura:component>
({
handleClick : function(component, event, helper) {
var compEvent = component.getEvent("sampleComponentEvent");
compEvent.setParams({
"message" : component.get("v.enteredText")
});
compEvent.fire();
}
})
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global" >
<aura:attribute name="enteredValue" type="String" />
<aura:handler name="sampleComponentEvent" event="c:compEvent" action="{!c.handleComponentEvent}"/>
<c:childComponentCmp/><br /><br />
<div class="slds-box slds-p-around_medium">
<h2>This is Parent Component</h2>
value entered on child component input Box: <b>{!v.enteredValue}</b>
</div>
</aura:component>
({
handleComponentEvent : function(component, event, helper) {
var valueFromChild = event.getParam("message");
alert(valueFromChild)
component.set("v.enteredValue", valueFromChild);
}
})
import labelName from '@salesforce/label/labelApiName';
import { LightningElement } from 'lwc';
//importing custom label to 'WELCOME_MESSAGE'
import WELCOME_MESSAGE from '@salesforce/label/c.Hello_World';
export default class CustomLabelLwc extends LightningElement {
message = WELCOME_MESSAGE; //assigning WELCOME_MESSAGE value to "message" property
}
<template>
<div class="slds-box slds-theme_shade slds-align_absolute-center">
<h1>{message}</h1>
</div>
</template>
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List<Account> getAccountList(){
return [SELECT Id, Name,Phone,Industry FROM Account order by createddate desc LIMIT 5];
}
}
<template>
<template if:true={isSpinner}>
<div class="spinner">
<lightning-spinner alternative-text="Loading" size="medium"></lightning-spinner>
</div>
</template>
<lightning-card title="connectedCallback Example">
<lightning-datatable data={accountList} columns={cols} key-field="Id">
</lightning-datatable>
</lightning-card>
</template>
import { LightningElement, wire, track } from 'lwc';
import getLatestAccounts from '@salesforce/apex/AccountController.getAccountList';
const COLS = [
{ label: 'Name', fieldName: 'Name', type: 'text' },
{ label: 'Stage', fieldName: 'Phone', type: 'text' },
{ label: 'Amount', fieldName: 'Industry', type: 'text' }
];
export default class ConnectedCallbackLwc extends LightningElement {
cols = COLS;
@track isSpinner = false;
@track accountList = [];
connectedCallback() {
this.isSpinner = true;
}
@wire(getLatestAccounts) fetchAccList(result) {
this.isSpinner = false;
if (result.data) {
this.accountList = result.data;
this.error = undefined;
} else if (result.error) {
this.error = result.error;
this.accountList = [];
}
}
}
<template>
<div class="slds-clearfix">
<lightning-button label="Left" class="slds-float_left"></lightning-button>
</div>
<div class="slds-clearfix">
<lightning-button label="I am Center" class="slds-align_absolute-center"></lightning-button>
</div>
<div class="slds-clearfix">
<lightning-button label="I am Right" class="slds-float_right"></lightning-button>
</div>
</template>
<aura:component >
<div class="slds-clearfix">
<lightning:button label="Left" class="slds-float_left"/>
</div>
<div class="slds-clearfix">
<lightning:button label="I am Center" class="slds-align_absolute-center"/>
</div>
<div class="slds-clearfix">
<lightning:button label="I am Right" class="slds-float_right"/>
</div>
</aura:component>
<template>
<div class="slds-box slds-theme_default">
<div class="slds-m-around_medium">
<lightning-input type="text" max-length=12 required label="Name" onchange={onNameChange}></lightning-input>
<lightning-input type="tel" label="Phone" onchange={onPhoneChange}></lightning-input>
<lightning-input type="email" required label="Email" onchange={onEmailChange}></lightning-input>
</div>
<div>
<center>
<lightning-button label="Save" onclick={saveContact}>
</lightning-button>
</center>
</div>
</div>
</template>
import { LightningElement, track } from 'lwc';
export default class LwcValidateInputForm extends LightningElement {
@track name;
@track phone;
@track email;
onNameChange(event) {
this.name = event.detail.value;
}
onPhoneChange(event) {
this.phone = event.detail.value;
}
onEmailChange(event) {
this.email = event.detail.value;
}
saveContact() {
const isInputsCorrect = [...this.template.querySelectorAll('lightning-input')]
.reduce((validSoFar, inputField) => {
inputField.reportValidity();
return validSoFar && inputField.checkValidity();
}, true);
if (isInputsCorrect) {
//perform success logic
}
}
}
if (object!= null){
string s= object.fieldName;
}
string s= object?.fieldName;
string accountName = accountIdAccountMap.get(accId).Name;
// this will return null pointer exception if accId not available in the map.
if(accountIdAccountMap.get(accId) != null) {
accountName = accountIdAccountMap.get(accId).Name;
}
if(accountIdAccountMap.containsKey(accId)) {
accountName = accountIdAccountMap.get(accId).Name;
}
string Account Name = accountIdAccountMap.get(accId)?.Name;
accountIdAccountMap?.get(accId)?.Name;
import { LightningElement,wire } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import STAGE from '@salesforce/schema/Opportunity.StageName';
export default class LwcPicklistWithRecordtype extends LightningElement {
@wire(getPicklistValues,
{
recordTypeId: '01228000000XckuAAC', //pass id dynamically
fieldApiName: STAGE
}
)
stageValues;
}
<template>
<div class="slds-box">
<template if:true={stageValues.data}>
<lightning-combobox name="progress" label="Opportunity Stage" value={value}
options={stageValues.data.values} onchange={handleChange}>
</lightning-combobox>
</template>
</div>
</template>
import { LightningElement,wire } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import LeadSource from '@salesforce/schema/Contact.LeadSource';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import CONTACT_OBJECT from '@salesforce/schema/Contact';
export default class LwcPicklistWithoutRecordtypeextends LightningElement {
@wire(getObjectInfo, { objectApiName: CONTACT_OBJECT })
contactInfo;
@wire(getPicklistValues,
{
recordTypeId: '$contactInfo.data.defaultRecordTypeId',
fieldApiName: LeadSource
}
)
leadSourceValues;
}
lwcPicklistWithoutRecordtype.html
<template>
<div class="slds-box">
<template if:true={leadSourceValues.data}>
<lightning-combobox name="progress" label="Lead Source" value={value}
options={leadSourceValues.data.values} onchange={handleChange}>
</lightning-combobox>
</template>
</div>
</template>
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List<account> getAccountList(){
return [SELECT Id, Name,Phone,Industry FROM Account order by createddate desc LIMIT 5];
}
}
import { LightningElement, wire, track } from 'lwc';
import { deleteRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
import getLatestAccounts from '@salesforce/apex/AccountController.getAccountList';
const COLS = [
{ label: 'Name', fieldName: 'Name', type: 'text' },
{ label: 'Stage', fieldName: 'Phone', type: 'text' },
{ label: 'Amount', fieldName: 'Industry', type: 'text' }
];
export default class LwcRefreshApex extends LightningElement {
cols = COLS;
@track selectedRecord;
@track accountList = [];
@track error;
@track wiredAccountList = [];
@wire(getLatestAccounts) accList(result) {
this.wiredAccountList = result;
if (result.data) {
this.accountList = result.data;
this.error = undefined;
} else if (result.error) {
this.error = result.error;
this.accountList = [];
}
}
handelSelection(event) {
if (event.detail.selectedRows.length > 0) {
this.selectedRecord = event.detail.selectedRows[0].Id;
}
}
deleteRecord() {
deleteRecord(this.selectedRecord)
.then(() => {
refreshApex(this.wiredAccountList);
})
.catch(error => {
})
}
}
<template>
<lightning-card title="Latest Five Accounts">
<lightning-button slot="actions" label="Delete Account" onclick={deleteRecord}></lightning-button>
<lightning-datatable
data={accountList} columns={cols} key-field="Id"
max-row-selection="1" onrowselection={handelSelection} >
</lightning-datatable>
</lightning-card>
</template>
Output:const lwcEvent= new CustomEvent('eventname', {
detail:{varible1:value, varible2 : value}
});
this.dispatchEvent(lwcEvent);
<c-child-lwc-component oneventName={handleEvent}></c-child-lwc-component>
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List<account> getAccountList(){
return [SELECT Id, Name,Phone,Industry FROM Account WITH SECURITY_ENFORCED LIMIT 10];
}
}
import { LightningElement, api } from 'lwc';
export default class LwcChild extends LightningElement {
@api accountList;
handleAccountClick(event) {
let selectedAccId = event.currentTarget.getAttribute("data-key");
//custom event
const passEvent = new CustomEvent('accountselection', {
detail:{recordId:selectedAccId}
});
this.dispatchEvent(passEvent);
}
}
<template>
<table class="slds-table slds-table_bordered">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Industry</th>
</tr>
</thead>
<tbody>
<template if:true={accountList}>
<template for:each={accountList} for:item="acc">
<tr key={acc.Id} data-key={acc.Id} onclick={handleAccountClick}>
<td>{acc.Name}</td>
<td>
<lightning-formatted-phone value={acc.Phone}></lightning-formatted-phone>
</td>
<td>{acc.Industry}</td>
</tr>
</template>
</template>
</tbody>
</table>
</template>
Create new lightning web component: lwcParentimport { LightningElement,wire,track } from 'lwc';
import getAccountList from '@salesforce/apex/AccountController.getAccountList';
export default class LwcParent extends LightningElement {
@track selectedAccountId;
@wire(getAccountList) accountList;
onAccountSelection(event){
this.selectedAccountId = event.detail.recordId;
}
}
<template>
<div class="row">
<div class="column" style="float: left; width: 70%; padding: 10px;">
<div class="slds-box">
<template if:true={accountList.data}>
<c-lwc-child account-list={accountList.data} onaccountselection={onAccountSelection}></c-lwc-child>
</template>
</div>
</div>
<div class="column" style="float: right; width: 30%; padding: 10px;">
<div class="slds-box">
<div slot="actions">
<div class="slds-box">
Selected Account Information
</div>
</div>
<lightning-record-view-form record-id={selectedAccountId} object-api-name="Account">
<div class="slds-box slds-them_shade">
<lightning-output-field field-name="Name"></lightning-output-field>
<lightning-output-field field-name="Phone"></lightning-output-field>
<lightning-output-field field-name="Industry"></lightning-output-field>
</div>
</lightning-record-view-form>
</div>
</div>
</div>
</template>
allCheckBoxChange(event) {
for (let j = 0; j < this.allOppList.length; j++) {
this.allOppList[j].isChecked = event.detail.checked;
}
}
allCheckBoxChange(event) {
let tempAllRecords = Object.assign([], this.allOppList);
for (let j = 0; j < this.allOppList.length; j++) {
let tempRec = Object.assign({}, tempAllRecords[j]);
tempRec.isChecked = event.detail.checked;
tempAllRecords[j] = tempRec;
}
this.allOppList = tempAllRecords;
}