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
gaurav.sfdcgaurav.sfdc 

Change ownerId of Accounts

I have an excel sheet with zip to user mapping. Now Accounts with those zip needs to be mapped with corresponding userid. For this I followed following approach,
I created a custom object (to upload data via loader) with user look-up--> written a before insert trigger on custom object to fetch user Id based on name. Now while writing After Insert trigger I would need some zip-account mapping (can be multiple accounts per Zip) but I stuck keeping Select out for loop which would break definately as there are almost 50 K records, Could you please help me with After trigger with optimized code or any altogether different approach.
ShashForceShashForce
Hi Gaurav,

I am assuming there is a zip field on the account. But is this zip field a custom text field or a lookup, or is it the standard Billing Zip/Postal code field? Based on this I can provide you a sample trigger.

Thanks,
Shashank
ShashForceShashForce
Hi Gaurav,

I have written this sample assuming zip__c is a custom object with user__c as the user lookup field and the zip code is stored in the default name field, and the zip maps to the BillingPostalCode field on the Account object. You can modify it as per your need.

trigger updateAccount on Zip__c (after insert) {
    set<string> zipset = new set<string>();
    list<account> accuplist = new list<account>();
    for(zip__c z:trigger.new){
        zipset.add(z.name);
    }
    list<account> acclist = [select BillingPostalCode,ownerId,name from account where BillingPostalCode IN :zipset];
    map<zip__c,list<account>> zipaccmap = new map<zip__c,list<account>>();
    for(zip__c zp:trigger.new){
        list<account> aclist = new list<account>();
        for(account a:acclist){ 
            if(a.BillingPostalCode!=null){
                if(zp.name==a.BillingPostalCode){
                    aclist.add(a);
                }
            }
        }
        if(aclist.size()>0){
            zipaccmap.put(zp,aclist);
        }
    }
    for(zip__c zip:zipaccmap.keyset()){
        for(account a:zipaccmap.get(zip)){
            account acct = new account();
            acct.Id = a.Id;
            acct.ownerId = zip.user__c;
            accuplist.add(acct);
        }
    }
    if(accuplist.size()>0){
        update accuplist;
    }
}

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
gaurav.sfdcgaurav.sfdc
Thanks Shashank,

I ve tried with Apex approach (job)

global class TempClassForAccountOwnerUpdate{

  public static void ChangeOwnerIds()
  {
  
      Map<string,Id> mapZipUser = new MAP<string,Id>(); 
  
      List<TempZipUser__c> listObjZipUser = [SELECT zip_code__c,user__c,ownerid from TempZipUser__c where user__c <> null];
  
      List<Account> listAccToUpdate = new List<Account>();
      
      for(TempZipUser__c obj : listObjZiptoRep) 
      {
        mapZipUser.put(obj.zip_code__c, obj.cfs__C);
      }
      
      SET<string> setZips = mapZipUser.keyset();
      List<Account> lstAccounts = [select Id, ownerId,shippingPostalCode from Account where shippingPostalCode IN :setZips];
  
      for(Account acc : lstAccounts) 
      {
        if (mapZipUser.containsKey(acc.shippingPostalCode))
        {
           acc.ownerId = mapZipUser.get(acc.shippingPostalCode);
           listAccToUpdate.add(acc); 
        } 
      }
      
      update lstAccounts;

  }

}

What do you say about this approach?

Regards
Gaurav
ShashForceShashForce
It's a good approach, Gaurav. I would also suggest you to tweek the code to update the accounts only if the ownerId is different. You can do it by adding another map of zip codes with user Ids.
gaurav.sfdcgaurav.sfdc
Thanks Shashank .. Actually this approach dosen't require complex map and nested for-loops,And the activity is devided into two steps to make sure if steps 1 goes fine then to start with another.Though I think there can be enough scope to optmize the code further..
but thanks for providing the trigger approach as well as people might be intrested appraocah as well