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
Joe BBJoe BB 

Trigger bizarre behavior with Data Loader

 

Fellas,

 

I cannot get this trigger to work upon Data Loader Account upsert.

 

The trigger was designed with the help of two members of the community.

 

It works when I do the changes within the application (UI), but it does not when I change the account owner of using the Data Loader.

 

Objective: After A Data Loader account upsert - Check IF an owner of an opportunity is NOT the owner of the account and a sharing person of the account,

THEN change owner of that opportunity to the NEW owner of the account to which that opportunity pertains.

 

 

For this purpose there is a custom field in account called SLS2__c,  which is updated each time accounts are upserted. This field contains the 15 character SF UserID of the person who shares the account.

 

 

 

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;

                    } else {

                      opty.OwnerId = opty.OwnerId ;

           }

            }

        update optyList;

        }

    }

   }

}

 

 

Again, the trigger works when account ownership is changed manually, but when I use the Data Loader, it changes ownership of ALL opportunities in ALL accounts no matter whether account owner for a particular opportunity has changed or not.

 

Do you have any suggestions? 

JitendraJitendra
Hi,
The reason is that Trigger executes and process record in Bulk. When you try from UI its working because its single record.

Example in DataLoader:
Account ID (only single digit for example) , Account Owner ID , Opp Owner Id
A1 , O1 , O2
A2 , O2 , O1

Now in your query you are getting owner of Account, so in Trigger Owner of account will be always O1 and O2.
So when yoy try this condition -
if(opty.OwnerId != acct.OwnerId && opty.OwnerId != acct.SLS2__c) , It will not true.

Suggestion - Use Map in which Key will be AccId and Value will be OwnerId. Instead of comparing in List, Compare in Map


Joe BBJoe BB

I believe that you are correct.

 

Since it changes owners of all Opportunities for all accounts this means that the trigger does not work within an account and its respective child objects.

 

I do not know how to fix that.

 

Does anyone know?

Vinit_KumarVinit_Kumar

Hi Joe,

 

Please try below :-

 

Map<id,Opportunity> optyMap = new Map<Id,Opportunity>( [SELECT OwnerId  FROM Opportunity WHERE AccountId in :acctIds]);

    if ( !optyMap.isEmpty() ) {

    for (Account acct : Trigger.new) {

        for(Opportunity opty : optyMap.values() ) {      

            if(opty.OwnerId != acct.OwnerId && opty.OwnerId != acct.SLS2__c){

               optyMap.get(acct.id).OwnerID= acct.OwnerID;

                    } else {

                      opty.OwnerId = opty.OwnerId ;

           }

            }

        update optyMap.values();

        }

    }

Joe BBJoe BB

Hi Vinit,

 

Thank you for the follow up!!!!!!!

 

I tried the new trigger the frirst thing in the morning.

It produced the following error when updating account owners with Data loader:

<<< updateOppOwner: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateOppOwner: line 17, column 1">>>

 

Please help me resolve that.

This is the Full trigger:

===============

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

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);
       }     
Map<id,Opportunity> optyMap = new Map<Id,Opportunity>( [SELECT OwnerId  FROM Opportunity WHERE AccountId in :acctIds]);

    if ( !optyMap.isEmpty() ) {

    for (Account acct : Trigger.new) {

        for(Opportunity opty : optyMap.values() ) {      

            if(opty.OwnerId != acct.OwnerId && opty.OwnerId != acct.SLS2__c){

               optyMap.get(acct.id).OwnerID= acct.OwnerID;
                    } else {
          opty.OwnerId = opty.OwnerId ;
                   }
            }

        update optyMap.values();

        }
  }
    }
    }

====================

 

Vinit_KumarVinit_Kumar

Joe,

 

I would recommend you to check the debug logs and see if there is any ownerId associated with Opportunity or not.The error says there is null in the OwnerId for any Opportunity.

Joe BBJoe BB

Hi Vinit,

 

I set up Debug Logs for users that I change Account ownership.

When I update the Acc ownership with DL, the error persists, no logs are produced.

 

When I manually change the owners of accounts, the trigger works fine and no logs or errors are produced.

 

I exported Opportunities and all Opps have OwnerID, which are correctly displayed.