Friday, January 12, 2024

Lightning Record Picker in Salesforce

The lightning-record-picker component allows you to search for a list of Salesforce Records that match search input. It uses the GraphQL wire adapter to search for records, displays the records, and allows the user to select a record.

 Ex:

HTML

<template>
	<lightning-card title="Lightning Record Picker" icon-name="standard:account">
	<lightning-record-picker label="Select Account" 
							 onchange={handleRecordChange} 
							 placeholder="Search Accounts..." 
							 object-api-name="Account"
							 display-info={displayInfo}>
		</lightning-record-picker>
		<template lwc:if={selectedRecordId}>Selected Record Id: {selectedRecordId}</template>
	</lightning-card>
</template>
JS

import { LightningElement } from 'lwc';

export default class RecordPicker extends LightningElement {
    selectedRecordId;

    displayInfo = {
        additionalFields: ['Phone', 'Industry']
    }
    handleRecordChange(event) {
        this.selectedRecordId = event.detail.recordId;
    }
}



Thursday, January 11, 2024

Dynamically detect the running context in salesforce using Apex

In the salesforce, the Request class provides a few methods among them we do have a getQuiddity () method that returns an enum(System.Quiddity) specifying the context of the origin of this request. 

Ex:

Quiddity contextInfo = Request.getCurrent().getQuiddity();
switch on contextInfo {
    when VF {
        system.debug('Visualforce');         
    } when AURA {
        system.debug('AURA');       
    } when FUTURE, SCHEDULED, QUEUEABLE, BATCH_APEX {
        system.debug('async context');        
    } when INVOCABLE_ACTION {
        system.debug('FLOW'); 
    } when ANONYMOUS {
        system.debug('ANONYMOUS');
    } when REST {
        system.debug('REST');        
    }
}
For Reference 

Wednesday, August 9, 2023

How to convert timestamp to datetime using apex in salesforce

 Sample Code:

String timestamp = '2023-08-07T14:53:49Z';
timestamp = timestamp.replace('T', ' ');
System.debug('Datetime ==> '+DateTime.valueOf(timestamp));

Screenshot: