You need to sign in to do that
Don't have an account?
Why this trigger is not firing? Trigger: Counting Unique Email Addresses of Child Records.
Hello Developers!
I have a situation here in which trigger is not firing.
Master object: Volunteer_Project__c and Detail object: Participation__c
I want to count number of unique email addresses (Total_Associated_Volunteers__c) of Participation__c on Volunteer_Project__c.
Approach: Whenever there is a record entry in child for one particular master, I am compering that newly inserting record's email address to all of the child records of that master. If similar email found then Unique_Roll_Up__c on that detail record is 0 otherwise 1. At the end, I am putting rollup summary on Unique_Roll_Up__c at the Master object.
Trigger:
Thank You!
I have a situation here in which trigger is not firing.
Master object: Volunteer_Project__c and Detail object: Participation__c
I want to count number of unique email addresses (Total_Associated_Volunteers__c) of Participation__c on Volunteer_Project__c.
Approach: Whenever there is a record entry in child for one particular master, I am compering that newly inserting record's email address to all of the child records of that master. If similar email found then Unique_Roll_Up__c on that detail record is 0 otherwise 1. At the end, I am putting rollup summary on Unique_Roll_Up__c at the Master object.
Trigger:
Trigger UniqueRollUp On Participation__c(Before Insert, Before Update){ List<Participation__c> Partici = New List<Participation__c>(); Set<String> UniqueEmailSet = New Set<String>(); Set<ID> ProjIds = New Set<ID>(); If(Trigger.IsInsert || Trigger.IsUpdate) { For(Participation__c P: Trigger.New) { ProjIds.add(P.Volunteer_Project_Name__c); } } Partici = [Select Volunteer_Email__c FROM Participation__c WHERE Volunteer_Project_Name__c = :ProjIds]; For(Participation__c Pa: Trigger.New){ If(Pa.Volunteer_Email__c != null){ for(Integer I = 0; I<Partici.size(); I++){ if(Pa.Volunteer_Email__c == Partici[0].Volunteer_Email__c){ Pa.Unique_Roll_Up__c = 0; } } } else{ Pa.Unique_Roll_Up__c = 1; } } }
Thank You!
All Answers
Here is a link :::https://appexchange.salesforce.com/listingDetail?listingId=a0N30000009i3UpEAI
Change your If condition in Integer For loop, as follows,
if(Pa.Volunteer_Email__c == Partici[i].Volunteer_Email__c)
The above code snippet needs some alteration.Few things what I have noted is as follows.
1.The events defined for this trigger is only insert and update. So, the trigger will fire only during insret/update, thus this statement is not required (If(Trigger.IsInsert || Trigger.IsUpdate)).
2. The SOQL statement should use "IN" operator instead of "=" as it is a list of values and not a specific value.
3. In line no 21, the email is always checked with the first value in the "Partici" list as the specified index is "0". The integer "i" should be specified here.
4. When multiple insert or update happens, the above logic checks the email in all the participants belonging to all the projects so the uniqueness is checked among all the projects participants rather than checking only participants belonging to a certain project. Thus we may need to have an extra check to compare if the project is same.
Kindly use the below one and let me know if it helps. Best Regards,
Sathish Balaji
I have just changed Partici[0].Volunteer_Email__c to Partici[I].Volunteer_Email__c. I don't know why I was making that silly mistake. I also saw the break; indicator which is definitely make sense.
Now, any new participation record comes it either gets born with Unique_Roll_Up__c as 0 or 1. This is working fine. But however, if there are two records for 'John Doe' first of them has Unique_Roll_Up__c = 1 and second one has Unique_Roll_Up__c = 0 and if I just go and update the first record again then Unique_Roll_Up__c becomes 0 from 1. I need to stop this behaviour..
Thank you!
Added a few logic below to include the negative criteria as well. Kindly check and let us know the result. Best regards,
Sathish Balaji
Highly appreciated your input here but I am getting an error saving the code above...
Error: Compile Error: AND operator can only be applied to Boolean expressions at line 24 column 62
Here is the few troubles I am experiencing
1) When I enter the record-1 named 'John Smith' with John@gmail.com then for him Unique_Roll_Up__c is null intstead of 1.
2) When I enter another record-2 named 'John Smith' with John@gmail.com then for him Uniqe_Roll_Up__c is 0 which make sense if the first one has 1 instead of null
3) When I enter the record-3 named 'Joe Cruz' with 'Joe@gmail.com then for him Unique_Roll_Up__c is 1 - Which is fine!
4) When I enter the record -4 named 'Joe Cruz' with 'Joe@gmail.com then for him Unique_Roll_Up__c is 0 - Which is fine as well!
But however,
5) If I go ahead and update record-4 then Unique_Roll_Up__c turns 0 to 1 - Which is arong behavior since now I have record-3 and record-4 both with 1.
samething with record 2 and record 1 as well, when they are updated they all becomes Unique_Roll_Up__c = 1.
So the problem is that when record gets updated they should have appropriate Unique_Roll_Up__c whether it's 1 or 0 and when the first record gets enter the Unique_Roll_Up__c shouldn't be null it should be 1.
Thank You for the followup I really do appreciate it!!