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
Heather_HansonHeather_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!!
 
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];

 
Best Answer chosen by Heather_Hanson
Waqar Hussain SFWaqar Hussain SF
Trigger OppSigningContact (before insert, before update) { 

set<Id> AccountIds = new set<Id>();
for (Opportunity  o: trigger.new) {
	AccountIds.add(o.AccountId);
}


map<Id, Account> AccountContactMap = new map<Id, Account>();
list<Account> AccConts = new list<Account>();
AccConts = [Select Id, Name, (Select Id, Name, Contact_Role__c from Contacts Where Contact_Role__c = 'SIGNING CONTACT') from Account where Id IN : AccountIds];
for(Account acc : AccConts){
    AccountContactMap.put(acc.Id, acc);
}

for (Opportunity  o: trigger.new) {
       if(o.Signing_Contact__c == null){
			Account Acc = AccountContactMap.get(o.AccountId);
			if(Acc != null){
				List<Contact> conts = new list<Contact>(Acc.Contacts);
				if(conts.size() == 1){
					o.Signing_Contact__c = conts[0].Id;
				}
			}
		}
	}
}

 

All Answers

Waqar Hussain SFWaqar Hussain SF
Hi Heather,

Try below trigger code 
 
Trigger OppSigningContact (before insert, before update) { 

set<Id> AccountIds = new set<Id>();
for (Opportunity  o: trigger.new) {
	AccountIds.add(o.AccountId);
}

map<Id, Account> AccountContactMap = new map<Id, Account>();
AccountContactMap = [Select Id, Name, (Select Id, Name, Contact_Role__c from Contacts Where Contact_Role__c = 'SIGNING CONTACT') from Account where Id IN : AccountIds];

for (Opportunity  o: trigger.new) {
       if(o.Signing_Contact__c == null){
			Account Acc = AccountContactMap.get(o.AccountId);
			if(Acc != null){
				List<Contact> conts = new list<Contact>(Acc.Contacts);
				if(conts.size() == 1){
					o.Signing_Contact__c = conts[0].Id;
				}
			}
		}
	}
}

 
Heather_HansonHeather_Hanson
OK, I was missing "on Opportunity" in line 1, but added that and I'm getting error message "Illegal assignment from list to map" on line 9 and I can't figure out why because what you wrote all makes sense to me so is it the order that needs to be adjusted?
 
Trigger OppSigningContact on Opportunity (before insert, before update) {

set<Id> AccountIds = new set<Id>();
for (Opportunity  o: trigger.new) {
	AccountIds.add(o.AccountId);
}

map<Id, Account> AccountContactMap = new map<Id, Account>();
AccountContactMap = [Select Id, Name, (Select Id, Name, Contact_Role__c from Contacts Where Contact_Role__c = 'SIGNING CONTACT') from Account where Id IN : AccountIds];

for (Opportunity  o: trigger.new) {
       if(o.Signing_Contact__c == null){
			Account Acc = AccountContactMap.get(o.AccountId);
			if(Acc != null){
				List<Contact> conts = new list<Contact>(Acc.Contacts);
				if(conts.size() == 1){
					o.Signing_Contact__c = conts[0].Id;
				}
			}
		}
	}
}

 
Waqar Hussain SFWaqar Hussain SF
Trigger OppSigningContact (before insert, before update) { 

set<Id> AccountIds = new set<Id>();
for (Opportunity  o: trigger.new) {
	AccountIds.add(o.AccountId);
}


map<Id, Account> AccountContactMap = new map<Id, Account>();
list<Account> AccConts = new list<Account>();
AccConts = [Select Id, Name, (Select Id, Name, Contact_Role__c from Contacts Where Contact_Role__c = 'SIGNING CONTACT') from Account where Id IN : AccountIds];
for(Account acc : AccConts){
    AccountContactMap.put(acc.Id, acc);
}

for (Opportunity  o: trigger.new) {
       if(o.Signing_Contact__c == null){
			Account Acc = AccountContactMap.get(o.AccountId);
			if(Acc != null){
				List<Contact> conts = new list<Contact>(Acc.Contacts);
				if(conts.size() == 1){
					o.Signing_Contact__c = conts[0].Id;
				}
			}
		}
	}
}

 
This was selected as the best answer
Heather_HansonHeather_Hanson
That worked!  Thank you SO much!!  I have been trying to figure out how to make that happen for a few weeks now!