You need to sign in to do that
Don't have an account?
Need to auto-populate Opportunity contact
Hello,
I am new to Apex so I would like assistance in creating a trigger to automatically update the Opportunity Contact field, with the contact designated as the Renewal Owner (Contact Type) in the contact description. We have added a field named Contact Type to the Contacts so that we know who to send renewal contracts to.
Field Name (field type): API Name:
Opportunity Contact (Lookup(Contact)): Opportunity_Contact__c
Contact type (Picklist): Contact_Type__c
The objective of the trigger is to eliminate the need to have to select the contact in the Opportunity Contact lookup field. This trigger should only fire once (when a new Opportunity is created) and not when someone updates the Opportunity. If someone wants to change the Opportunity contact to someone else, later on, they should be able to edit that field as well. Thanks for the help with this.
Try this
for (List<Contact> contactlist : [SELECT id FROM Contact WHERE AccountId = :opty.AccountId AND Contact_Type__c = 'Renewal Owner' LIMIT 1]) {
All Answers
Try something like this:
trigger setRenewalOwnerContact on Opportunity (before insert) {
List<Opportunity > optylist = Trigger.new;
for ( Opportunity opty : optylist ) {
if (opty.Opportunity_Contact__c == null) {
for (List<Contact> contactlist : [SELECT id FROM Contact WHERE AccountId = :opty.Account__c AND Contact_Type__c = 'Renewal Owner' LIMIT 1]) {
for(Contact con : contactlist ) {
opty.Opportunity_Contact__c = con.id;
}
}
}
}
}
Thanks for the help Eugene. I pasted your code and this is the message I received:
The Account object is just "Account" I believe. I changed it to this in line 8:
for (List<Contact> contactlist : [SELECT id FROM Contact WHERE AccountId = :opty.Account AND Contact_Type__c = 'Renewal Owner' LIMIT 1]) {
and I received this error message:
Error: Compile Error: Invalid bind expression type of SOBJECT:Account for column of type Id at line 8 column 81
I appreciate your help.
Try this:
edit: nvm this is wrong.
Here is the modified code, but I now receive the following error message:
Any suggestions?
I was able to save the trigger by making the modificaitons below (using "Account.Name" instead of "Account":
I created a new Opportunity and saved it, but it still isn't populating the Opportunity contact with the Contact that is tagged as the Renewal Owner. What is missing?
Try this
for (List<Contact> contactlist : [SELECT id FROM Contact WHERE AccountId = :opty.AccountId AND Contact_Type__c = 'Renewal Owner' LIMIT 1]) {
update opty;
this better not be wrong too :P
Hello Eugene,
I made the change you suggested, but still nothing. I was reviewing the code and I don't see, or understand, where we are instructing the trigger to populate the Opportunity_Contact__C field with the First and Last Name of the contact that is tagged as the Renewal Owner. Thanks again.
Regards,
Alex Bonilla
Hello Sdry,
Excuse my ignorance, but what should I update opty to?
"
Hello Sdry,
Excuse my ignorance, but what should I update opty to?"
Eugene's code was already updating the object, but not sending those changes back to salesforce database. Have a look at the code below, which is part of the trigger posted above.
You told us Opportunity_Contact__C is a Contact lookup, so we should not try to populate the firstname and lastname, but fill in the contact ID in the lookup relation field. Which is what Eugene's code does.
caevat: I've only started with salesforce and apex myself recently too, I could be wrong too.
Thanks for all your help Eugene! Your last suggestion did the trick. Have a great day!
Regards,
Alex Bonilla
Because the trigger is on the opportunity, you do not need to specifically update the opportunity. By establishing a join from your opportunity directly to your contact, the other contact fields will populate. However, as Opportunities connect to Accounts which connect to Contacts, what this code does is to search for a Renewal Owner associated with the account associated with the opportunity. If one doesn't exist, the opty contact will not populate.
I based my suggestion on a working trigger I wrote to pick "drop contacts" for a "site", not unlike what you are attempting to do.
Because the trigger is on the opportunity, you do not need to specifically update the opportunity
That is the case because it's a before trigger , and would only work in a before insert/update ? Hadn't thought of that. Glad to learn.