Author Archives: Biswajeet

About Biswajeet

Biswajeet is my Name, Success is my Aim and Challenge is my Game. Risk & Riding is my Passion and Hard Work is my Occupation. Love is my Friend, Perfection is my Habit and Smartness is my Style. Smiling is my Hobby, Politeness is my Policy and Confidence is my Power.

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;
            }
        }
    }
}