I am trying to create a trigger that will fire only when the Probability % (Probability) is changed over 10% and update the contact roles (contact) with a date of this event happening (SQLDate__c). The trigger is currently firing on any change of the opportunity.
How can this be changed to fire only on the Probability % going above 10% (if it falls below I don't need any change to happen).
Thanks!!
trigger UpdateContact on Opportunity (after insert, after update) {
OpportunityContactRole ocr;
Contact contact;
Opportunity opp = Trigger.new[0];
list<Contact> listToUpdate = new list<Contact>();
if(opp.Probability > 10)
{
for(OpportunityContactRole iterating_oppConRole : [SELECT o.Role,
o.OpportunityId,
o.IsPrimary,
o.Id,
o.ContactId,
o.Contact.SQLDate__c
FROM OpportunityContactRole o where o.OpportunityId =: opp.id])
{
Contact tempContact = new Contact(id=iterating_oppConRole.ContactId, SQLDate__c = system.today());
listToUpdate.add(tempContact);
}
}
if(!listToUpdate.isEmpty())
update listToUpdate;
}
before update
,after update
, etc.). What developers should do is limiting unnecessary code blocks execution by checking field values where necessary(as you currently doing). Importantly make sure your trigger is bulk enabled to process multiple records at a time. As at now it's only processing one record.