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
Jonathan Osgood 3Jonathan Osgood 3 

Trouble updating roll up trigger with additional soql criteria

Hi All,

I have a simple trigger to roll up the count of child records (product__c) on the parent record (Person__c). Record count landed on a field on the person__c record called Total_Number_of_Products__c. Everything was working great, but I'm trying update the trigger to only roll up those records of a specific record type.

Here is my original working trigger code:
trigger PersonRollUpProducts on Product__c (after delete, after insert, after update) {
 
  //Limit the size of list by using Sets which do not contain duplicate elements
  
  set<id> PersonIds = new set<id>();
  
  List<Product__c> lstProducts = new List<Product__c>();
  
  //When adding new products or updating existing products
  if(trigger.isInsert || trigger.isUpdate){
    lstProducts = trigger.new;
    /*
    for(Product__c pr : trigger.new){
      PersonIds.add(pr.Person__c);
    }*/
  }
 
  //When deleting payments
  if(trigger.isDelete){
    lstProducts = trigger.old;
    /*
    for(Product__c pr : trigger.old){
      PersonIds.add(pr.Person__c);
    }*/
  }
  
  for(Product__c pr : lstProducts){
      PersonIds.add(pr.Person__c);
    }
 
  //Map will contain one Person Id to one sum value
  map<id, Double> PersonMap = new map<id, Double> ();
 
  //Produce a sum of Products__c and add them to the map
  //use group by to have a single Person__c Id with a single sum value
  for(AggregateResult q : [select Person__c,count(id)
    from Product__c where Person__c IN :PersonIds group by Person__c]){
      PersonMap.put((Id)q.get('Person__c'),(Double)q.get('expr0'));
  }
 
  List<Person__c> PersonsToUpdate = new List<Person__c>();
 
  //Run the for loop on person using the non-duplicate set of person Ids
  //Get the sum value from the map and create a list of persons to update
  for(Person__c pe : [Select Id, Total_Number_of_Products__c from Person__c where Id IN :PersonIds]){
    
    Double ProductSum = PersonMap.get(pe.Id);
    pe.Total_Number_of_Products__c = ProductSum;
    PersonsToUpdate.add(pe);
  }
 
  update PersonsToUpdate;
}

I added the following (bold, underlined) code to my SOQL query with no luck:

[select Person__c,count(id) from Product__c where RecordTypeId ='012d0000000SnGM' and Person__c IN :PersonIds group by Person__c]

What am I missing here?

Thanks!
 
Best Answer chosen by Jonathan Osgood 3
Alexander TsitsuraAlexander Tsitsura
Hi Jonathan,

I recomented you to avoid hardcoded id, use RecordType.DeveloperName = 'Some_RT' insted of recordTypeId. And all looks good, what not worked? Count not correctly calculate?

Thanks,
Alex

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi Jonathan,

I recomented you to avoid hardcoded id, use RecordType.DeveloperName = 'Some_RT' insted of recordTypeId. And all looks good, what not worked? Count not correctly calculate?

Thanks,
Alex
This was selected as the best answer
Pramodh KumarPramodh Kumar
Hi Jonathan,

Use this code and let me know if you have any issues.
trigger PersonRollUpProducts on Product__c (after insert, after update,after delete) {
    list<Person__c > personToupdate = new list<Person__c >();
    list<id> PersonIds = new list<id>();
    list<string> highestTRank;
    
    if(Trigger.isInsert){
        for(Product__c tca : Trigger.new){
            PersonIds.add(tca.Person__c);
        }
    }
    
    else if(Trigger.isUpdate || Trigger.isDelete){
        for(Product__c tca : Trigger.old){
            PersonIds.add(tca.Person__c);
        }
    }
    
    map<id, Person__c> personMap = new map<id,Person__c>([select id,Total_Number_of_Products__c from  Person__c where id IN: PersonIds]);
    
    for(Person__c ad : [select id,Total_Number_of_Products__c,(select id from Products__r) 
                           from  Person__c where id IN: PersonIds]){
        personMap.get(ad.Id).Total_Number_of_Products__c = ad.Products__r.size();
        personToupdate.add(personMap.get(ad.id));    
    }
    update personToupdate;
	
}

Thanks,
pRAMODH.
Jonathan Osgood 3Jonathan Osgood 3
That did it, thanks Alex!