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
SFDCDevQASFDCDevQA 

Opportunity Ownership to be Account Owner

So we have data miners that put in opportunities and they are "supposed to" change the ownership to the correct person.  But often this doesn't happen.  I could do multiple workflow rules to change the ownership to the correct person but they would require a lot of upkeep as turnover happens.  So I'm trying to create a trigger that would check to see if the Opportunity Owner does not equal the account owner and then make it equal the account owner.  This is what I have but it is throwing an error before I can even save.  Can someone please help?

 

trigger OpportunityOwnerUpdate on Opportunity (before insert,before update) {
//change the owner of an opportunity to be the owner of the account
// Declare the variable to hold the ID of the account owner
    ID MyOppId;
    Opportunity[] opp = Trigger.new;
    for (Opportunity o:opp)
    {
       if(o.Account!=null)
       {
           ID AccountOwnerId =[Select OwnerId from Account where Id=:o.Account__c].OwnerId;
           o.OwnerID = AccountOwnerId;
       }
   }  
}

 

Thanks,

Amanda

SFDCDevQASFDCDevQA

Not sure why it had __c  Current code trying is this:

trigger OpportunityOwnerUpdate on Opportunity (before insert,before update) {
//change the owner of an opportunity to be the owner of the account
// Declare the variable to hold the ID of the account owner
    ID MyOppId;
    Opportunity[] opp = Trigger.new;
    for (Opportunity o:opp)
    {
       if(o.Account!=null)
       {
           ID AccountOwnerId =[Select OwnerId from Account where Id=:o.Account].OwnerId;
           o.OwnerID = AccountOwnerId;
       }
   }  
}

 

Tells me Compile Error: Invalid bind expression type of SOBJECT:Account for column of type Id  when I try to save.

Jia HuJia Hu
try
ID AccountOwnerId =[Select OwnerId from Account where Id=:o.AccountId].OwnerId;

Jia HuJia Hu
use this

if(o.AccountId!=null)
{
ID AccountOwnerId =[Select OwnerId from Account where Id=:o.AccountId].OwnerId;
o.OwnerID = AccountOwnerId;
}
vishal@forcevishal@force

First thing, you have one soql query inside a for loop. It's a crime in salesforce. The moment there's a batch insert/update having 200 or more records, your code is dead.

 

 

trigger OpportunityOwnerUpdate on Opportunity (before insert,before update) {
// gather all the Account Id's in a set

   Set<Id> setAccountIds = new Set<Id>();

   for(Opportunity o : trigger.new)

   {
      setAccountIds.add(o.AccountId);

   }

  

   // store each Account Id with it's owner Id in a map, such that AccountId => Account.OwnerId

   Map<Id, Id> mapAccountToOwner = new Map<Id, Id>();

   for(Account a : [Select Id, OwnerId From Account Where Id IN : setAccountIds]

   {

      mapAccountToOwner.put(a.Id, a.OwnerId);
   }

   for (Opportunity o:opp)
    {
       if(o.AccountId !=null)
       {
           o.OwnerID = mapAccountToOwner.get(o.AccountId); // here you are avoiding the query
       }
   }  
}

SFDCDevQASFDCDevQA

Vishal,

I didn't think about them doing batch uploads so thanks.  I tried your code and kept getting errors regarding variables and tokens so I kept messing with it until it seems to be working.  I added an end parenthesis, a duplicate for(Opportunit o:opp) near the top and changed this for(Opportunity o : trigger.new) to Opportunity[] opp = Trigger.new;

It seems to be working now but if you could just give it a quick glance to make sure I didn't royally screw something up that would be great.  This is my first time trying to work with Sets and Maps.

 

Thanks,

Amanda

 

trigger OpportunityOwnerUpdate on Opportunity (before insert,before update) {
// gather all the Account Id's in a set

   Set<Id> setAccountIds = new Set<Id>();

   Opportunity[] opp = Trigger.new;
   for (Opportunity o:opp)

   {
      setAccountIds.add(o.AccountId);

   }

 

   // store each Account Id with it's owner Id in a map, such that AccountId => Account.OwnerId

   Map<Id, Id> mapAccountToOwner = new Map<Id, Id>();

   for(Account a : [Select Id, OwnerId From Account Where Id IN : setAccountIds])

   {

      mapAccountToOwner.put(a.Id, a.OwnerId);
   }
   for (Opportunity o:opp)
    {
       if(o.AccountId !=null)
       {
           o.OwnerID = mapAccountToOwner.get(o.AccountId); // here you are avoiding the query
       }
   }  
}

vishal@forcevishal@force

Hi,

 

Sorry for the errors you got. Actually I did not validate the code in IDE, just wrote it down here. Anyway glad that it works now. Yes, your code is perfect and will work fine for all cases.

 

 

Note : You can avoid using the list<opportunity> opp, by using trigger.new in its place. But nothing major.

ScottedtScottedt
Hi,

I am a newbie to doploying triggers into SFDC, this trigeer works perfectly in my sandbox but when I pulled it into my production org and went to deploy I get this error


OpportunityOwnerUpdate    Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
Deploy Error    Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required.

Any ideas to help me this working in my org??

Scott D