1

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;
}
  • Triggers are meant to fire every time for a defined context(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. – highfive May 1 '18 at 6:35
3

More filtering is possible because both the old and new value of fields are available in the Trigger Context. So you should use code like this to minimise the updates:

Set<Id> oppIds = new Set<Id>();
for (Opportunity opp : Trigger.new) {
    Opportunity old = Trigger.oldMap.get(opp.Id);
    if (opp.Probability != old.Probability) {
        if (opp.Probability > 10) {
            oppIds.add(opp.Id);
        }
    }
}
if (oppIds.size() > 0) {
    // Use a Map to avoid duplicates
    Map<Id, Contact> updates = new Map<Id, Contact>();
    for(OpportunityContactRole ocr : [
            select ContactId
            from OpportunityContactRole
            where o.OpportunityId in :oppIds
            and ContactId != null
            } {
        updates.put(ocr.ContactId, new Contact(
                Id = ocr.Contact,
                SQLDate__c = Date.today()
                ));
    }
    update updates.values();
}

This code is bulkified too.

2

You're doing as much as you can currently. There is no more granular firing mechanism than before/after update/insert. All you can do is filter out the records which don't match your criteria, as you are in your code. Be aware though, as previously mentioned, currently your code is not properly bulkified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.