You need to sign in to do that
Don't have an account?
AmitZ
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;
}
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;
}
Try the below one, Its working fine for me. please try and let me know. if its helps you please mark it as solved and best answer
Regards,
Rajeshkumar
All Answers
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
Remove = in Line no 7 and put : (Colon) like below,
Regards,
Rajeshkumar
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!
In which object you having SQLDate__c field?
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
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.
Should this be implemented differently?
use below code 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
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?
Try the below one, Its working fine for me. please try and let me know. if its helps you please mark it as solved and best answer
Regards,
Rajeshkumar