You need to sign in to do that
Don't have an account?
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:
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!
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!
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
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
Use this code and let me know if you have any issues.
Thanks,
pRAMODH.