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
AmitZAmitZ 

Trigger to update only Contact Role with NULL date

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;
}
Best Answer chosen by AmitZ
Raj2019ssRaj2019ss
Hi
Try the below one, Its working fine for me.
trigger UpdateContact on Opportunity (after insert, after update) 
{

        OpportunityContactRole ocr;
        Contact contact;
        List<Contact> lstCont = new List<Contact>(); 
        Set<Id> oppIds =new Set<Id>();
    	Set<Id> AccIds =new Set<Id>();
        list<Contact> listToUpdate = new list<Contact>();
    	Map<Id, Contact> newMap = new Map<Id, Contact>();
        for (Opportunity opp : Trigger.new)
        {
          if(opp.Probability >= 10 && opp.Probability !=trigger.oldMap.get(opp.id).Probability) 
          {
            oppIds.add(opp.id);
            AccIds.add(opp.AccountId);
          }
        }
    	
        list<OpportunityContactRole> iterating_oppConRoles = [SELECT o.Role,
                                                                                                                   o.OpportunityId,  o.IsPrimary, o.Id, o.ContactId, o.Contact.SQLDate__c 
                                                                                                                   FROM OpportunityContactRole o 
                                                                                                                   WHERE o.OpportunityId IN :oppIds];
   for(OpportunityContactRole iterating_oppConRole : iterating_oppConRoles){                                                          
                for(Contact tempContact :[SELECT id, SQLDate__c FROM Contact WHERE AccountId IN : AccIds]){
                   if(tempContact.SQLDate__c == null){
               		  tempContact.SQLDate__c = system.today();
                    }
                newMap.put(tempContact.Id,tempContact);
            }
              
        if(!newMap.isEmpty())
            update newMap.values();
       }
}
please try and let me know. if its helps you please mark it as solved and best answer

Regards,
Rajeshkumar
 

All Answers

Steven NsubugaSteven Nsubuga
This is my stab at your code.
trigger UpdateContact on Opportunity (after insert, after update) {

        OpportunityContactRole ocr;
        Contact contact;
        
        Set<Id> oppIds =new Set<Id>();
        list<Contact> listToUpdate = new list<Contact>();
        for (Opportunity opp = Trigger.new){

          if(opp.Probability >= 10 && opp.Probability !=trigger.oldMap.get(opp.id).Probability) {
            oppIds.add(opp.id);
          }
        }
        list<OpportunityContactRole> iterating_oppConRoles = [SELECT o.Role, 
                                                                      o.OpportunityId, 
                                                                      o.IsPrimary, 
                                                                      o.Id, 
                                                                      o.ContactId,
                                                                      o.Contact.SQLDate__c 
                                                              FROM OpportunityContactRole o where o.OpportunityId IN :oppIds]);


        
            for(OpportunityContactRole iterating_oppConRole : iterating_oppConRoles) {
                                                              
                Contact tempContact = new Contact(id=iterating_oppConRole.ContactId, SQLDate__c=((SQLDate__c == null)?  system.today() : SQLDate__c));
                
                listToUpdate.add(tempContact);
                
                
           }
        
       
        if(!listToUpdate.isEmpty())
            update listToUpdate;
}

 
AmitZAmitZ
Thanks for your time Steven!
Got this error and was not able to proceed even when trying to fix:
Error: Compile Error: Expecting ';' but was: ')' at line 8 column 43
Raj2019ssRaj2019ss
Hi AmitZ,
Remove = in Line no 7 and put : (Colon) like below,
 
for (Opportunity opp : Trigger.new){
Regards,
Rajeshkumar

 
AmitZAmitZ
Hi Raejeshkumar,
Thanks for your help. 
After some touching up, now getting this:
Error: Compile Error: Variable does not exist: SQLDate__c at line 26 column 99 which is pointing to the bolded text:
Contact tempContact = new Contact(id=iterating_oppConRole.ContactId, SQLDate__c=((SQLDate__c == null)? system.today() : SQLDate__c))

Thanks!
 
trigger UpdateContact on Opportunity (after insert, after update) 
{

        OpportunityContactRole ocr;
        Contact contact;
        
        Set<Id> oppIds =new Set<Id>();
        list<Contact> listToUpdate = new list<Contact>();
        for (Opportunity opp : Trigger.new)
        {
          if(opp.Probability >= 10 && opp.Probability !=trigger.oldMap.get(opp.id).Probability) 
          {
            oppIds.add(opp.id);
          }
        }
        list<OpportunityContactRole> iterating_oppConRoles = [SELECT o.Role,
                                                                     o.OpportunityId, 
                                                                     o.IsPrimary, 
                                                                     o.Id, 
                                                                     o.ContactId,
                                                                     o.Contact.SQLDate__c 
                                                              FROM OpportunityContactRole o where o.OpportunityId IN :oppIds];
    
            for(OpportunityContactRole iterating_oppConRole : iterating_oppConRoles) 
            {                                                           
                Contact tempContact = new Contact(id=iterating_oppConRole.ContactId, SQLDate__c=((SQLDate__c == null)?  system.today() : SQLDate__c));
                listToUpdate.add(tempContact);
            }
        
       
        if(!listToUpdate.isEmpty())
            update listToUpdate;
}


 
Raj2019ssRaj2019ss
Hi,
In which object you having SQLDate__c field?
AmitZAmitZ
The field SQLDate__C is of type DATE and is under the Contact object.

When I attempt to add Contact.SQLDate__C to the two fields below as such:

26                Contact tempContact = new Contact(id=iterating_oppConRole.ContactId, SQLDate__c=((Contact.SQLDate__c == null)?  system.today() : Contact.SQLDate__c));

The trigger saves yet I get an error when running it on my test environment:

Error:Apex trigger UpdateContact caused an unexpected exception, contact your administrator: UpdateContact: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateContact: line 26, column 1
Raj2019ssRaj2019ss
Hi,
Below line is cause an error(I have bolded that text). Because you are checking the field is Null. If it is null then how it will update.
((Contact.SQLDate__c == null)?  system.today() : Contact.SQLDate__c));

 
AmitZAmitZ
The business idea is to fill in the SQLDate__c field only if it is empty. If it is already filled in with a previous date, I would like to keep the previous date.
Should this be implemented differently?
Raj2019ssRaj2019ss
Hi Amitz,

use below code
for(OpportunityContactRole iterating_oppConRole : iterating_oppConRoles) 
            {                                                           
                Contact tempContact = new Contact();
                tempContact.id = iterating_oppConRole.ContactId;
                tempContact.SQLDate__c =((tempContact.SQLDate__c == null)?  system.today() : iterating_oppConRole.Contact.SQLDate__c);
                listToUpdate.add(tempContact);
            }
I hope it will not throw the error. If yes, let me know.

Is it useful means please select as solved and best answer.

Regards,
Rajeshkumar
 
AmitZAmitZ
Thanks for this attempt Rajeshkumar yet it seems that now it is overwriting the SQLDate__C date field in the contact even if it is already populated.
 
trigger UpdateContact on Opportunity (after insert, after update) 
{

        OpportunityContactRole ocr;
        Contact contact;
        
        Set<Id> oppIds =new Set<Id>();
        list<Contact> listToUpdate = new list<Contact>();
        for (Opportunity opp : Trigger.new)
        {
          if(opp.Probability >= 10 && opp.Probability !=trigger.oldMap.get(opp.id).Probability) 
          {
            oppIds.add(opp.id);
          }
        }
        list<OpportunityContactRole> iterating_oppConRoles = [SELECT o.Role,
                                                                     o.OpportunityId, 
                                                                     o.IsPrimary, 
                                                                     o.Id, 
                                                                     o.ContactId,
                                                                     o.Contact.SQLDate__c 
                                                              FROM OpportunityContactRole o where o.OpportunityId IN :oppIds];
    
            for(OpportunityContactRole iterating_oppConRole : iterating_oppConRoles) 
            {                                                          
                Contact tempContact = new Contact();
                tempContact.id = iterating_oppConRole.ContactId;
                tempContact.SQLDate__c =((tempContact.SQLDate__c == null)?  system.today() : iterating_oppConRole.Contact.SQLDate__c);
                listToUpdate.add(tempContact);
            }
              
        if(!listToUpdate.isEmpty())
            update listToUpdate;
}

 
Raj2019ssRaj2019ss
Hi,
 Because you are using  Contact tempContact = new Contact();. Its like a new record. So it taking every time taking as null. So it will update today date.
Can we try process builder for that above requirement?
Steven NsubugaSteven Nsubuga
Updated my code
trigger UpdateContact on Opportunity (after insert, after update) {

        OpportunityContactRole ocr;
        Contact contact;
        
        Set<Id> oppIds =new Set<Id>();
        list<Contact> listToUpdate = new list<Contact>();
        for (Opportunity opp : Trigger.new){

          if(opp.Probability >= 10 && opp.Probability !=trigger.oldMap.get(opp.id).Probability) {
            oppIds.add(opp.id);
          }
        }
        list<OpportunityContactRole> iterating_oppConRoles = [SELECT o.Role, 
                                                                      o.OpportunityId, 
                                                                      o.IsPrimary, 
                                                                      o.Id, 
                                                                      o.ContactId,
                                                                      o.Contact.SQLDate__c 
                                                              FROM OpportunityContactRole o where o.OpportunityId IN :oppIds]);


        
            for(OpportunityContactRole iterating_oppConRole : iterating_oppConRoles) {
                                                              
                Contact tempContact = new Contact(id=iterating_oppConRole.ContactId, SQLDate__c=((SQLDate__c == null)?  system.today() : SQLDate__c));
                
                listToUpdate.add(tempContact);
                
                
           }
        
       
        if(!listToUpdate.isEmpty())
            update listToUpdate;
}

 
Raj2019ssRaj2019ss
Hi
Try the below one, Its working fine for me.
trigger UpdateContact on Opportunity (after insert, after update) 
{

        OpportunityContactRole ocr;
        Contact contact;
        List<Contact> lstCont = new List<Contact>(); 
        Set<Id> oppIds =new Set<Id>();
    	Set<Id> AccIds =new Set<Id>();
        list<Contact> listToUpdate = new list<Contact>();
    	Map<Id, Contact> newMap = new Map<Id, Contact>();
        for (Opportunity opp : Trigger.new)
        {
          if(opp.Probability >= 10 && opp.Probability !=trigger.oldMap.get(opp.id).Probability) 
          {
            oppIds.add(opp.id);
            AccIds.add(opp.AccountId);
          }
        }
    	
        list<OpportunityContactRole> iterating_oppConRoles = [SELECT o.Role,
                                                                                                                   o.OpportunityId,  o.IsPrimary, o.Id, o.ContactId, o.Contact.SQLDate__c 
                                                                                                                   FROM OpportunityContactRole o 
                                                                                                                   WHERE o.OpportunityId IN :oppIds];
   for(OpportunityContactRole iterating_oppConRole : iterating_oppConRoles){                                                          
                for(Contact tempContact :[SELECT id, SQLDate__c FROM Contact WHERE AccountId IN : AccIds]){
                   if(tempContact.SQLDate__c == null){
               		  tempContact.SQLDate__c = system.today();
                    }
                newMap.put(tempContact.Id,tempContact);
            }
              
        if(!newMap.isEmpty())
            update newMap.values();
       }
}
please try and let me know. if its helps you please mark it as solved and best answer

Regards,
Rajeshkumar
 
This was selected as the best answer