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
Stuart HARRISONStuart HARRISON 

Set a flag if a contact is referenced in an opportunity

Our opportunities have links to, potentially, several contacts (and accounts) as per:
User-added image

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 ?
   }
Best Answer chosen by Stuart HARRISON
Niraj Kr SinghNiraj Kr Singh
Hi Stuart,

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:
 
trigger checkforopps on Contact (before update) {
	if(Trigger.isBefore && Trigger.isUpdate) {
		for (Contact a : [SELECT Id, GDPR_Contact_has_Opportunities__c, (SELECT Id, Name, Account_Contact__c FROM Opportunities) FROM Contact
					 WHERE Id IN (SELECT Account_Contact__c FROM Opportunity) AND
					 Id IN :Trigger.oldMap.KeySet()]) {
			//a.GDPR_Contact_has_Opportunities__c = true;
			Trigger.newMap.get(a.Id).GDPR_Contact_has_Opportunities__c = true;
		}
	}
}
And let me know if it works for you.

Thanks
Niraj

 

All Answers

devedeve
Trigger.Old is list of sobject here it is Contact and you arecomparing Id with this list
Stuart HARRISONStuart HARRISON
im very new to all this - i lifted the code from something "similar" and tried to amend it. are you suggesting i can take out the AND part ?
Niraj Kr SinghNiraj Kr Singh
Hi Stuart,

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:
 
trigger checkforopps on Contact (before update) {
	if(Trigger.isBefore && Trigger.isUpdate) {
		for (Contact a : [SELECT Id, GDPR_Contact_has_Opportunities__c, (SELECT Id, Name, Account_Contact__c FROM Opportunities) FROM Contact
					 WHERE Id IN (SELECT Account_Contact__c FROM Opportunity) AND
					 Id IN :Trigger.oldMap.KeySet()]) {
			//a.GDPR_Contact_has_Opportunities__c = true;
			Trigger.newMap.get(a.Id).GDPR_Contact_has_Opportunities__c = true;
		}
	}
}
And let me know if it works for you.

Thanks
Niraj

 
This was selected as the best answer
Stuart HARRISONStuart HARRISON
That works great thanks !!

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.
Stuart HARRISONStuart HARRISON
Nope - cant figure out how to set the flag to false before i start so that it gets re-evaluated every time. any clues ?
Niraj Kr SinghNiraj Kr Singh
Hi Stuart,

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.