I picked up this trigger code yesterday and it works perfectly:
trigger OpportunityAutoUpdate on Opportunity (before insert)
{
// get the ids of all the accounts
Set<Id> accIds=new Set<Id>();
for (Opportunity opp : trigger.new)
{
accIds.add(opp.AccountId);
}
// retrieve the accounts and owners
List<Account> accs=[select id, OwnerId from Account where id in :accIds];
Map<Id, Account> accById=new Map<Id, Account>();
accById.putall(accs);
// iterate the opportunities, updating the opportunity owner to the account owner
for (Opportunity opp : trigger.new)
{
// any logic about when to do this - i.e. only if the current owner matches a
// particular id goes here
if (null!=opp.AccountId)
{
opp.OwnerId=accById.get(opp.AccountId).OwnerId;
} }
}
I can not get it into our production Org as we only have 73% coverage.
Any ideas for writing a test class to help get this in?
Update: I found a way to get it in, how do I change the code so it only works if the opportunity owner if a Picklist value is selected?