You need to sign in to do that
Don't have an account?
Heather_Hanson
trigger to populate contact lookup field in opportunity
I'm new to Apex and am have trouble gathering the info I need to properly build my trigger.
We create our contacts from an account and we also create our opportunities from the account. So account name is a common factor between contact and opportunity.
I need to automatically populate a contact lookup field in the opportunity. This field is called Signing_Contact__c. In the Contacts object, we have a field called Contact_Role__c which is a picklist which includes the value "SIGNING CONTACT".
Basically, what I need to happen upon creation of an Opportunity is for it to query the Contacts object, and match up the associated Account IDs and if that is successful, it needs to query the field Contact_Role__c of associated records and locate the one that has "SIGNING CONTACT" as the value. It then needs to take the Contact.Name and populate the Opportunity.Signing_Contact__c field.
If more than one record with "SIGNING CONTACT" or none at all, the trigger can stop.
I'm really not sure how to put it all together. I think it is a bit complicated for my first try at triggers. I'm not even sure if this is possible. This is what I have so far...help would be greatly appreciated!!
We create our contacts from an account and we also create our opportunities from the account. So account name is a common factor between contact and opportunity.
I need to automatically populate a contact lookup field in the opportunity. This field is called Signing_Contact__c. In the Contacts object, we have a field called Contact_Role__c which is a picklist which includes the value "SIGNING CONTACT".
Basically, what I need to happen upon creation of an Opportunity is for it to query the Contacts object, and match up the associated Account IDs and if that is successful, it needs to query the field Contact_Role__c of associated records and locate the one that has "SIGNING CONTACT" as the value. It then needs to take the Contact.Name and populate the Opportunity.Signing_Contact__c field.
If more than one record with "SIGNING CONTACT" or none at all, the trigger can stop.
I'm really not sure how to put it all together. I think it is a bit complicated for my first try at triggers. I'm not even sure if this is possible. This is what I have so far...help would be greatly appreciated!!
Trigger OppSigningContact (before insert, before update) { for (Opportunity o: trigger.new) { if(o.Signing_Contact__c = null) List<Account> a = [SELECT Id, Name FROM Account WHERE Id = o.AccountId];
All Answers
Try below trigger code