+ Start a Discussion
SFDC@ErrorSFDC@Error 

Trigger Not Updating

Hi All

I have created a trigger for sum of child field and update in parent like roll up summary through look up relationship.When i am creating new child record its working,but i am updating extisting child record its not working ...
trigger TotalSalesTrigger on Sales__c (after insert, after delete, after undelete, after update) {
    set<Id> invIdSet = new set<Id>();
    
    if(trigger.isinsert || trigger.isUpdate || trigger.Isundelete){
        for(Sales__c invItem: Trigger.new){
            if(Trigger.isInsert || Trigger.isUndelete || (invItem.Sold_Price__c!= Trigger.oldMap.get(invItem.Id).Sold_Price__c|| invItem.Customer_Name__c!= Trigger.oldMap.get(invItem.Id).Customer_Name__c))
                invIdSet.add(invItem.Customer_Name__c);            
        }
    }    
    if(trigger.isUpdate || trigger.isDelete) {
        for(Sales__c invItem: Trigger.old){
            if(Trigger.isDelete || (invItem.Sold_Price__c!= Trigger.newMap.get(invItem.Id).Sold_Price__c|| invItem.Customer_Name__c!= Trigger.newMap.get(invItem.Id).Customer_Name__c))
                invIdSet.add(invItem.Customer_Name__c);
        }
    }
     List<Contact> invList= [Select id,name,Total_Sale__c,(Select id,Sold_Price__c from Sales__r) from Contact  Where ID IN: invIdSet];       
      
    for(Contact inv : invList){
        
        inv.Total_Sale__c= 0;
        for(Sales__c invItem : inv.Sales__r) {
            inv.Total_Sale__c+= invItem.Sold_Price__c ;
        }
    }
    update invList;

 
Mukesh_SfdcDevMukesh_SfdcDev
Hi,

You have developed good code.But in this code line no 6 and line no 12 there is the duplicate statement when the record will update.
maybe this duplication can create a problem for update the record.
Sol: remove the trigger.isUpdate one of line 4 or 10.because this will work same when the record will update.

Hope this will help you

Thanks Mukesh
Lalit Mistry 21Lalit Mistry 21
May be you can give it a try to Rollup Helper app on AppExchange to do your rollups
https://appexchange.salesforce.com/appxListingDetail?listingId=a0N30000009i3UpEAI
v varaprasadv varaprasad
Hi ,

Please check  once below sample code : 

 
Trigger :

trigger RollupSummaryTriggerOnAccountObj on contact(after insert, after update, after delete, after undelete) { 
    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
        SampleRollupSummary.rollupContacts(trigger.new);
    }
    else if (trigger.isAfter && trigger.isDelete) {
        SampleRollupSummary.rollupContacts(trigger.old);
    }
}
 
public class SampleRollupSummary {
   
    public static void rollupContacts(list<contact> lstOfconts){
        system.debug('==lstOfconts== : '+lstOfconts);
        set<id> accIds = new set<id>();
        list<account> updLstOfAccs = new list<account>();
        list<contact> lstCons = new list<contact>();
       
        for(contact con : lstOfconts){
            accIds.add(con.accountid);
        }
        system.debug('==accIds==:'+accIds);
        list<account> lstAccs = [select id,name,Total_Count__c, (select id from contacts) from account where id in : accIds];
       
        for(account acc : lstAccs){
            system.debug('==acc.contacts.size()=='+acc.contacts.size());
            acc.Total_Count__c = acc.contacts.size();
            updLstOfAccs.add(acc);
        }
        if(updLstOfAccs.size() > 0){
            update updLstOfAccs;
        }
       
       
    }
   
}

Hope it helps you.


Let me know in case of any other assistance.


Thanks
Varaprasad










 
v varaprasadv varaprasad
Hi Please check once below code for rollup summary : 
 
public class SampleRollupSummary {  
    
    public static void rollupContacts(list<Contact_Partner__c> lstOfconts){
        
        system.debug('==lstOfconts== : '+lstOfconts);
        
        set<id> accIds = new set<id>();
        
        list<Customer_Outlet__c> updLstOfAccs = new list<Customer_Outlet__c>();      
        
        public double  totalAmount = 0;
        
        for(Contact_Partner__c con : lstOfconts){
            
            accIds.add(con.Customer_Assigned__c);
            
        }
        
        system.debug('==accIds==:'+accIds);
        
        list<Customer_Outlet__c> lstAccs = [select id,Total_CP_Salary__c,(select id,Salary__c from Contact_Partner__r) from Customer_Outlet__c where id in : accIds];
        
        
        
        for(Customer_Outlet__c acc : lstAccs){
            
            
            
            for(Contact_Partner__c cpr : acc.Contact_Partner__r){
                
                totalAmount = totalAmount + cpr.Salary__c;
                
            }
            
            acc.Total_CP_Salary__c = totalAmount;
            
            updLstOfAccs.add(acc);
            
        }
        
        if(updLstOfAccs.size() > 0){
            
            update updLstOfAccs;
            
        }
        
        
        
        
        
    }
    
    
    
}

Thanks
Varaprasad​
SFDC@ErrorSFDC@Error
Hi Varaprasad​
How can i create multiple child object when parent create .Multiple child object will be created based on lookup related child 
trigger CreateProjecttLineItems on Project_Management__c (after insert) {
        List<SubProject__c> PItem = new List<SubProject__c>();
        Set<Id> setAccountId = new Set<Id>(); 
        Map<Id,Integer> mapAccountIdInvoiceCount = new Map<Id,Integer>(); 
        for (Project_Management__c PM : trigger.new) 
        {
            setAccountId.add(PM.Quote_Name__c); 
        for(Quote__c a : [Select Id,(Select Id,Product__c from Quotation_Line_Items__r) from Quote__c where id =: setAccountId])
        {
            mapAccountIdInvoiceCount.put(a.id,a.Quotation_Line_Items__r.size()); 
        for (Project_Management__c  PM: trigger.new) 
        {
            for(Integer i=0; i<mapAccountIdInvoiceCount.get(PM.Quote_Name__c);i++)
            {
                SubProject__c PBI = new SubProject__c();
                PBI.Project_Name__c= PM.id;
                
                PBI.Kick_Off_Date__c=PM.Kick_Off_Date__c;
                PBI.Name=PM.Account_Name__c;
                
                PItem.add(PBI);
            }
        }
        insert PItem;
    }

Its created number of child record but not able to copy all record .... 
SFDC@ErrorSFDC@Error
Hi Varaprasad​

I have facing this error when i am editing record .I am editing child record of contact.if one child then its working,if more than one child i am facing issue and its working in sandbox,but problem is coming from production.
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger TotalSalesTrigger caused an unexpected exception, contact your administrator: TotalSalesTrigger: execution of AfterUpdate caused by: System.NullPointerException: Argument cannot be null.: Class.SampleRollupSummary.rollupContacts: line 31, column 1

 
trigger TotalSalesTrigger on Sales__c (after insert, after update, after delete, after undelete) { 
    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
        SampleRollupSummary.rollupContacts(trigger.new);
    }
    else if (trigger.isAfter && trigger.isDelete) {
        SampleRollupSummary.rollupContacts(trigger.old);
    }
}
 
public class SampleRollupSummary {  
    
    public static void rollupContacts(list<Sales__c> lstOfconts){
        
        system.debug('==lstOfconts== : '+lstOfconts);
        
        set<id> accIds = new set<id>();
        
        list<Contact> updLstOfAccs = new list<Contact>();      
        
         double  totalAmount = 0;
        
        for(Sales__C con : lstOfconts){
            
            accIds.add(con.Customer_Name__c);
            
        }
        
        system.debug('==accIds==:'+accIds);
        
        list<Contact> lstAccs = [select id,Total_Sale__c,(select id,Sold_Price__c from Sales__r) from Contact where id in : accIds];
        
        
        
        for(Contact acc : lstAccs){
            
            
            
            for(Sales__c cpr : acc.Sales__r){
                
                totalAmount = totalAmount + cpr.Sold_Price__c ;
                
            }
            
            acc.Total_Sale__c= totalAmount;
            
            updLstOfAccs.add(acc);
            
        }
        
        if(updLstOfAccs.size() > 0){
            
            update updLstOfAccs;
            
        }
        
        
        
        
        
    }
    
    
    
}