3
trigger CompareAndChangeOpportunityOwner on Account (after update) {

    List<Account> SelectedAccount = [SELECT ID,OwnerID, (SELECT ID,OwnerID FROM Opportunities)  FROM Account WHERE Account.ID IN : trigger.oldMap.keyset()];

    Map<ID,Opportunity> AccountIDandOpportunity = new Map<ID,Opportunity>();

    For(Account acc : SelectedAccount) {
        for(Opportunity op : acc.Opportunities){
            AccountIDandOpportunity.put(acc.ID,op);
        }
    }

    List<Opportunity> opp = new List<Opportunity>();
    List<opportunity> opp2 = new List<opportunity>();

    for(Account a : trigger.new)
    {
       opp2.add(AccountIDandOpportunity.get(a.ID));
       for(Opportunity op : opp2){
       system.debug('compareeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'+op.ownerID+'    '+a.ownerID);
       if(op.OwnerID.equals(a.OwnerID)){
           op.OwnerID = a.OwnerID;
           opp.add(op);
           }
       }


    }

    update opp;
}

The trigger is entering the if statement, but the value is not updating.

please help.

2

I see that there are couple of things wrong with the implementation:

Example this piece of code.

Map<ID,Opportunity> AccountIDandOpportunity = new Map<ID,Opportunity>();

For(Account acc : SelectedAccount) {
            for(Opportunity op : acc.Opportunities){
                AccountIDandOpportunity.put(acc.ID,op);
            }
        }

The relationship between account and opportunity is one to many as one account may contain many opportunities.

The map in which you are putting the opportunities will give you one to one mapping only because a map can contain only unique keys. So every-time you add an opportunity with same account Id to the map, the previous one gets replaced.

Second mistake that I found was that your requirement says that you want to update the owners of the opportunities which do not match your account owner, however in the code you are updating those opportunities which have same ownerId as that of account so actually you wont see any change in the data base.

 if(op.OwnerID.equals(a.OwnerID)){
           system.debug('cominggggggggggggggggggggggggggggggggggggg');
           op.OwnerID = a.OwnerID;
           opp.add(op);
           }
       }

If you put a NOT operator before your If condition, it will work but only for one opportunity.

Here is an optimized piece of code. I have typed this directly as I do not have access to IDE but you'll get the basic idea.

trigger CompareAndChangeOpportunityOwner on Account (after update) {

   //Query Opportunities related to the accounts
    List<Opportunity> queriedOpportunites = [SELECT ID,OwnerID,AccountId FROM Opportunity WHERE AccountID IN : trigger.new];    
    List<Opportunity> oppToUpdate = new List<Opportunity>();

    for(Opportunity opp : queriedOpportunites){
        //Check if Opportunity Owner = Account Owner
        if(!opp.OwnerID.equals(trigger.newMap.get(opp.AccountId).OwnerID)){
            opp.OwnerID = trigger.newMap.get(opp.AccountId).OwnerID;
            oppToUpdate.add(opp);
        }
    }
    update oppToUpdate;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.