You need to sign in to do that
Don't have an account?

How do I edit my trigger to only fire when criteria is met once?
I have written the below trigger which creates a related record when a checkbox is updated. The problem is, it is creating a new record everytime the record is edited since the criteria is being met. Can anyone advise how I can edit my trigger so that it only fires once. (ie.The first time the criteria is met) ?
Trigger
Trigger
Trigger copyEquipmentonsite on Unit_Placements_Sales__c(after insert, after update) { List<Services__c> sub=new List<Services__c>(); for(Unit_Placements_Sales__c u : Trigger.new) { if(u.Service_Month_is_Next_Month__c == TRUE) { Services__c s=new Services__c(); s.Equipment_Onsite__c=u.ID; s.Equipment_Type__c=u.Equipment__c; s.Machine_Specific_Notes__c=u.Machine_Specific_Notes__c; s.Product__c=u.The_Product__c; s.Dell_Tag_Number__c=u.Dell_Tag_Number__c; s.Serial_Number__c=u.Serial_Number__c; s.XD_Number__c=u.XD_Number__c; sub.add(s); } if(sub.size()>0) insert sub; } } Thank you
Two other things: since trigger.new is read-only, and you are updating the records in trigger.new, you will need to query for these records anew. Also, it would be best to move your DML statements to the outside of the for loop, to avoid hitting governor limits.
Putting it all together, you might have something like this:
Trigger copyEquipmentonsite on Unit_Placements_Sales__c(after insert, after update)
{
List<Services__c> sub=new List<Services__c>();
List<Unit_Placements_Sales__c> unitsToUpdate = new List<Unit_Placements_Sales__c>();
for(Unit_Placements_Sales__c u : [SELECT Id, Service_Month_is_Next_Month__c, New_Checkbox_Field__c, Equipment__c, Machine_Specific_Notes__c, The_Product__c, Dell_Tag_Number__c, Serial_Number__c, XD_Number__c FROM Unit_Placements__c WHERE Id IN :trigger.newMap.keySet()])
{
if(u.Service_Month_is_Next_Month__c == TRUE && u.New_Checkbox_Field__c == false)
{
Services__c s=new Services__c();
s.Equipment_Onsite__c=u.ID;
s.Equipment_Type__c=u.Equipment__c;
s.Machine_Specific_Notes__c=u.Machine_Specific_Notes__c;
s.Product__c=u.The_Product__c;
s.Dell_Tag_Number__c=u.Dell_Tag_Number__c;
s.Serial_Number__c=u.Serial_Number__c;
s.XD_Number__c=u.XD_Number__c;
sub.add(s);
u.New_Checkbox_Field__c = true;
unitsToUpdate.add(u);
}
}
if(sub.size()>0)
insert sub;
if (unitsToUpdate.size() > 0)
update unitsToUpdate;
}
All Answers
for (Unit_Placements_Sales__c u : Trigger.new) {
if (u.Service_Month_is_Next_Month__c == true && trigger.oldMap.get(u.Id).Service_Month_is_Next_Month__c == false) {
}
}
You could also create a checkbox field that this trigger would check on the Unit_Placements_Sales__c object that indicates that the trigger has already run.
This might help
Thank you
Thank you for the quick reply. I like the sound of the second option. Just so I am understanding this correctly- Is the idea to add a line in the trigger that sets this new checkbox to true after the trigger has run and then include this in the trigger criteria.
For example If (u.Service_Month_is_Next_Month__c == true && u.new_checkbox_field__c ==true)
Two other things: since trigger.new is read-only, and you are updating the records in trigger.new, you will need to query for these records anew. Also, it would be best to move your DML statements to the outside of the for loop, to avoid hitting governor limits.
Putting it all together, you might have something like this:
Trigger copyEquipmentonsite on Unit_Placements_Sales__c(after insert, after update)
{
List<Services__c> sub=new List<Services__c>();
List<Unit_Placements_Sales__c> unitsToUpdate = new List<Unit_Placements_Sales__c>();
for(Unit_Placements_Sales__c u : [SELECT Id, Service_Month_is_Next_Month__c, New_Checkbox_Field__c, Equipment__c, Machine_Specific_Notes__c, The_Product__c, Dell_Tag_Number__c, Serial_Number__c, XD_Number__c FROM Unit_Placements__c WHERE Id IN :trigger.newMap.keySet()])
{
if(u.Service_Month_is_Next_Month__c == TRUE && u.New_Checkbox_Field__c == false)
{
Services__c s=new Services__c();
s.Equipment_Onsite__c=u.ID;
s.Equipment_Type__c=u.Equipment__c;
s.Machine_Specific_Notes__c=u.Machine_Specific_Notes__c;
s.Product__c=u.The_Product__c;
s.Dell_Tag_Number__c=u.Dell_Tag_Number__c;
s.Serial_Number__c=u.Serial_Number__c;
s.XD_Number__c=u.XD_Number__c;
sub.add(s);
u.New_Checkbox_Field__c = true;
unitsToUpdate.add(u);
}
}
if(sub.size()>0)
insert sub;
if (unitsToUpdate.size() > 0)
update unitsToUpdate;
}