Thursday, May 2, 2019

How to check field exists or not on sobject using Apex?

Some scenarios, we need to validate field is exists or not on the object.
use below simple code it checks the field on an object if the field has existed on the object it returns true otherwise it returns false.

Example:

Boolean bHasField = Account.getSobjectType().getDescribe().fields.getMap().keySet().contains('phone');
System.debug('bHasField =====> '+bHasField);

Result:

How to use custom metadata type values in Process Builder

In the Summer 19 release, Salesforce introduced the option to use custom metadata type values in process builder, formulas, and validation rules.

if you don't have pre-release dev org, sign up using below link
https://www.salesforce.com/form/signup/prerelease-summer19/

Syntax to use Metadata value in Formulas
$CustomMetadata.CustomMetadataTypeAPIName.RecordAPIName.FieldAPIName

Example
I created the Account_Generic_Data__mdt for this demo.

and created the Value__c filed on Account Generic Data Custom metadata type.
and I created the record

To Use this value in formulas and process builders
Example:
$CustomMetadata.Account_Generic_Data__mdt.Acc_Minimum_Amount.Value__c 

Resource:
https://releasenotes.docs.salesforce.com/en-us/summer19/release-notes/rn_forcecom_development_custom_metadata_process_builder.htm?edition=&impact=

Thursday, April 25, 2019

how to use lightning web component(lwc) in lightning communities

This post explains how to use lighting web components in communities.

To use the lightning component in communities component must implement the forceCommunity:availableForAllPageTypes interface but in lightning web components we will use the Configuration file.
Ex: <componentname>.js-meta.xml
Config the Configuration file as per the requirement.

Example:

lwcCommunitiesDemo.html
<template>
    <lightning-card title="Design Attribute Demo" icon-name="custom:custom19">
        <center>
            <h3>HI, <b>{strName}</b></h3><br />
            <div if:true={showDetails}>
                <ul>
                    <li> Emp Name: <b>{empName}</b></li>
                    <li> Emp Department: <b>{empDepartment}</b></li>
                    <li> Emp Location: <b>{empLocation}</b></li>
                    <li> Emp Age: <b>{empAge}</b><br /></b></li>
                    <li> Emp Gender: <b>{empGender}</b></li>
                </ul>
            </div>
        </center>
    </lightning-card>
</template>

lwcCommunitiesDemo.js
export default class lwcCommunitiesDemo extends LightningElement {
    @api showDetails = false;
    @api strName;
    @api empName;
    @api empDepartment;
    @api empLocation;
    @api empAge;
    @api empGender;
}

<target>
1.  This property indicates that where we need to use the component(record page, app page, and home page, etc).
2.  To use component in communities add lightningCommunity__Page, lightningCommunity__Default targets in Configuration file.
<targetConfigs> 
These properties are used to design your component as per your need.

lwcCommunitiesDemo.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="lwcCommunitiesDemo">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
      <targets>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>

      <targetConfigs>
        <targetConfig targets="lightningCommunity__Default">
            <property name="strName" type="String" default="Salesforce Code Crack" label="Enter the Employee Name"/>
            <property name="showDetails" type="Boolean" default="true" label="Do you want to Show Details ?"/>
            <property name="empName" type="String" default="" label="Enter Employee Name"/>
            <property name="empDepartment" type="String" default="" label="Enter Employee Department"/>
            <property name="empLocation" type="String" label="Enter Employee Location" datasource="Hyderabad, Delhi, Pune, Noida"/>
            <property name="empAge" type="integer" label="Enter Employee Age"/>
            <property name="empGender" type="String" label="Enter Employee Gender" datasource="Male, Female"/>
        </targetConfig>
      </targetConfigs>

</LightningComponentBundle>

Result