You need to sign in to do that
Don't have an account?
HARSHIL U PARIKH
Trigger works fine for individual record entry but when mass updated, all records gets updated with same value.
Hello Developers!
I need some help on this..
Two Objects with Master-Detail: 1) Volunteer_Project__c (Master) , 2) Participation__c (Detail)
Task: Need to count identical email address on parent for child.
Trigger:
When I individually add parent and then child for them it counts unique emails. All good as of now!
But when I Mass update all parent then they all gets updated with same value.
(e.g., Let's say 3 projects A, B, C and each has Total_Associated_Volunteers__c as 5, 10, 15.
But When I update all 3 project as Mass Update then all Projects has Total_Associated_Volunteers__c = 15)
Thank you for the support!
I need some help on this..
Two Objects with Master-Detail: 1) Volunteer_Project__c (Master) , 2) Participation__c (Detail)
Task: Need to count identical email address on parent for child.
Trigger:
Trigger UniqueEmailCount On Volunteer_Project__c(before insert, before update) { List<Participation__c> ParticipationList = new List<Participation__c>(); Set<String> uniqueEmails = new Set<String>(); ParticipationList = [SELECT Volunteer_Email__c FROM Participation__c where Volunteer_Project_Name__c IN :Trigger.New]; for (Integer i = 0; i< ParticipationList.size(); i++) {uniqueEmails.add(ParticipationList[i].Volunteer_Email__c); } for(Volunteer_Project__c proj: Trigger.new) { proj.Total_Associated_Volunteers__c = uniqueEmails.size(); } }Problem:
When I individually add parent and then child for them it counts unique emails. All good as of now!
But when I Mass update all parent then they all gets updated with same value.
(e.g., Let's say 3 projects A, B, C and each has Total_Associated_Volunteers__c as 5, 10, 15.
But When I update all 3 project as Mass Update then all Projects has Total_Associated_Volunteers__c = 15)
Thank you for the support!
All Answers
So here is the thing. You cannot have a before insert on the trigger. It does not make any sense, as it will not have children associated to the parent by then.
I have changed the looping logic of your base trigger for it to work for your needs.
If it helps, mark this as an answer! Thanks a lot!
I am try to execute and it says:
Error: Compile Error: Illegal assignment from Set<String> to Set<Participation__c> at line 14 column 35
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UniqueEmailCount caused an unexpected exception, contact your administrator: UniqueEmailCount: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Participation__c.Volunteer_Project_Name__c: Trigger.UniqueEmailCount: line 12, column 1
When I Enter new child record then below is the error shown:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UniqueEmailCount caused an unexpected exception, contact your administrator: UniqueEmailCount: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Participation__c.Volunteer_Project_Name__c: Trigger.UniqueEmailCount: line 12, column 1