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
chriseustacechriseustace 

Update AccountID on Oppty when ContactId is filled out

Hello all,

I am trying to update the accountid on an oppty record where the contact has been populated, but the account is not, however I'm getting the error:

Illegal assignment from List<Contact> to Id

 

Here is the code:

trigger Update_Acct_Opps on Opportunity (after insert, after update) {

    for(Opportunity opp : trigger.new) {

if(opp.AccountId == NULL && opp.Contact__c != NULL){
    List<Opportunity> opps = new List<Opportunity>();
    Opportunity oppty = new Opportunity();
    oppty.AccountId = [select AccountId from Contact where Id =:Trigger.new[0].Contact__c];
    opps.add(oppty);
    update opps;
    }

}


}

 

Any ideas?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Alok_NagarroAlok_Nagarro

Hi Chriseustace,

 

I think your code has some error, let's see.........

 

 

trigger Update_Acct_Opps on Opportunity (after insert, after update) {

    for(Opportunity opp : trigger.new) {

if(opp.AccountId == NULL && opp.Contact__c != NULL){
    List<Opportunity> opps = new List<Opportunity>();
    Opportunity oppty = new Opportunity();
    oppty.AccountId = [select AccountId from Contact where Id =:Trigger.new[0].Contact__c];
    opps.add(oppty);
    update opps;
    }

 

Here you r getting list from select query, and u r assigning it to Id.This is not posssible.

You should code somthing like..

 

trigger Update_Acct_Opps on Opportunity (after insert, after update) {

    for(Opportunity opp : trigger.new) {

if(opp.AccountId == NULL && opp.Contact__c != NULL){
    List<Opportunity> opps = new List<Opportunity>();
    Opportunity oppty = new Opportunity();

List<Contact> con=[select AccountId from Contact where Id =:Trigger.new[0].Contact__c];

oppty.AccountId=con[0].AccountId;

opps.add(oppty);
    update opps;
    }

 

I think ,it will work.