You need to sign in to do that
Don't have an account?
mng0827
Primary Contact Trigger in Opportunity
I'm trying to create a trigger which updates the custom Contact__c lookup field on the Opportunity record. It uses the Contact record associated with the related Account record and selects the Contact record marked as isPrimary firsthen selects the Contact record with the earliest Created Date.
The trigger works on a custom object but when I do this in Opp, I get this error:
Error: Compile Error: Incompatible element type SOBJECT:Account for collection of Id at line 14 column 13
trigger updateOppContact on Opportunity (before insert, before update) {
//This Trigger updates the direct contact link/ Contact__c lookup field on the Opportunity record
//It uses the Contact record associated with the related Account record
//Selects the Contact record marked as isPrimary first
//Then selects the Contact record with the earliest Created Date
List <Id> AccountIds = new List <Id> ();
for (Integer i = 0; i < Trigger.new.size(); i++) {
{
AccountIds.add(Trigger.new[i].Account); // Add AccountIds to List
}
}
//Select the Contact that has the same Account, choose the primary first, then the first created
List <Contact> ContactList = new List <Contact> ([Select Id, AccountId, isPrimary__c from Contact where AccountId in :AccountIds ORDER BY isPrimary__c ASC, createdDate DESC]);
for(Contact con : ContactList) {
for (Integer i = 0; i < Trigger.new.size(); i++) {
if (con.AccountId == Trigger.new[i].Account) {
Trigger.new[i].Contact__c = con.Id;
}
}
}
}
The trigger works on a custom object but when I do this in Opp, I get this error:
Error: Compile Error: Incompatible element type SOBJECT:Account for collection of Id at line 14 column 13
trigger updateOppContact on Opportunity (before insert, before update) {
//This Trigger updates the direct contact link/ Contact__c lookup field on the Opportunity record
//It uses the Contact record associated with the related Account record
//Selects the Contact record marked as isPrimary first
//Then selects the Contact record with the earliest Created Date
List <Id> AccountIds = new List <Id> ();
for (Integer i = 0; i < Trigger.new.size(); i++) {
{
AccountIds.add(Trigger.new[i].Account); // Add AccountIds to List
}
}
//Select the Contact that has the same Account, choose the primary first, then the first created
List <Contact> ContactList = new List <Contact> ([Select Id, AccountId, isPrimary__c from Contact where AccountId in :AccountIds ORDER BY isPrimary__c ASC, createdDate DESC]);
for(Contact con : ContactList) {
for (Integer i = 0; i < Trigger.new.size(); i++) {
if (con.AccountId == Trigger.new[i].Account) {
Trigger.new[i].Contact__c = con.Id;
}
}
}
}
Try changing thi in your code:
-----» AccountIds.add(Trigger.new[i].Account);
for this:
-----» AccountIds.add(Trigger.new[i].AccountId);
The lookup field for Account on Opportunity is called Account but the api name is AccountId (check this link: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_opportunity.htm).
The same goes for Contact, which you are already doing here:
-----» if (con.AccountId == Trigger.new[i].AccountId) {
(...)
}
Hope I helped. Have a good one.
eclf
All Answers
Try changing thi in your code:
-----» AccountIds.add(Trigger.new[i].Account);
for this:
-----» AccountIds.add(Trigger.new[i].AccountId);
The lookup field for Account on Opportunity is called Account but the api name is AccountId (check this link: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_opportunity.htm).
The same goes for Contact, which you are already doing here:
-----» if (con.AccountId == Trigger.new[i].AccountId) {
(...)
}
Hope I helped. Have a good one.
eclf
Error: Compile Error: No such column 'isPrimary__c' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 20 column 54
Does that make sense? Is there a custom field I need to add?