Saturday, November 30, 2019

How to remove ‘noreply@salesforce.com on behalf of’ on email sent from salesforce

Salesforce automatically adds the "no-reply@Salesforce.com on behalf of" to the sender field on emails sent from Salesforce.
To remove the "no-reply@Salesforce.com on behalf of" we have to deselect the "Enable Sender ID compliance" permission in email delivery setting.

Path:
From Setup, enter Email Administration(Classic) || Email(Lightning Experience) in the Quick Find box, and then Deliverability
Under the Email Security Compliance section, Deselect the Enable Sender ID compliance box and then click Save.


Monday, November 25, 2019

Undelete trigger event in apex trigger in Salesforce


Before Undelete

The record is available, but the field isDeleted set to True. the before undelete trigger wouldn’t be able to see the records(values) that it needs. due to this reason, there is no before undeleting operation available.

After undelete

The after undelete trigger event only works with recovered records—that is, records that were deleted and then recovered from the Recycle Bin through the undelete DML statement. These are also called undeleted records.

Ex:
If you delete an Account, an Opportunity may also be deleted. When you recover the Account from the Recycle Bin, the Opportunity is also recovered.
If there is an after undelete trigger event associated with both the Account and the Opportunity, only the Account after undelete trigger event executes.

The after undelete trigger event only fires for the following objects:

  • Account
  • Asset
  • Campaign
  • Case
  • Contact
  • ContentDocument
  • Contract
  • Custom objects
  • Event
  • Lead
  • Opportunity
  • Product
  • Solution
  • Task

Tuesday, November 19, 2019

How to get recently viewed information of user in org using SOQL

Salesforce stores the recently viewed information in RecentlyViewed object
RecentlyViewed represents records that the current user has recently viewed or referenced (by viewing a related record).

If you want to query the multiple sObject records recently viewed information.
Ex: I want to see the recently viewed account and contact records in org.
Select Id, Name, Type, LastViewedDate, Profile.Name FROM RecentlyViewed WHERE Type IN ('Account', 'Contact') ORDER BY LastViewedDate DESC



You can also use the LastViewdDate field on the sObject. 
Ex: I want to see only recently viewed Account records.
SELECT Id, Name, LastViewedDate FROM Account WHERE LastViewedDate != NULL ORDER BY LastViewedDate DESC limit 5


Resource
RecentlyViewed Object

Monday, November 18, 2019

How to deploy custom metadata type using ANT tool in Salesforce

Use the below package.xml to retrieve/deploy the custom metadata type using ANT tool
Custom metadata type is like custom object but you have use __mdt
Ex: custommetdatatypename__mdt

package.xml
<!-- Custom Metadata Type -->  
<types>  
    <members>CustomMetadataAPIname__mdt</members>  
    <name>CustomObject</name>  
</types>  

<!-- Custom Metadata Type records/data-->  
<types>  
    <members>CustomMetadata__mdt.RecordName</members>  
    <name>CustomMetadata</name>  
</types>  
 


Tuesday, November 12, 2019

How to get Record Id and Object Name into Lightning Web Component(lwc)

We used the force:hasRecordId interface to a Lightning component to enable the component to be assigned the ID of the current record. The current record ID is useful if the component is used on a Lightning record page.

We used the force:hasSObjectName interface to a Lightning component to enable the component to be assigned the API name of the current record’s sObject type. The sObject name is useful if the component can be used with records of different sObject types.

To get the record id and sObjectName in Lightning web components we can use the public decorate '@api'
@api recordId;
@api objectApiName;

HTML Code
<template>
    <lightning-card>
        <div class="slds-text-heading_medium slds-text-align_center">
            Record Id : {currenRecordId}
        </div>
        <div class="slds-text-heading_medium slds-text-align_center">
            Object Name : {currenObjectName}
        </div>
    </lightning-card>
</template>

Javascript Controller
import { LightningElement, api, track } from 'lwc';

export default class LWCDemo extends LightningElement {
    @api objectApiName;
    @api recordId;
    @track currenObjectName;
    @track currenRecordId;

    connectedCallback() {
        this.currenRecordId = this.recordId;
        this.currenObjectName = this.objectApiName;
    }
}
Configuration File
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="LWCDemo">
    <apiVersion>46.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>

Output

Saturday, November 9, 2019

Convert Attachment to File in Salesforce Using Apex

Note:
If you want to modify System audit Fields like CreatedDate, CreatedById, etc fields while converting to file. you must have the below permission "Set Audit Fields upon Record Creation" and "Update Records with Inactive Owners" 


How to Enable?

Path:
From Setup, enter User Interface in the Quick Find box, and then select "Set Audit Fields upon Record Creation" and "Update Records with Inactive Owners" 


Activating this feature in User Interface allows you to grant permission to set audit fields and update records with inactive owners, but it does not grant the permission by default.
You will need to grant the following profile permissions to your Users(Who are converting to file)

Set Audit Fields upon Record Creation - Allow the User to set audit fields (like 'Created By' or 'Last Modified By') when you create a record via API importing tools like Data Loader.
Update Records with Inactive Owners - Allow the User to update record owner and sharing-based records with inactive owners.

Add a permission set

1. Navigate to Setup.
2. Quick Find 'Permission Set'.
3. Click New.
4. Enter the name of the permission set.
5. Under System Permissions, grant any of the 2 permissions:
    i. Set Audit Fields upon Record Creation
    ii. Update Records with Inactive Owners
6. Click Save.
7. Click Manage Assignments.
8. Search for the name of the Users that will need the permission.
9. Click Add Assignment.

After you have permission now you are ready to convert the file.

Sample Code
list<ContentVersion> lstCntVersionsToInsert = new list<ContentVersion>();
list<Attachment> lstAttachments = [SELECT Id, ParentId, Name, IsPrivate, Body, OwnerId, CreatedById, CreatedDate, Description FROM Attachment where ParentId != null];
for(Attachment attIterator : lstAttachments) {
 ContentVersion objCntVersion = new ContentVersion();
 objCntVersion.Title = attIterator.Name;
 objCntVersion.PathOnClient = '/' + attIterator.Name;
 objCntVersion.VersionData = (attIterator.Body == null ? Blob.valueOf('.') : attIterator.Body);
 objCntVersion.Description = attIterator.Description;
 objCntVersion.SharingPrivacy = 'N'; // Can be always public.
 objCntVersion.FirstPublishLocationId = attIterator.ParentId; // Parent Id
 objCntVersion.OwnerId = attIterator.OwnerId;
 // To avoid "Documents in a user's private library must always be owned by that user" error.
 objCntVersion.CreatedById = attIterator.OwnerId; 
 objCntVersion.CreatedDate = attIterator.CreatedDate;
 objCntVersion.FirstPublishLocationId = attIterator.OwnerId;
 lstCntVersionsToInsert.add(objCntVersion);
}

if(!lstCntVersionsToInsert.isEmpty()) {
    insert lstCntVersionsToInsert;
}

How to retrieve Lightning Web Components Using ANT tool

To retrieve the Lightning web components use new Metadata API component is called LightningComponentBundle

Package.xml
<types>
    <members>*</members>
    <name>LightningComponentBundle</name>
</types>


Reference
LightningComponentBundle

Friday, November 8, 2019

How to Get the Session Id of Current User in Lightning Development



You may be faced with Error is "errorCode": "INVALID_SESSION_ID"  while doing the external web services and internal salesforce API Callouts with Salesforce in lighting development.

The session id can be fetched through UserInfo.getSessionId() method in apex but which is not available lightning context.
you can use the below solution it will resolve the error.

Visualforce Page
<apex:page contentType="application/json">
    {!$Api.Session_ID}
</apex:page>

Apex Class
public class SessionIdController {
 public static String getSessionId() {
        return Page.GenerateSessionIdForLWC.getContent().toString();
    }
}

simply use the Apex method where you need to the Session-Id.

lightning Button group in Lightning web component(lwc)

HTML Code
<template>
    <lightning-card>
    <div class="slds-m-top_medium slds-m-bottom_x-large">
        <h2 class="slds-text-heading_medium slds-m-bottom_medium">
            Simple Button group using lightning buttons.
        </h2>
        <lightning-button-group>
            <lightning-button label="Refresh" onclick={handleButtonGroup}></lightning-button>
            <lightning-button label="Edit" onclick={handleButtonGroup}></lightning-button>
            <lightning-button label="Save" onclick={handleButtonGroup}></lightning-button>
        </lightning-button-group><br/><br/>

        <p>You clicked on <b>{value}</b> button</p>
    </div>
    </lightning-card>
</template>

Javascript Controller
import { LightningElement, track } from 'lwc';

export default class ButtonGroupBasic extends LightningElement {
    @track value;
    handleButtonGroup(event) {
        console.log('label ===> '+event.target.label);
        this.value = event.target.label;
    }
}

Output

How to get available Lightning Components In your org

Note: My domain should be enabled in your org

  • Login to your org 
  • Add componentReference/suite.app in your org domain URL
Ex:
https://<yourorgdomain>.lightning.force.com/componentReference/suite.app

you can see all the available components of your Org along with Salesforce Lightning Component and Lightning web components examples.


Thursday, November 7, 2019

Calculate number of years between two dates in salesforce using apex

Sample Code
Date dtToday = Date.today();
Date dtOther = Date.today().addMonths(50);
system.debug('Number of months  ===> ' + dtToday.monthsBetween(dtOther));
String strNoOfYears = String.valueOf(dtToday.monthsBetween(dtOther) / 12) + ' Year(s) ' + String.valueOf(Math.mod(dtToday.monthsBetween(dtOther), 12)) + ' Month(s)';
system.debug('Difference b/w two dates ====>  ' + strNoOfYears);

Result

Saturday, November 2, 2019

Lightning Formatted Address component in Lightning Web Component(lwc)

We can show the formatted address using the lightning-formatted-address component in a lightning web component.
This example shows how to use the lightning-formatted-address
Ex:
HTML Code
<template>
    <lightning-card>
        <lightning-formatted-address city={accCity} 
                                     country={accCountry} 
                                     postal-code={accPostalcode} 
                                     province={accState} 
                                     street={accStreet}
                                     show-static-map={showStaticMap}></lightning-formatted-address>
    </lightning-card>
</template>
Javascript Controller
import { LightningElement, api, wire, track} from 'lwc';
import { getFieldValue, getRecord } from 'lightning/uiRecordApi';
import ACC_BillingCity from '@salesforce/schema/Account.BillingCity';
import ACC_BillingStreet from '@salesforce/schema/Account.BillingStreet';
import ACC_BillingState from '@salesforce/schema/Account.BillingState';
import ACC_BillingPostalCode from '@salesforce/schema/Account.BillingPostalCode';
import ACC_BillingCountry from '@salesforce/schema/Account.BillingCountry';

export default class formatAddressComponent extends LightningElement {
    // recordId value provided by Account record page
    @api recordId;
    @track showStaticMap = true;
    @wire(getRecord, { recordId: '$recordId', fields: [ACC_BillingCity, ACC_BillingStreet, ACC_BillingState, ACC_BillingPostalCode, ACC_BillingCountry]})
    account;


    get accStreet() { 
        window.console.log('accpount ===> '+JSON.stringify(this.account.data))
        return getFieldValue(this.account.data, ACC_BillingStreet)
    }

    get accCity() { 
        return getFieldValue(this.account.data, ACC_BillingCity)
    }

    get accCountry() { 
        return getFieldValue(this.account.data, ACC_BillingCountry)
    }

    get accState() { 
        return getFieldValue(this.account.data, ACC_BillingState)
    }

    get accPostalcode() { 
        return getFieldValue(this.account.data, ACC_BillingPostalCode)
    }
}

Configuration File
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="formatAddressComponent">
    <apiVersion>46.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>

Output


Friday, November 1, 2019

Reusable Picklist in Salesforce using Lightning web components(lwc)

This post explains how to implement reusable picklist in the lightning web component(lwc)


To get the picklist values in lightning web components we can use 'getPicklistValues' or 'getPicklistValuesByRecordType' wire adapters, these wire adapters uses the lightning/uiObjectInfoApi scoped module to get the picklist values based on specific Field or based on Record Type.

HTML Code
<template>
    <lightning-combobox label={picklistlabel} 
                        name={picklistlabel} 
                        onchange={handleValueChange} 
                        options={options} 
                        placeholder="--None--" 
                        value={selectedValue}></lightning-combobox>
    <div if:true={error} style="margin-left:3%">
        <b style="color: red;">{error}</b>
    </div>
</template>
Javascript Controller
import {LightningElement, api, track, wire} from 'lwc';
import {getPicklistValues, getObjectInfo} from 'lightning/uiObjectInfoApi';

export default class Reusepicklistinlwc extends LightningElement {
    @api objAPIName;
    @api fieldAPIName;
    @track options = [];
    // pick list label
    @track picklistlabel;
    @track error;

    recordTypeId;
    objfieldAPIName;

    @wire(getObjectInfo, {objectApiName: '$objAPIName'})
    objectInfo(result) {
        if(result.data) {
            // Field Data
            let fieldData = result.data.fields[this.fieldAPIName];
            if(fieldData) {
                this.picklistlabel = fieldData.label;
            
                this.objfieldAPIName = {};
                this.objfieldAPIName.fieldApiName = fieldData.apiName;
                this.objfieldAPIName.objectApiName = result.data.apiName;
    
                this.recordTypeId = result.data.defaultRecordTypeId;
            }
            else {
                this.error = 'Please enter valid field api name';
            }
        }
        else if(result.error) {
            this.error = JSON.stringify(result.error);
        }
    }
    
    @wire(getPicklistValues, { recordTypeId: '$recordTypeId', fieldApiName: '$objfieldAPIName'})
    picklistValues({error, data}) {
        if (data) {

            let picklistOptions = [{ label: '--None--', value: '--None--'}];

            // Picklist values
            data.values.forEach(key => {
                picklistOptions.push({
                    label: key.label, 
                    value: key.value
                })
            });

            this.options = picklistOptions;

        } else if (error) {
            this.error = JSON.stringify(error);
        }
    }


    handleValueChange(event) {
        this.selectedValue = event.target.value;
    }
}

Configuration File
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="reusepicklistinlwc">
    <apiVersion>46.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

How to use the component

Add this component in your parent component and pass object API name and field API name

Ex:
<template>
    <lightning-card>
        <!-- Account Type picklist -->
        <c-reusepicklistinlwc field-a-p-i-name="Type" obj-a-p-i-name="Account"></c-reusepicklistinlwc>
        <!--Contact Lead source picklist-->
        <c-reusepicklistinlwc field-a-p-i-name="LeadSource" obj-a-p-i-name="Contact"></c-reusepicklistinlwc>
    </lightning-card>
</template>