Thursday, December 19, 2019

How to cover the Approval Process in test class in Salesforce

# Approach 1

Ex: you have an approval process on an opportunity when the amount is greater than 500 the opportunity requires an approval.
Create the opportunity test data as per the approval process criteria and query the process instance object data and use the assert methods to verify the values.

Sample Code
Opportunity objOpp = new Opportunity(Name = 'Test Opp', amount = 800);
insert objOpp;

ProcessInstance objProcessInstance = [SELECT Id,TargetObjectId, CreatedDate FROM ProcessInstance WHERE TargetObjectId = :objOpp.Id];
System.assertEquals(objOpp.CreatedDate,objProcessInstance.CreatedDate);

# Approach 2

Create the Opportunity test data and submit the Opportunity for the approval process using the ProcessSubmitRequest method.

Sample Code
Opportunity objOpp = new Opportunity(Name = 'Test Opp', amount = 500);
insert objOpp;

Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
app.setObjectId(objOpp.id);
Approval.ProcessResult result = Approval.process(app);

Tuesday, December 17, 2019

How to get Custom Metadata Type data without Apex in Lightning Web Components(lwc)

What is Custom Metadata Type in Salesforce?
Custom metadata are like custom settings but records in custom metadata type considered as metadata rather than data.
These are typically used to define application configurations that need to be migrated from one environment to another, or packaged and installed.

You cannot perform CUD (Create, Update, Delete) operation on custom metadata type in apex.

Custom Metadata Type object suffix with __mdt

Normally in apex, we are using SOQL query to get the Custom metadata type data.

How to get the Custom Metadata Type data in Lightning web components?

To get the Custom metadata type data in the lightning web component you can use the wire service to get record data. To get the data, the Lightning Web Component uses the getRecord wire adapter.

Syntax
import { getRecord } from 'lightning/uiRecordApi';

For the demo, I created the Country and Amount(Country_And_Amount__mdt) Custom Metadata type object and records.

Country_And_Amount__mdt object


Country_And_Amount__mdt records



HTML Code
<template>
    <lightning-card title="Custom Metadata Types Record Values">
        <template if:true={objMetadataValues}>
            <dl class="slds-list_horizontal slds-wrap" style="margin-left: 3%;">
                <dt class="slds-item_label slds-truncate" title="First Name">Master Label</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objMetadataValues.MasterLabel}</b></dd>
                <dt class="slds-item_label slds-truncate" title="Last Name">Developer Name:</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objMetadataValues.DeveloperName}</b></dd>
                <dt class="slds-item_label slds-truncate" title="Full Name">Amount:</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objMetadataValues.Amount}</b></dd>
            </dl>
        </template>
    </lightning-card>
</template>

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

// import to get the record details based on record id
import { getRecord } from 'lightning/uiRecordApi';

export default class metadataDetailsInLWC extends LightningElement {

    @track objMetadataValues = {};
    
    // using wire service getting current user data
    // HardCoded the Record Id you can dynamically pass the record id using track or api decorates
    @wire(getRecord, { recordId: 'm02B00000006hGx', fields: ['Country_And_Amount__mdt.MasterLabel', 'Country_And_Amount__mdt.DeveloperName', 'Country_And_Amount__mdt.Amount__c'] })
    userData({error, data}) {
        if(data) {
            let currentData = data.fields;
            this.objMetadataValues = {
                MasterLabel : currentData.MasterLabel.value,
                DeveloperName: currentData.DeveloperName.value,
                Amount: currentData.Amount__c.value

            }
        } 
        else if(error) {
            window.console.log('error ====> '+JSON.stringify(error))
        } 
    }

}

Output

Monday, December 16, 2019

Use of VisualforceAccessMetrics object in the Salesforce

This object is used to query the metrics of the visualforce page in your org.
Using VisualforceAccessMetrics, you can track the number of views each Visualforce page in your org receives in a 24-hour time period. To find out how many views a page got over the course of multiple days, you can query multiple VisualforceAccessMetrics objects for the same ApexPageId.

Page views are tallied the day after the page is viewed, and each VisualforceAccessMetrics object is removed after 90 days.

To get the metrics of the visualforce page data, make a SOQL query in Workbench to get information from the VisualforceAccessMetrics object.

1. Log in to Workbench.
2. Select production/sandbox.
3. select 'SOQL Query'.

SOQL Query
SELECT ApexPageId, DailyPageViewCount, Id, ProfileId, MetricsDate, LogDate FROM VisualforceAccessMetrics

Output

Sunday, December 15, 2019

Recycle Bin in Salesforce Lightning Experience

In 'Winter 20' salesforce releases the Recycle bin for Lightning Experience.

Recycle bin contains all the features like list view, Delete, restore, empty recycle bin.

Wednesday, December 11, 2019

How to disable task notification when task is assigned to the user in salesforce

Go the user personal settings, enter Activity Reminders in the Quick Find box, then select Activity Reminders.
Deselect Email me when someone assigns me a task.


Monday, December 2, 2019

How to make default Landing Page as Setup page in Salesforce

To make the default landing page as setup page, go to the 'Advanced User Details' of the user and enable the 'Make Setup My Default Landing Page' permission.


Sunday, December 1, 2019

How to get Picklist values based on record type in Lightning Web Components(lwc)

To get picklist values based on record type we have to use getPicklistValuesByRecordType wire adapter.
Syntax
import { getPicklistValuesByRecordType } from 'lightning/uiObjectInfoApi';

In this demo, I am using Account object, based on record type id I am getting picklist values of Industry field

HTML Code
<template>
    <lightning-card title="Picklist Vlaues based on Record Type">
      <div if:true={options}>
            <lightning-combobox name="progress"
                                label="Industry"
                                value={value}
                                placeholder="-Select-"
                                options={options}
                                onchange={handleChange} ></lightning-combobox><br/>
        
            you selected industry: <b>{value}</b><br/><br/>
      </div>
    </lightning-card>
    
</template>

Javascript Controller
import { LightningElement, track, wire} from 'lwc';
import { getPicklistValuesByRecordType } from 'lightning/uiObjectInfoApi';

export default class PicklistValuesByRecordType extends LightningElement {
    @track value;
    @track options;

    @wire(getPicklistValuesByRecordType, { objectApiName: 'Account', recordTypeId: '012B0000000gTUOIA2' }) 
    IndustryPicklistValues({error, data}) {
        if(data) {
            this.options = data.picklistFieldValues.Industry.values;
        }
        else if(error) {
            window.console.log('error =====> '+JSON.stringify(error));
        }
    }

    handleChange(event) {
        this.value = event.target.value;
    }
}

Output