+ Start a Discussion
mng0827mng0827 

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;
            }
        } 
         
    }
   

}
Best Answer chosen by mng0827
eclfeclf
Hello, 

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

eclfeclf
Hello, 

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

This was selected as the best answer
mng0827mng0827
Thanks eclf!
Esti LeiserEsti Leiser
This code looks like exactly what I need. However when I try to save it i get:
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?