function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
navy11jain@hotmail.comnavy11jain@hotmail.com 

Count Campaign History Records on Person Account

I have to count Campaign history Record in custom field on person account. I don't know how we can achieve this.

Please Give me Your suggestions or any idea anybody have

 

Thanks in advance.

Vinita_SFDCVinita_SFDC

Hello,

 

You can use roll-up summary field. Refer: https://help.salesforce.com/HTViewHelpDoc?id=fields_about_roll_up_summary_fields.htm&language=en_US

 

OR

 

This can be done by two triggers. One on the child object (to cause a parent-level trigger), and a second to update the field.

 

trigger OnParent on Child__c (after insert, after update, after delete, after undelete) {
  Map<Id,Parent__c> parents = new Map<Id,Parent__c>();
  if(Trigger.new<>null)
    for(Child__c c:Trigger.new)
      if(c.ParentLookup__c<>null)
        parents.put(c.ParentLookup__c,new Parent__c(id=c.ParentLookup__c));
  if(Trigger.old<>null)
    for(Child__c c:Trigger.old)
      if(c.ParentLookup__c<>null)      
        parents.put(c.ParentLookup__c,new Parent__c(id=c.ParentLookup__c));
  update parents.values();
}


trigger Onchild on Parent__c (before insert, before update) {
  for(Parent__c p:Trigger.new)
    p.RollupCounter__c = 0;
  for(Child__c c:[select id,ParentLookup__c from Child__c where ParentLookup__c in :Trigger.new])
    Trigger.newMap.get(c.ParentLookup__c).RollupCounter__c++;
}

The child trigger is responsible for making sure that both the old parent and new parent (if there is change) has their values updated accordingly. The parent trigger actually performs the tallying.