Ask Search:
Ivo BoevIvo Boev 

Update Opp owner trigger help?!

I am trying to construct a trigger that does the following:

When Accounts are upserted with  the Data Loader, the trigger checks if the owners of opportunities are ALSO owners or sharing users of the accounts to which these opportunities are attached. If owners of opportunities are NOT the owners or sharing users of the accounts, THEN change owner of that opportunity to the owner of the account to which that opportunity pertains.
 
For this purpose, upon account update I will insert a SF share user ID (such as 005E00000021HAC)  into a custom field named SLS2__c, located in Account.
So basically, after account upsert, SFDC has to check:
  • Is the OwnerID of the opportunity equal to either of 1) Account Owner ID OR 2) SLS2__c
  • If NOT, then update owner of Opportunity TO owner of Account
 
A very kind person from force.com designed for me the following Trigger. However I cannot test it because there is an error and I do not know how to fix it.

Error: Compile Error: Loop variable must be of type SOBJECT:Account at line 6 column 17

This is the trigger:
 
 
Trigger updateOppOwner on Account (before insert, before update) {
 
List<Id> accIds = new List<Id>();
List<Account> accList = new List<Account>();
 
for(Opportunity opp : trigger.new){
 
accIds.add(opp.AccountId);
}
 
accList = [select SLS2__c, OwnerId from Account where id in:accIds];
 
for(Opportunity op : Trigger.new){
if(op.OwnerId!= accList[0]. OwnerId || op.OwnerId != accList[0]. SLS2__c){
op.OwnerId = accList[0].OwnerID;
 
}
 
}
}
 
Please help!
Best Answer chosen by Moderator (salesforce.com) 
Eugene NeimanEugene Neiman
Try this for size.

trigger updateOppOwner on Account (after insert, after update) {
  if (Trigger.isAfter) {
  Set<Id> acctIds = new Set<Id>();
    for (Account acct : Trigger.new) {
       acctIds.add(acct.Id);
       }    
 
    List<Opportunity> optyList = [SELECT OwnerId  FROM Opportunity WHERE AccountId in :acctIds];
    if ( !optyList.isEmpty() ) {
    for (Account acct : Trigger.new) {
        for(Opportunity opty : optyList ) {       
            if(opty.OwnerId != acct.OwnerId || opty.OwnerId != acct.SLS2__c){
               opty.OwnerId = acct.OwnerID;
               }
            }
        update optyList;
        }
    }
   }
}

All Answers

Eugene NeimanEugene Neiman
Try this for size.

trigger updateOppOwner on Account (after insert, after update) {
  if (Trigger.isAfter) {
  Set<Id> acctIds = new Set<Id>();
    for (Account acct : Trigger.new) {
       acctIds.add(acct.Id);
       }    
 
    List<Opportunity> optyList = [SELECT OwnerId  FROM Opportunity WHERE AccountId in :acctIds];
    if ( !optyList.isEmpty() ) {
    for (Account acct : Trigger.new) {
        for(Opportunity opty : optyList ) {       
            if(opty.OwnerId != acct.OwnerId || opty.OwnerId != acct.SLS2__c){
               opty.OwnerId = acct.OwnerID;
               }
            }
        update optyList;
        }
    }
   }
}
This was selected as the best answer
Ivo BoevIvo Boev
Thank you, Sir!!!
That trigger updated the owner of the opportunity when he does not own or share the account anymore.


However, it also changer the owner of a second opportunity, although he still shares the account. Basically the second opportunity belongs to the user who has SLS2__c populated with his SFDC UserID. That did not suppose to happen, since he is still sharing the account.


I am wondering whether this might happened because of the inconsistency between 15 and 18 character IDs. But as long as I know, OwnerID is 15 character when used to populate a field within the app.
SLS2__c is also 15 characters. 

Any help on this is grqtly appreciated!!!!!! 
Ivo BoevIvo Boev
-

If that can help:
This is the SLS2 field in Accounts
User-added image



This is how User ID look when I transfer them from account to Opportunity.



User-added image



Where "SLS1 Name" = Owner Name in Account
SLS2 = "SLS2" (SLS2__c) in Account


 

Ivo BoevIvo Boev

Good morning,

I changed the query logic to AND: 
   if(opty.OwnerId != acct.OwnerId && opty.OwnerId != acct.SLS2__c){
 

Now when I change ownership manually, it only changes owners of opportunities that change account owners. But I need to use Data Loader.

However, when I use the Data Loader, it changes ownership of ALL opportunities to the new account owner. All Opportunities, no matter whether their account owner had changed, have the same owner=changed owner of the only account which ownership was changed.

Please help.

Ivo BoevIvo Boev

Good morning,

I changed the query logic to AND: 
   if(opty.OwnerId != acct.OwnerId && opty.OwnerId != acct.SLS2__c){
 

Now when I change ownership manually, it only changes owners of opportunities that change account owners. But I need to use Data Loader.

However, when I use the Data Loader, it changes ownership of ALL opportunities to the new account owner. All Opportunities, no matter whether their account owner had changed, have the same owner=changed owner of the only account which ownership was changed.

Please help

.
Jared PaulJared Paul
Hi everyone! Please don't forget to mark "BEST ANSWER" for the most helpful solution and don't forget to post a profile picture if you haven't yet. Thanks!