You need to sign in to do that
Don't have an account?
Stuart HARRISON
Set a flag if a contact is referenced in an opportunity
Our opportunities have links to, potentially, several contacts (and accounts) as per:
Whenever a contact is updated I want to check to see if it is linked to any opportunities.
Ive been trying to create an Apex trigger to update a field on the contact if the contact is linked to any opportunities as the account contact, intermediary reinsurance contact local insurer contact etc.
Ive tried building a trigger just focusing on the Account contact to start with:
trigger checkforopps on Contact (after update) {
// Check if contact has related opportunities.
for (Contact a : [SELECT Id FROM Contact
WHERE Id IN (SELECT Account_Contact__c FROM Opportunity) AND
Id IN :Trigger.old]) {
a.GDPR_Contact_has_Opportunities__c = True;
}
it never seems to set the tickbox on. Am i missing somethig daft ?
}
Whenever a contact is updated I want to check to see if it is linked to any opportunities.
Ive been trying to create an Apex trigger to update a field on the contact if the contact is linked to any opportunities as the account contact, intermediary reinsurance contact local insurer contact etc.
Ive tried building a trigger just focusing on the Account contact to start with:
trigger checkforopps on Contact (after update) {
// Check if contact has related opportunities.
for (Contact a : [SELECT Id FROM Contact
WHERE Id IN (SELECT Account_Contact__c FROM Opportunity) AND
Id IN :Trigger.old]) {
a.GDPR_Contact_has_Opportunities__c = True;
}
it never seems to set the tickbox on. Am i missing somethig daft ?
}
Your flag is not updating with True, because your contact instance is not a trigger context varibale instance.
And one more thing, your trigger should be on "Before Update", so that you donot need dml operation and will avoid recursive as well.
Try this updated code:
And let me know if it works for you.
Thanks
Niraj
All Answers
Your flag is not updating with True, because your contact instance is not a trigger context varibale instance.
And one more thing, your trigger should be on "Before Update", so that you donot need dml operation and will avoid recursive as well.
Try this updated code:
And let me know if it works for you.
Thanks
Niraj
I just need to default it to off at the start so i can re-evaluate on every change. Can I update the "input record" before the "if" statement ? Many thanks.
Trigger will get executed while updating your record. By default that flag will be false at the time of record created. And will get true when you update the record.
If u want u can use filter criteria before updating true. According to ur requirement
Mark your answer above solution solve ur problem to help others.