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
JJJenkinsJJJenkins 

Issue with a basic trigger

So this is my first trigger going into production.  I've gone through the workbooks but I have come to a stop as I'm not sure where I'm going wrong.  With the trigger I'm trying to populate a lookup field on the opportunity with the current account owner that is associated to the opportunity.

 

Here is the code:

 

trigger AccountOwner on Opportunity (after update) {

//update opportunity with current account owner
List<Opportunity> AccountOwner = 
[SELECT Account.OwnerId, Acct_Owner__c FROM Opportunity FOR UPDATE];

    for (Opportunity o: AccountOwner){
        if(o.Acct_Owner__c <> Account.OwnerId){
        o.Acct_Owner__c=Account.OwnerId;
        }
    }
    update AccountOwner;
}

 

 

I'm getting a compile error at line 9 column 9 Illegal assignment from Schema.SObjectField to Id.

 

Suggestions/ help?  It doesn't have to be a look up field, it can be changed to text if that is the issue.

 

thanks,

Best Answer chosen by Admin (Salesforce Developers) 
wkuehlerwkuehler

There may be a simplier solution, but the below code should work for one or more opportunities being sent to the trigger:

 

 

trigger AccountOwner on Opportunity (before update)
{
    //Create a unique set of account ids that are related to opportunities in this trigger batch
    Set<Id> acct_ids = new set<Id>();
    for(Opportunity o : trigger.new)
    {
        acct_ids.add(o.accountid);
    }
    
    //Select the Id and OwnerId of all accounts from the unique set
    list<account> accounts = [select id, ownerid from account where id in :acct_ids];
    map<Id, Id> acct_owners = new map<Id, Id>();
    for(Account a : accounts)
    {
        acct_owners.put(a.Id, a.Ownerid);
    }
    
    //For each opportunity in this trigger batch, set the Acct_Owner__c field equal to the account owner field
    for (Opportunity o : trigger.new)
    {
        //Account a = [select Id, OwnerId from Account where Id = :o.accountid limit 1];
        if(o.Acct_Owner__c <> acct_owners.get(o.accountid))
        {
            o.Acct_Owner__c = acct_owners.get(o.accountid);
        }
    }
}

 

Hope this helps.

 

All Answers

vhanson222vhanson222

When comparing the Account_Owner__c to the Account.OwnerId, you need to specify that it's the Account.OwnerId of the Opportunity that you are currently referenceing (variable o).  See the code below, i hope that helps.

 

 

trigger AccountOwner on Opportunity (after update) {

//update opportunity with current account owner
List<Opportunity> AccountOwner = 
[SELECT Account.OwnerId, Acct_Owner__c FROM Opportunity FOR UPDATE];

    for (Opportunity o: AccountOwner){
        if(o.Acct_Owner__c <> o.Account.OwnerId){
        o.Acct_Owner__c=o.Account.OwnerId;
        }
    }
    update AccountOwner;
}

 

 

wkuehlerwkuehler

There may be a simplier solution, but the below code should work for one or more opportunities being sent to the trigger:

 

 

trigger AccountOwner on Opportunity (before update)
{
    //Create a unique set of account ids that are related to opportunities in this trigger batch
    Set<Id> acct_ids = new set<Id>();
    for(Opportunity o : trigger.new)
    {
        acct_ids.add(o.accountid);
    }
    
    //Select the Id and OwnerId of all accounts from the unique set
    list<account> accounts = [select id, ownerid from account where id in :acct_ids];
    map<Id, Id> acct_owners = new map<Id, Id>();
    for(Account a : accounts)
    {
        acct_owners.put(a.Id, a.Ownerid);
    }
    
    //For each opportunity in this trigger batch, set the Acct_Owner__c field equal to the account owner field
    for (Opportunity o : trigger.new)
    {
        //Account a = [select Id, OwnerId from Account where Id = :o.accountid limit 1];
        if(o.Acct_Owner__c <> acct_owners.get(o.accountid))
        {
            o.Acct_Owner__c = acct_owners.get(o.accountid);
        }
    }
}

 

Hope this helps.

 

This was selected as the best answer
JJJenkinsJJJenkins

Thanks! Now I need to walk through this to fully understand what I just did.  But it works!