Category Archives: Salesforce

Difference between Salesforce external objects and custom objects

Feature Custom Objects External Objects
Data is stored in your Salesforce org Yes No
Read Yes Yes
Write Yes Yes (limited)
Tabs, layouts Yes Yes
Visualforce Yes Yes
Field-level security Yes Yes
Sharing Yes No
REST and SOAP API Yes Yes
SOQL Yes Yes (limited)
Search and SOSL Yes Yes (pass-through)
Formula fields Yes Not Yet
Workflow, triggers Yes Not Yet
Reports and analytics Yes Not Yet
Chatter Yes Yes (no field tracking)

Custom Rollup Summary Field Using Apex Trigger

Here is a sample Rollup Summary calculation trigger for Lookup relationship. In below example I’m using Account object as parent and Contact object as child.

I’m updating Account object Account record field Number_of_Contacts__c with Count of all related Contact records.

Apex Trigger:

trigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) {
    
    Set<Id> accountIds = new Set<Id>();
    
    if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete){
        for(Contact con:trigger.new){
            if(con.AccountId != null){
                accountIds.add(con.AccountId);
            }
        }
    }
    
    if(trigger.isUpdate || trigger.isDelete){
        for(Contact con:trigger.old){
            if(con.AccountId != null){
                accountIds.add(con.AccountId);
            }
        }
    }
    
    if(!accountIds.isEmpty()){
        List<Account> accList = [SELECT Id, Number_of_Contacts__c, (SELECT Id FROM Contacts) 
                                 FROM Account WHERE Id IN : accountIds];
        if(!accList.isEmpty()){
            List<Account> updateAccList = new List<Account>();
            for(Account acc:accList){
                Account objAcc = new Account(Id = acc.Id, Number_of_Contacts__c = acc.Contacts.size());
                updateAccList.add(objAcc);
            }
            if(!updateAccList.isEmpty()){
                update updateAccList;
            }
        }
    }
}

Check Profile Based Field Level Security Using Apex

For Specific Profile :

List<FieldPermissions> fpList = [SELECT SobjectType, Field, PermissionsRead, PermissionsEdit, Parent.ProfileId FROM FieldPermissions WHERE SobjectType = 'Account' and Field='Account.Customer_Priority__c' AND Parent.ProfileId IN (SELECT Id FROM PermissionSet WHERE PermissionSet.Profile.Name = 'System Administrator')];
if(!fpList.isEmpty()){
    Boolean hasReadPermission = fpList[0].PermissionsRead;
    Boolean hasEditPermission = fpList[0].PermissionsEdit;
    system.debug('Read Permission - ' + hasReadPermission);
    system.debug('Edit Permission - ' + hasEditPermission);
}

For Current User :

List<FieldPermissions> fpList = [SELECT SobjectType, Field, PermissionsRead, PermissionsEdit, Parent.ProfileId FROM FieldPermissions WHERE SobjectType = 'Account' and Field='Account.Customer_Priority__c' AND Parent.ProfileId=:Userinfo.getProfileId()];
if(!fpList.isEmpty()){
    Boolean hasReadPermission = fpList[0].PermissionsRead;
    Boolean hasEditPermission = fpList[0].PermissionsEdit;
    system.debug('Read Permission - ' + hasReadPermission);
    system.debug('Edit Permission - ' + hasEditPermission);
}

Populate Picklist from Custom Object to the Visualforce Page

Controller:

public with sharing class Sample
{
    public string selectedValue {get; set;}
    public List<SelectOption> industry {get; set;}
    
    public void getIndustry()
    {
        Schema.DescribeFieldResult industryDescription = Account.Industry.getDescribe();
        industry = new List<SelectOption>();
        
        for (Schema.Picklistentry pl : industryDescription.getPicklistValues())
        {
            industry.add(new SelectOption(pl.getValue(),pl.getLabel()));
        }
    }
    
    public void checkValue()
    {
        System.debug('Selected Industry Type - ' + selectedValue);
    }
}

Visualforce Page:

<apex:page controller="Sample" action="{!getIndustry}" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock>
            <apex:pageBlockSection columns="1" >
                <apex:outputLabel value="Industry Type" />
                <apex:selectList size="1" value="{!SelectedValue}" >
                    <apex:selectOptions value="{!industry}"/>
                    <apex:actionSupport event="onchange" action="{!checkValue}" />
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>

Output:

Calculate # of Months between a Start and End Date Formula in Salesforce

IF(
AND(
YEAR(End_Date__c) = YEAR(Begin_Date__c),
MONTH(End_Date__c) = MONTH(Begin_Date__c)),
1,
IF(ISBLANK(End_Date__c),
(YEAR(TODAY())*12+MONTH(TODAY())) –
(YEAR(Begin_Date__c)*12+MONTH(Begin_Date__c)),
(YEAR(End_Date__c)*12+MONTH(End_Date__c)) –
(YEAR(Begin_Date__c)*12+MONTH(Begin_Date__c))
)
)