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;
}
}