Tuesday, June 19, 2018

Rollup summary trigger for sum in salesforce

Trigger contactSumTrigger on Contact(After insert,After Delete,After Undelete,After update)
{
    Set<Id> setAccountIds = new Set<Id>();   
   
    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate)
    {
        for(Contact con : Trigger.new)
        {
            setAccountIds.add(con.AccountId);
        }
    }
   
    if(Trigger.isDelete)
    {
       
        for(Contact con : Trigger.old)
        {
            setAccountIds.add(con.AccountId);
        }
    }
    set<Account> lstAcc = new set<Account>();
    System.debug(setAccountIds);
    List<AggregateResult> lstCon = [select accountid,sum(Amount__c) am from contact where accountid in : setAccountIds group by accountid];
   
    for(AggregateResult ar : lstCon){
        for(Contact c:trigger.new){
            if(c.accountid == ar.get('accountid')){
                account a = new Account(id = c.accountid);
                a.Total_Amount__c = Decimal.valueOf(String.valueOf(ar.get('am')));
                lstAcc.add(a);
            }
        }
    }
}

Update the contact record with Opportunity record

trigger contactupdate on Opportunity (after insert,after update) { 
    List<Contact> lstConToUpdate = new List<Contact>();
    Map<id,string> accids = new Map<id,string>();
    for(Opportunity opp : Trigger.new)
    {
        accids.put(opp.AccountId,opp.Phone__c);
    }
    List<Contact> listCon =[select id,name,AccountId,phone from contact where Accountid=:accids.keyset()];
    for(contact con: listCon)
    {
        if(accids.containsKey(con.AccountId))
        {
            con.Phone = accids.get(con.AccountId);
            lstConToUpdate.add(con);
        }
    }
    if(lstConToUpdate.size() > 0)
    {
        update lstConToUpdate;
    }
    
}

Insert or Update same records on two Objects when there is no relationship

Object 1 : Parent__c
 Fields   : Phone__c

Object 2   : Parent_Clone__c
Fields       : Parent_Id__c,Phone_Clone__c


Trigger on parent object:

trigger ParentClone on Parent__c (after insert, after update) {    
    ParentCloneHelper.dmlMethod(trigger.New,trigger.Newmap,trigger.Old,trigger.Oldmap,trigger.isBefore,trigger.isInsert,trigger.isAfter,trigger.isUpdate,trigger.isDelete);
    

}

Helper class : ParentCloneHelper


public class ParentCloneHelper {
    
    public static boolean controlveriable = true;
    
    public static void dmlMethod(list<Parent__c > triggerNew,map<Id,Parent__c > triggerNewMap,list<Parent__c> triggerOld,map<Id,Parent__c> triggerOldMap,boolean isBefore,boolean isInsert,boolean isAfter,boolean isUpdate,boolean isDelete)
    {
        
        if(isInsert || isupdate){
            if(controlveriable == true){
                set<ID> ParentIds = new set<ID>();
                for (Parent__c a : triggerNew) {
                    ParentIds.add(a.Id);
                }
                if (ParentIds.size() > 0) {
                    List<Parent_Clone__c> PCList = new list<Parent_Clone__c>();
                    Map<string,Id> Parentclonemap = New Map<string,Id>();        
                    for(Parent_Clone__c PC : [select Id,Name,Parent_Id__c,Phone_Clone__c from Parent_Clone__c]){
                        Parentclonemap.put(PC.Parent_Id__c,PC.Id);
                    }
                    for(Parent__c P: triggerNew){  
                        if(Parentclonemap.containsKey(P.Id)){
                            for(String ParentcloneId : Parentclonemap.keyset()){ 
                                if(P.Id==ParentcloneId){
                                    Parent_Clone__c PCR =  new Parent_Clone__c(Id = Parentclonemap.get(ParentcloneId));
                                    PCR.Name = P.Name;
                                    PCR.Phone_Clone__c = P.Phone__c;
                                    PCList.add(PCR);   
                                }
                            }
                        }
                        else{
                            Parent_Clone__c PCR =  new Parent_Clone__c();
                            PCR.Parent_Id__c = P.Id;
                            PCR.Name = P.Name;
                            PCR.Phone_Clone__c = P.Phone__c;
                            PCList.add(PCR);                           
                        }
                    }
                    controlveriable = false;
                    Upsert PCList;
                }
            }
        }
        if(isdelete){
            for(Parent__c Pdel:triggerOld){   
                Parent_Clone__c PCdel = [select id,Parent_Id__c from Parent_Clone__c where Parent_Id__c =:Pdel.Id];
                delete PCdel;                
            }
        }
    }
}

Trigger on parent clone object:

trigger Parent on Parent_Clone__c (after update) {
    
    Map<string,string> parentCloneMap = new Map<string,string>();
    for (Parent_Clone__c  PC : Trigger.new) {
        parentCloneMap.Put(PC.Parent_Id__c,PC.Id);
    }
     system.debug('parentCloneMap : '+parentCloneMap.keyset());
    if(parentCloneMap.size()>0){
        list<Parent__c> P = [select id,Phone__c from Parent__c where Id =:parentCloneMap.keyset()];
           system.debug('p : '+P);
        for (Parent_Clone__c  PC : Trigger.new) {
            for(Parent__c eachP :P){
                if(PC.Parent_Id__c == eachP.Id){
                    Parent__c updateP = new Parent__c(Id =PC.Parent_Id__c);
                    updateP.Phone__c = PC.Phone_Clone__c;
                    update updateP;
                }
            }
        }
    }    

}