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
HARSHIL U PARIKHHARSHIL U PARIKH 

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:
 
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!
Best Answer chosen by HARSHIL U PARIKH
Sathish balajiSathish balaji
Yep all the above issues are Valid. I could'nt test them as i dont have similar structure in my Dev account.I have rewritten the logic. Kindly test them and let me know. Seggregated the update and insert logic.
 
Trigger UniqueRollUp On Participation__c(Before Insert, Before Update){

    List<Participation__c> Partici = New List<Participation__c>();
    Set<ID> ProjIds = New Set<ID>();
    
    For(Participation__c P: Trigger.New)
    {    
          ProjIds.add(P.Volunteer_Project_Name__c);    
    }
      
   Partici = [Select id,Volunteer_Email__c,Volunteer_Project_Name__c  FROM Participation__c WHERE Volunteer_Project_Name__c in :ProjIds];
    
	If(trigger.isinsert){
		for(Participation__c pa1: Trigger.New){
			pa1.Unique_Roll_Up__c = 1;
			for(Participation__c pa2: Partici){
				If(pa1.Volunteer_Project_Name__c == pa2.Volunteer_Project_Name__c && pa1.Volunteer_Email__c == pa2.Volunteer_Email__c && pa1.Volunteer_Email__c != null){
					pa1.Unique_Roll_Up__c = 0;
					break;
				}
			}
		}
	}
	
	If(trigger.isupdate){
		for(Participation__c pa1: Trigger.New){
			If(pa1.Volunteer_Email__c != Trigger.oldmap.get(pa1.id).Volunteer_Email__c){
				pa1.Unique_Roll_Up__c = 1;
				for(Participation__c pa2: Partici){
					if(pa1.Volunteer_Project_Name__c == pa2.Volunteer_Project_Name__c && pa1.Volunteer_Email__c == pa2.Volunteer_Email__c)
						pa1.Unique_Roll_Up__c = 0;
						break;
				}
			}
		}
	}
    
}

 

All Answers

Jay Parikh 36Jay Parikh 36
Hii -- There is a free application call rollup helper in appexchange where you can create rollup fields on lookup relationship....

Here is a link :::https://appexchange.salesforce.com/listingDetail?listingId=a0N30000009i3UpEAI
Abhishek M.Abhishek M.
Hi Govind,
Change your If condition in Integer For loop, as follows,
if(Pa.Volunteer_Email__c == Partici[i].Volunteer_Email__c)
Sathish balajiSathish balaji
Hi Govind,

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.
Trigger UniqueRollUp On Participation__c(Before Insert, Before Update){

    List<Participation__c> Partici = New List<Participation__c>();
    Set<ID> ProjIds = New Set<ID>();
    
    For(Participation__c P: Trigger.New)
    {    
          ProjIds.add(P.Volunteer_Project_Name__c);    
    }
      
   Partici = [Select Volunteer_Email__c,Volunteer_Project_Name__c  FROM Participation__c WHERE Volunteer_Project_Name__c in :ProjIds];
    
    For(Participation__c Pa: Trigger.New){
        If(Pa.Volunteer_Email__c != null){
            
            for(Integer I = 0; I<Partici.size(); I++){
                  if(Pa.Volunteer_Project_Name__c == Partici[i].Volunteer_Project_Name__c ){
                       if(Pa.Volunteer_Email__c == Partici[i].Volunteer_Email__c){
                            Pa.Unique_Roll_Up__c = 0;
                            break;
                       }
                       else{
                           Pa.Unique_Roll_Up__c = 1;
                       }
                   }   
            }
        }
        else{
            Pa.Unique_Roll_Up__c = 1;
        }
    }
}
Best Regards,
Sathish Balaji
HARSHIL U PARIKHHARSHIL U PARIKH
Thank you for the help guys I really appreciate it.

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!
Sathish balajiSathish balaji
Hi Govind,

Added a few logic below to include the negative criteria as well. Kindly check and let us know the result.
Trigger UniqueRollUp On Participation__c(Before Insert, Before Update){

    List<Participation__c> Partici = New List<Participation__c>();
    Set<ID> ProjIds = New Set<ID>();
    
    For(Participation__c P: Trigger.New)
    {    
          ProjIds.add(P.Volunteer_Project_Name__c);    
    }
      
   Partici = [Select id,Volunteer_Email__c,Volunteer_Project_Name__c  FROM Participation__c WHERE Volunteer_Project_Name__c in :ProjIds];
    
    For(Participation__c Pa: Trigger.New){
        If(Pa.Volunteer_Email__c != null){
            
            for(Integer I = 0; I<Partici.size(); I++){
                  if(Pa.Volunteer_Project_Name__c == Partici[i].Volunteer_Project_Name__c ){
                       if(Pa.Volunteer_Email__c == Partici[i].Volunteer_Email__c){
                           if(trigger.isinsert){
                               Pa.Unique_Roll_Up__c = 0;
                               break;
                           }
                           else{
                                if(pa.id != Partici[i].id && Pa.Unique_Roll_Up__c = 1 && trigger.oldmap.get(pa.id).Volunteer_Email__c != pa.Volunteer_Email__c ){
                                    Pa.Unique_Roll_Up__c = 0;
                                    break;
                                }
                           }
                       }
                       else{
                           Pa.Unique_Roll_Up__c = 1;
                       }
                   }   
            }
        }
        else{
            Pa.Unique_Roll_Up__c = 1;
        }
    }
}
Best regards,
Sathish Balaji
 
HARSHIL U PARIKHHARSHIL U PARIKH
Sathish,
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
Sathish balajiSathish balaji
Oops, that's a blunder. Just replace the '=' with '==' in the if statement at line 24
if(pa.id != Partici[i].id && Pa.Unique_Roll_Up__c == 1 && trigger.oldmap.get(pa.id).Volunteer_Email__c != pa.Volunteer_Email__c )
HARSHIL U PARIKHHARSHIL U PARIKH
Alright Thanks again for your help here Sathish,

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!!
Sathish balajiSathish balaji
Yep all the above issues are Valid. I could'nt test them as i dont have similar structure in my Dev account.I have rewritten the logic. Kindly test them and let me know. Seggregated the update and insert logic.
 
Trigger UniqueRollUp On Participation__c(Before Insert, Before Update){

    List<Participation__c> Partici = New List<Participation__c>();
    Set<ID> ProjIds = New Set<ID>();
    
    For(Participation__c P: Trigger.New)
    {    
          ProjIds.add(P.Volunteer_Project_Name__c);    
    }
      
   Partici = [Select id,Volunteer_Email__c,Volunteer_Project_Name__c  FROM Participation__c WHERE Volunteer_Project_Name__c in :ProjIds];
    
	If(trigger.isinsert){
		for(Participation__c pa1: Trigger.New){
			pa1.Unique_Roll_Up__c = 1;
			for(Participation__c pa2: Partici){
				If(pa1.Volunteer_Project_Name__c == pa2.Volunteer_Project_Name__c && pa1.Volunteer_Email__c == pa2.Volunteer_Email__c && pa1.Volunteer_Email__c != null){
					pa1.Unique_Roll_Up__c = 0;
					break;
				}
			}
		}
	}
	
	If(trigger.isupdate){
		for(Participation__c pa1: Trigger.New){
			If(pa1.Volunteer_Email__c != Trigger.oldmap.get(pa1.id).Volunteer_Email__c){
				pa1.Unique_Roll_Up__c = 1;
				for(Participation__c pa2: Partici){
					if(pa1.Volunteer_Project_Name__c == pa2.Volunteer_Project_Name__c && pa1.Volunteer_Email__c == pa2.Volunteer_Email__c)
						pa1.Unique_Roll_Up__c = 0;
						break;
				}
			}
		}
	}
    
}

 
This was selected as the best answer
HARSHIL U PARIKHHARSHIL U PARIKH
All good Sathish, Thank You very much. Appreciated!