1

I have a batch process that does a query on an address object with a recordtype for billing / shipping / mailing. When I update the account there are multiple updates. Would there be a better approach then what I'm currently doing? For example, figure out all updated fields for the account record and then update them?

Current Solution:

if(scope.size()!= null){     
  System.debug('scope: '+scope); 
      List<Account> billingAddessAccount = New List<Account>();
      List<Account> shippingAddessAccount = New List<Account>();
      List<Account> mailingAddessAccount = New List<Account>();
      for (New_Account_Address__c ac : scope) {

          // add account to list to be updated

          if(ac.New_Address__r.recordtype.name == 'billing') {
          Account acc = New Account (
              Id = ac.Account__c,
              BillingStreet = ac.New_Address__r.BillingStreet__c,
              BillingPostalCode = ac.New_Address__r.BillingPostalCode__c,
              BillingCity = ac.New_Address__r.BillingCity__c,
              BillingState = ac.New_Address__r.BillingState__c
          );
          billingAddessAccount.add(acc); system.debug('Billing done');
          }
           if(ac.New_Address__r.recordtype.name == 'shipping') {
           Account acc = New Account (
              Id = ac.Account__c,
              ShippingStreet = ac.New_Address__r.ShippingStreet__c,
              ShippingPostalCode = ac.New_Address__r.ShippingPostalCode__c,
              ShippingCity = ac.New_Address__r.ShippingCity__c,
              ShippingState = ac.New_Address__r.ShippingState__c
          );
           shippingAddessAccount.add(acc); system.debug('Shipping done'); }
            if(ac.New_Address__r.recordtype.name == 'mailing') {
              Account acc = New Account (
              Id = ac.Account__c,
              PersonMailingStreet = ac.New_Address__r.MailingStreet__c,
              PersonMailingPostalCode = ac.New_Address__r.MailingPostalCode__c,
              PersonMailingCity = ac.New_Address__r.MailingCity__c,
              PersonMailingState = ac.New_Address__r.MailingState__c
          );
            mailingAddessAccount.add(acc); system.debug('Mailing done');
            }
          // increment the instance member counter
          recordsProcessed = recordsProcessed + 1;
      }
      update billingAddessAccount;
      update shippingAddessAccount;
      update mailingAddessAccount;
  }
  }

2 Answers 2

2

Since they are all accounts you simply can use one list. But if there is same account with multiple update then a map is required. It would look like this:

   if(scope.size() != 0){ //size should not be 0
      Map<Id, Account> accountsWithUpdatedAddress = new Map<Id, Account();
      for (New_Account_Address__c ac : scope) {
          Account acc = accountsWithUpdatedAddress.get(ac.Account__c);
          if(acc == null){
            acc = new Account(Id = ac.Account__c);
          }
          if(ac.New_Address__r.recordtype.name == 'billing') {

              acc.BillingStreet = ac.New_Address__r.BillingStreet__c;
              acc.BillingPostalCode = ac.New_Address__r.BillingPostalCode__c;
              acc.BillingCity = ac.New_Address__r.BillingCity__c;
              acc.BillingState = ac.New_Address__r.BillingState__c;

          system.debug('Billing done');
          } else if(ac.New_Address__r.recordtype.name == 'shipping') {
              acc.ShippingStreet = ac.New_Address__r.ShippingStreet__c;
              acc.ShippingPostalCode = ac.New_Address__r.ShippingPostalCode__c;
              acc.ShippingCity = ac.New_Address__r.ShippingCity__c;
              acc.ShippingState = ac.New_Address__r.ShippingState__c;

           system.debug('Shipping done'); 
       } else if(ac.New_Address__r.recordtype.name == 'mailing') {

              acc.PersonMailingStreet = ac.New_Address__r.MailingStreet__c;
              acc.PersonMailingPostalCode = ac.New_Address__r.MailingPostalCode__c;
              acc.PersonMailingCity = ac.New_Address__r.MailingCity__c;
              acc.PersonMailingState = ac.New_Address__r.MailingState__c;

            system.debug('Mailing done');
            }
            accountsWithUpdatedAddress.put(ac.Account__c, acc);
      }
      update accountsWithUpdatedAddress.values();
  }
}

Howewer if you field is not a lookup (Account__c) then you'd need to update map key with String instead of Id.

2
  • I tried this but if an account gets updated for 2 record types this gives an error duplicate Id in list
    – Thomas
    Dec 16, 2019 at 6:00
  • I updated the code sample with a map.
    – tugce
    Dec 16, 2019 at 10:21
1

You could always update make a Map<Id, Account> from all three lists and then perform a single update:

Map<Id, Account> toUpdate = new Map<Id, Account>();
for (New_Account_Address__c ac : scope) {
 // Update as needed
 toUpdate.put(ac.Account_c, acc)
}

update toUpdate.values();
2
  • I tried your solution but this gives a duplicate error if 2 record types are active.
    – Thomas
    Dec 16, 2019 at 8:57
  • You can use a map to avoid that error. I've updated my solution to reflect this.
    – Brand0R
    Dec 16, 2019 at 15:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .