Skip to content

Instantly share code, notes, and snippets.

View choudharymanish8585's full-sized avatar
💭
Never Settle

Manish Choudhari choudharymanish8585

💭
Never Settle
View GitHub Profile
@choudharymanish8585
choudharymanish8585 / salesforce.code-snippets
Created November 5, 2022 00:30
Use Apex, Lwc, and Aura code snippets for VS Code
{
/**
* My custom code snippets for Salesforce which makes life of a developer easy :)
* @author - Manish Choudhari
*/
"Package Creator": {
"scope": "xml",
"prefix": "package",
"body": [
public class AccountController {
@AuraEnabled
public static List<Account> getAccounts(){
return [SELECT
Id, Name, Phone, Rating, My_Custom_Field__c, Active__c
FROM Account LIMIT 10];
}
}
({
/**
* Init function to build data table columns and data from server
* Buid a page reference for the component where we need to navigate
* @author Manish Choudhari
* */
doInit : function(component, event, helper) {
//buiding a page reference for the component where we need to navigate
var pageReference = {
type: 'standard__component',
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes" access="global" controller="AccountController">
<aura:attribute name="pageReference" type="Object"/>
<aura:handler name="init" value="{! this }" action="{! c.doInit }"/>
<lightning:navigation aura:id="navService"/>
<!-- attributes -->
<aura:attribute name="data" type="Object"/>
<aura:attribute name="columns" type="List"/>
({
/**
* Get page reference and set record id, object name attribute
* @author Manish Choudhari
* */
doInit: function(component, event, helper) {
//getting page reference from pageReference attribute supplied by lightning:isUrlAddressable interface
var myPageRef = component.get("v.pageReference");
//get parameter from state
var recordId = myPageRef.state.c__recordId;
<aura:component implements="lightning:isUrlAddressable,force:appHostable,flexipage:availableForAllPageTypes">
<aura:attribute type="String" name="recordId" />
<aura:attribute type="String" name="objectName" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<lightning:card title="URL Addressable Component">
<lightning:recordForm
recordId="{!v.recordId}"
objectApiName="{!v.objectName}"
layoutType="Full"
({
/**
* Get Selected Value from AutoComplete Component
* @author Manish Choudhari
* */
getSelectedValue : function(component, event, helper) {
//find autocomplete component using aura id
const autoCompleteComponent = component.find("account-record");
if(autoCompleteComponent){
//get selected option from auto complete component's selectedOption attribute
<aura:component implements="flexipage:availableForAllPageTypes">
<aura:attribute type="String" name="selectedValue" />
<lightning:card title="AutoComplete Demo">
<lightning:layout multipleRows="true">
<lightning:layoutItem size="12" padding="around-small">
<c:AutoComplete aura:id="account-record" label="Account" objectApiName="Account" idFieldApiName="Id" valueFieldApiName="Name" />
</lightning:layoutItem>
<lightning:layoutItem size="12" padding="around-small">
<lightning:button label="Get Selected Value" variant="brand" onclick="{!c.getSelectedValue}" />
import { LightningElement, track } from 'lwc';
//Let's configure the columns of the data table
const columns = [
//Key here is cellAttributes object for Particular column
{label: 'Name', fieldName: 'name', type: 'text'},
//Diet type column can accept a css class. The css class name should be passed in field name "dietClass" from server
{label: 'Diet Type', fieldName: 'diet', type: 'text', cellAttributes: { class: { fieldName: 'dietCSSClass' }}},
{label: 'Blood Group', fieldName: 'bloodGroup', type: 'text'},
//likewise fvtColor column can also have a css class in fvtColorCSSClass field
<template>
<!--A very basic data table-->
<lightning-datatable
key-field="id"
data={data}
columns={columns}
is-loading={tableLoadingState}>
</lightning-datatable>
</template>