+ Start a Discussion
sfdc dev 2264sfdc dev 2264 

Campaign object roll up summary help n eeded

Hi ,

I need help on the following requirement as follows

1) I have a campaign member object associated to a campaign , I have a custom field called as "No of calls" 
2) I have to get the count of all the no of calls for all campiagn members and show that count in the campaign associated to it


can it be achieved using a roll up summary field
Kindly help me on this regard

Thanks in Advance,
Best Answer chosen by sfdc dev 2264
buggs sfdcbuggs sfdc
Hello 

Try the below code and let me know if you have any issues.
 
trigger totalnoofcalls on CampaignMember (after insert,after update,after delete,after undelete) {
 set<id> setcm = new set<id>();
    if(trigger.isinsert || trigger.isUpdate || trigger.isundelete){
    for(CampaignMember cm : trigger.new){
      setcm.add(cm.Campaignid);
    }
  }
  
  if(trigger.isdelete){
    for(CampaignMember cm : trigger.old){
      setcm.add(cm.Campaignid);
    }
  }
  list<campaign> lstcamp = [select id,total_no_of_columns__c from campaign where id in : setcm];
  List<campaign> updatecamp = new List<campaign>();
  
  for(AggregateResult mem :[select SUM(No_of_calls__c)sumtc from CampaignMember where campaignid in : setcm]){
  for(Campaign cm : lstcamp){
  cm.total_no_of_columns__c = (Decimal)mem.get('sumtc');
  system.debug('@@@@@@@@@@@@@@@@@@@@@' +cm.total_no_of_columns__c);
  updatecamp.add(cm);
  }}
  if(updatecamp.size()>0)
  update updatecamp;
}

if everything went well mark it has correct answer!it may help others

All Answers

Pavit SiddhuPavit Siddhu

Hello, You can achieve this by
slect sum function and in Field to Aggregate select "No of calls"

If helpful Plz rply
 

buggs sfdcbuggs sfdc
Hello 

Try the below code and let me know if you have any issues.
 
trigger totalnoofcalls on CampaignMember (after insert,after update,after delete,after undelete) {
 set<id> setcm = new set<id>();
    if(trigger.isinsert || trigger.isUpdate || trigger.isundelete){
    for(CampaignMember cm : trigger.new){
      setcm.add(cm.Campaignid);
    }
  }
  
  if(trigger.isdelete){
    for(CampaignMember cm : trigger.old){
      setcm.add(cm.Campaignid);
    }
  }
  list<campaign> lstcamp = [select id,total_no_of_columns__c from campaign where id in : setcm];
  List<campaign> updatecamp = new List<campaign>();
  
  for(AggregateResult mem :[select SUM(No_of_calls__c)sumtc from CampaignMember where campaignid in : setcm]){
  for(Campaign cm : lstcamp){
  cm.total_no_of_columns__c = (Decimal)mem.get('sumtc');
  system.debug('@@@@@@@@@@@@@@@@@@@@@' +cm.total_no_of_columns__c);
  updatecamp.add(cm);
  }}
  if(updatecamp.size()>0)
  update updatecamp;
}

if everything went well mark it has correct answer!it may help others
This was selected as the best answer
sfdc dev 2264sfdc dev 2264
thanks bro :)