Category Archives: Salesforce

Salesforce Workflow Rule Evaluation Criteria

created :

  • The workflow rule will be evaluated the rule criteria when a record is created. If the rule criteria is met, run the rule.
  • With this option, the rule never runs more than once per record.

created, and every time it’s edited :

  • The workflow rule will be evaluated the rule criteria each time a record is created or updated. If the rule criteria is met, run the rule.
  • With this option, the rule repeatedly runs every time a record is edited, as long as the record meets the rule criteria.
  • We cannot add time-dependent actions to the rule if you select this option.

created, and any time it’s edited to subsequently meet criteria :

  • The workflow rule will be evaluated the rule criteria each time a record is created or updated.
  • For a new record, run the rule if the rule criteria is met.
  • For an updated record, run the rule only if the record is changed from not meeting the rule criteria to meeting the rule criteria.
  • With this option, the rule can run multiple times per record, but it won’t run when the record edits are unrelated to the rule criteria.
  • For example, Suppose the Director wants to be notified when an Opportunity value is > $50,000. Using this criteria, the rule would fire when the Opportunity is created with a value $60,000 and the Director would get an email notifying him of this Opportunity. Now if the record is changed from a value of $60,000 to $75,000 the rule does not fire again because it has already met the criteria at its initial creation. Now if the reverse happened, and the value went from $60,000 to $40,000 nothing would happen since the record doesn’t meet the criteria for the rule. But, if in another subsequent change, the value increased to $55,000 the criteria would again be met, and since the record didn’t previously meet the criteria during the last change, the rule would again fire and the Director would receive a new alert.

Select All Fields with SOQL in Apex

Sample Code:

DescribeSObjectResult descResult = Account.getSObjectType().getDescribe();
List<String> fieldList = new List<String>(descResult.Fields.getMap().keySet());

String query = ' SELECT ' + String.join(fieldList, ',') + ' FROM ' + descResult.getName();
List<SObject> records = Database.query(query);

System.debug( records );

Select All Fields with SOQL in Apex by Using Record Id

Sample Code:

Id recordId = '00128000002KuXO';

DescribeSObjectResult descResult = recordId.getSObjectType().getDescribe();
List<String> fieldList = new List<String>(descResult.Fields.getMap().keySet());

String query = ' SELECT ' + String.join(fieldList, ',') + ' FROM ' + descResult.getName() + ' WHERE ' + ' id = :recordId';
List<sObject> records = Database.query(query);

System.debug(records);

Customize Lookup Field Mouse Hover Details

In Salesforce when we hover on a lookup field on a record detail page, we can quickly view some information related to the lookup record in a little popup window. That popup window is called mini page layout.

A mini page layout contains a subset of the items in an existing page layout. Sometimes we need to customize the mini page layout as per our requirement.

Here is the mini page layout of Account object, which is showing in Contact record details page.

Follow below steps to edit mini page layout:

  • Go to Setup || Customize and choose a Standard object, or Go to Setup || Develop || Objects and choose a custom object.
  • Select Page Layouts, and choose a page layout name.
  • Click Mini Page Layout.

  • Select which fields and related lists will be displayed for this type of record in the mini view.

  • Updated Account object mini page layout view on Contact details page.

Note:

  • There are field that we cannot remove (Like – object name, Parent Account for account object) To hide this, we can use Field Level Security, but of course field will not be visible anywhere, like: page layout, report, etc.
  • There are field that always shown, example in User object, it will always show Title, Email, Phone, Manager.
  • We cannot choose related objects for the Close Case Layout or the Log a Case Page and View Cases Page layouts on the Self-Service Portal.

Using AggregateResult in Salesforce

The aggregate functions COUNT(fieldname), COUNT_DISTINCT(), SUM(), AVG(), MIN() and MAX() in SOQL return an AggregateResult object or a List of AggregateResult objects. We can use aggregate functions result in apex by using AggregateResult object.

Here is an example to use AggregateResult in Salesforce. In below example I’m using COUNT(fieldname) aggregate function in SOQL to show Account record respective number of Contacts.

Visualforce Page:

<apex:page controller="SampleController" action="{!getData}">
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockTable value="{!accWrapList}" var="acc">            
                <apex:column headerValue="Account Name" value="{!acc.AccountName}"/>
                <apex:column headerValue="Number of Contacts" value="{!acc.TotalContact}"/>
            </apex:pageblockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:

public with sharing class SampleController {
    public List<AggregateResult> result {get;set;}    
    public List<AccWrapper> accWrapList {get;set;}
    public List<Account> accList;
    public Map<Id, Account> accMap;
    List<Id> idList;
    
    public void getData() {
        accWrapList = new List<AccWrapper>();
        result = new List<AggregateResult>();
        idList = new List<Id>();
        accList = new List<Account>();
        accMap = new Map<Id, Account>();
        
        result = [SELECT COUNT(Id) Total, AccountId FROM Contact WHERE AccountId != null GROUP BY AccountId];               
        
        for(AggregateResult a : Result) {   
            idList.add((Id)a.get('AccountId'));
        }
        
        accList = [SELECT Id, Name FROM Account WHERE Id IN : idList];
        for(Account a : accList) {
            accMap.put(a.Id, a);            
        }
        
        for(AggregateResult aResult : result) {
            Account acc = accMap.get((Id)(aResult.get('AccountId')));
            accWrapList.add(new AccWrapper(aResult, acc.Name)); 
        }
    }
    
    public class AccWrapper {
        public Integer TotalContact {get;set;}
        public String AccountName {get;set;}
        
        public AccWrapper(AggregateResult a, String AccountName) {
            this.TotalContact =  (Integer)a.get('Total');
            this.AccountName = AccountName;
        }
    }
}

Output: