• AmitZ
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 5
    Replies
Hello all,
I'm an admin trying to customize some code I found and it works - yet I got a comment that it is not bulkified.
The code looks at the probability of the opportunity - if it is 10% or over AND the field updated on the opportunity is the probability % field, the trigger should fire and update the date field SQLDate__c under each one of the contact ( Contact Roles).
I also would like it to update the SQLDate__c field only if it is null.

Can you suggest how to bulkify such code and if I used the correct expression (bolded) to make sure that the date will be updated only if the field is null?

Any suggestions to optimize the code are very welcome as well.

Thank you!


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 && opp.Probability !=trigger.oldMap.get(opp.id).Probability){

            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());
                if( tempContact.SQLDate__c == null){
                listToUpdate.add(tempContact);
                }
                
           }
        }
       
        if(!listToUpdate.isEmpty())
            update listToUpdate;
}
  • May 01, 2018
  • Like
  • 0
Hello all,
I'm an admin trying to customize some code I found and it works - yet I got a comment that it is not bulkified.
The code looks at the probability of the opportunity - if it is 10% or over AND the field updated on the opportunity is the probability % field, the trigger should fire and update the date field SQLDate__c under each one of the contact ( Contact Roles).
I also would like it to update the SQLDate__c field only if it is null.

Can you suggest how to bulkify such code and if I used the correct expression (bolded) to make sure that the date will be updated only if the field is null?

Any suggestions to optimize the code are very welcome as well.

Thank you!


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 && opp.Probability !=trigger.oldMap.get(opp.id).Probability){

            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());
                if( tempContact.SQLDate__c == null){
                listToUpdate.add(tempContact);
                }
                
           }
        }
       
        if(!listToUpdate.isEmpty())
            update listToUpdate;
}
  • May 01, 2018
  • Like
  • 0