You need to sign in to do that
Don't have an account?
XIO
Apex Trigger - Opportunity Rollup Summary
Hello Developers!
I have been working on a trigger that needs to count the number of related "conference" Opportunities that are associated with a Product voucher. This allows tracking of Product vouchers of a particular conference. Basically I need to count the number of Opportunities (ConferenceOppty = TRUE) that are associated with the Product (Product2).
The rollup count field is in the Product object and it's called "Related_Conference_Opportunities__c."
Any assistance will be greatly appreciated!
I have been working on a trigger that needs to count the number of related "conference" Opportunities that are associated with a Product voucher. This allows tracking of Product vouchers of a particular conference. Basically I need to count the number of Opportunities (ConferenceOppty = TRUE) that are associated with the Product (Product2).
The rollup count field is in the Product object and it's called "Related_Conference_Opportunities__c."
Any assistance will be greatly appreciated!
trigger RelatedVoucherOpportunity on Product2 (after insert, after update, after delete, after undelete) { Set<Id> oppIds = new Set<Id>(); Map<Id,Opportunity> oppRecords = new Map<Id,Opportunity>(); for(Product2 p2:Trigger.isDelete?Trigger.old:Trigger.new) oppIds.add(p2.oppIds); oppIds.remove(null); for(Id oppId:oppIds) oppRecords.put(oppId,new Opportunity(Id=oppId,ConferenceOppty__c = TRUE)); for(Product2 c:[select id,Related_Conference_Opportunities__c from Product2 where id in :oppIds]) oppRecords.get(p2.oppIds).ConferenceOppty__c += p2.Related_Conference_Opportunities__c; Database.update(oppRecords.values()); }
There is a slight change in the code use the below code which is more ideal for your req including the delete condition
Try this and let me know if it works for the delete condition also.
Regards
All Answers
I need to know the relationship field name in the opportuntiy object for writing trigger. Also you need to write the trigger on child that is opportunity and update the parent ie product2.
Regards
The relationship field name is called Discount_code_LU__c
thank you!!
Try the below trigger.
Here replace the Child_Relationship_Name__r with the corresponding child relationship name in your opportuntiy look up field(Discount_code_LU__c).
Let me know if you have any issues.
PS: If you are interested you can look into a library called LREngine (https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries) which helps to find roll up using look up fields
Regards
I thought the child relationship name is Discount_code__LU__c? Which is a lookup field to the Product from the Opportunity.
Here you can find it as pobs. Take whats there in your field and add __r
Regards
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ContactRoleCountOpportunity caused an unexpected exception, contact your administrator: ContactRoleCountOpportunity: execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.ConferenceOppty__c: Trigger.ContactRoleCountOpportunity: line 21, column 1
Also, ConferenceOppty__c doesn't have to be included. I want to count all related Opportunities not just the ones that are ConferenceOppty__c + TRUE
If you want to count all opps remove that if condition in the line 21.
There is a slight change in the code use the below code which is more ideal for your req including the delete condition
Try this and let me know if it works for the delete condition also.
Regards