+ Start a Discussion
Joe BBJoe BB 

Update opportunity owner with the current account owner is friteria is met?

I need to update an owner of an opportunity pertaining to an account when the owner of the account changes, but only if the owner of the opportunity is not one of the persons sharing the account pertaining to that opportunity.

 

So the owner of the opportunity should change only if the owner of the opportunity does not coincide with any of the USERS who share the account pertaining to the opportunity. Ownership of the opportunity will have to change to the person who owns the account.

 

Accounts can be shared by 3 persons - one owner and 2 shared users.

The owner of an Opportunity has a  custom field that represents his unique code. For example SLS=AT1. All 3 users who share an account have custom fields both in Accounts that represent their personal unique identification codes - SLS1, SLS2 and SLS3, where SLS1=account owner's code.

For example SLS1=ABF, SLS2=GHT, SLS3=AT1

 

Basically, the logic has to say the following:

If Opportunity field SLS does not match any of the account fields SLS1, SLS2 and SLS3, update opportunity owner with the current account owner.

Is that possible and how? I gues trigger, but how should it look like?

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

HI Joe,

 

Please try below :-

 

Trigger updateOppOwner on Opprtunity(Before insert){

List<Id> accIds = new List<Id>();
List<Account> accList = new List<Account>();

for(Opportunity opp : trigger.new){

accIds.add(opp.AccountId);
}

accList = [select SLS1__c,SLS2__c,SLS3__c,OwnerId from Account where id in:accIds];

for(Opportunity op : Trigger.new){
if(op.SLS__c != accList[0].SLS1__c || op.SLS__c != accList[0].SLS2__c || op.SLS__c != accList[0].SLS3__c){
op.OwnerId = accList[0].OwnerID;

}

}
}

All Answers

Joe BBJoe BB

I did a WF rule but cannot bring up account owner to change the owner of the Opportunity once the criteria is met:

 

This is the rule based on a formula:

 

IF( Opportunity_Owner_SLS__c <> Account.SLS1__c, IF( Opportunity_Owner_SLS__c <> Account.SLS2__c , IF(Opportunity_Owner_SLS__c <> Account.SLS3__c, TRUE, FALSE),FALSE),FALSE)

 

The field update cannot be implemented. There is no referance to Account Ownership.

Any suggestions?

Vinit_KumarVinit_Kumar

HI Joe,

 

Please try below :-

 

Trigger updateOppOwner on Opprtunity(Before insert){

List<Id> accIds = new List<Id>();
List<Account> accList = new List<Account>();

for(Opportunity opp : trigger.new){

accIds.add(opp.AccountId);
}

accList = [select SLS1__c,SLS2__c,SLS3__c,OwnerId from Account where id in:accIds];

for(Opportunity op : Trigger.new){
if(op.SLS__c != accList[0].SLS1__c || op.SLS__c != accList[0].SLS2__c || op.SLS__c != accList[0].SLS3__c){
op.OwnerId = accList[0].OwnerID;

}

}
}

This was selected as the best answer
Joe BBJoe BB

Thanks for the response.

I am trying to realize how to install this trigger in my organization by reviewing the developer guide:

http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf

 

Do you have any idea which method is best? I have not used triggers before.

My org is live but has not been rolled out to the users yet.

 

Thanks!

Vinit_KumarVinit_Kumar

Joe,

 

Please follow the below steps :-

 

1.)Create this Trigger in your Sandbox environment (Go to Customize->Opportunity -> Triggers->New)

 2.) Test it out if this is working as expected.

3.) Create the Test class for the Trigger

4.)Deploy your code in your Prod/Live environment.

Joe BBJoe BB

I installed Eclipse and the Force plugin.

I created a new project and selected opportunities > before insert.

Then I copied the trigger. But do not know what to do after that.

 

BTW, after I created an opportunity opoject in eclipse, I have the trigger showing in Setup> Opportunity>  Triggers in my production environment. However, It stays at:

"Tigger updateOppOwner on Opprtunity(Before insert){"

Icannot edit it. i cannot delete it either. What shoudl I do?

 

After I got your message I went to Sandbox. I had a test sandbox and I refreshed it. So now I cannot login to it. I guess it needs time to refresh.

 

Joe BBJoe BB

This is the trigger that I placed in Production environment, but cannot save it.

 

It says: "

Multiple markers at this line

- File only saved locally, not to server

- Save error: Invalid SObject type name:

Opprtunity"

Trigger updateOppOwner on Opprtunity(Before insert){

List<Id> accIds = new List<Id>();
List<Account> accList = new List<Account>();

for(Opportunity opp : trigger.new){

accIds.add(opp.AccountId);
}

accList = [select SLS1__c,SLS2__c,SLS3__c,OwnerId from Account where id in:accIds];

for(Opportunity op : Trigger.new){
if(op.Opportunity_Owner_SLS__c != accList[0].SLS1__c || Opportunity_Owner_SLS__c != accList[0].SLS2__c || op.Opportunity_Owner_SLS__c != accList[0].SLS3__c){
op.OwnerId = accList[0].OwnerID;

}

}
}

 I just changed SLS field =

Opportunity_Owner_SLS__c , which is the actual name of the field.

Vinit_KumarVinit_Kumar

Joe,

 

Like I said you can't directly create the Trigger in Prod as you are not allowed to do so,you will have to create it in Sandbox and then deploy the same to your Production after testing it.

 

Hence,create a package of your Sandbox in your Eclipse IDE and then paste thhis code and then you can save.

Joe BBJoe BB

Ok, Vinit! Thanks! I will try that.

 

A counple of questions.

 

1) I need Opportunity owner to change based on the criteria after account update for those accounts that are updated after account upsert with the data Loader..

Does this mean that the statement ""Opprtunity(Before insert){" has to be different?

 

2) Do I paste the trigger in Accounts > Triggers or Opportunity >Triggers?

 

Thanks!

 

Vinit_KumarVinit_Kumar

Joe,

 

1.) If the Opportunity owner needs to be updated after the account update via Data Loader,then the Trigger needs to be on Account object.

 

The logic is very simple if you are doing operation on a particular object (Here,it is Account)and then, you want something to happen.So,your Trigger should be on the object on which operation is being performed.

 

the statement should be "" Account(before insert,before update){ ""

 

2.) The Trigger should be pasted on the object on which it is written here it would be account,so,

 

Accounts > Triggers

Joe BBJoe BB

Hello Vinit,

 

I tried to insert the trigger into Account triggers but it gave me the following error:

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

 

This is the code:

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 SLS1__c, SLS2__c, SLS3__c,OwnerId from Account where id in:accIds];

for(Opportunity op : Trigger.new){
if(op.Opportunity_Owner_SLS__c != accList[0]. SLS1__c || op.Opportunity_Owner_SLS__c != accList[0]. SLS2__c || op.Opportunity_Owner_SLS__c != accList[0]. SLS3__c){
op.OwnerId = accList[0].OwnerID;

}

}
}

 

I found my error in line 14. it was a missing "op", but now there is another error.

 

Thank you!

Vinit_KumarVinit_Kumar

Joe,

 

I wrote this Trigger keeping in mind that the Trigger is on Opportunity.Now,that the scenario has changed it needs to be modified.

 

Like from for(opportunity opp: trigger.new) to for(Account ac: Trigger.new) and few other things too.

Joe BBJoe BB

Thank you for the follow up, Vinit!

 

This is the redisigned trigger that works correctly when I change the account ownership manually(within the application), but it does not work properly when I use the Data Loader to change ownership.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
        }
    }
   }
}

 

 

When I change account owner using the Data Loader, it changes all opportunity owners of all accounts (no matter whether they pertain to the same account) to the new owner of the account which owner has been changed.

 

Do you know why it behaves that way. Do you know any resolution?

Thank you.