You need to sign in to do that
Don't have an account?

Help With a For Loop To Update Multiple Records
Good Morning:
I have the following piece of code that I am having some issues with.
What I am trying to do is to pull all Billing_Account__c records where Billing_Account__c.Zip_Code__c = MTU_Location__c.Zip_Code__c and Billing_Account__c.Address_1__c CONTAINS MTU_Location__c.Street_Address__c. After doing so, I want to update Billing_Account__c.MTU_Location__c with MTU_Location__c.ID.
The query should return multiple records to update, which it does, but that is causing my trigger to erorr out - I am getting the SOQL returned multiple rows for assignment error.
How to I re-write this so that it pulls multiple records but allows me to update multiple records?
Thanks,
Matt
for(MTU_Location__c mtuRecord : addMTUList){
Billing_Account__c mtuUpdate = [Select ID, Address_1__c, Zip_Code__c, MTU_Location__c from Billing_Account__c where Zip_Code__c = :mtuRecord.Zip_Code__c];
if(mtuUpdate.Address_1__c.CONTAINS(mtuRecord.Street_Address__c)){
mtuUpdate.MTU_Location__c = mtuRecord.ID;}
updateBillingAccountList.add(mtuUpdate);
}
update updateBillingAccountList;
I have the following piece of code that I am having some issues with.
What I am trying to do is to pull all Billing_Account__c records where Billing_Account__c.Zip_Code__c = MTU_Location__c.Zip_Code__c and Billing_Account__c.Address_1__c CONTAINS MTU_Location__c.Street_Address__c. After doing so, I want to update Billing_Account__c.MTU_Location__c with MTU_Location__c.ID.
The query should return multiple records to update, which it does, but that is causing my trigger to erorr out - I am getting the SOQL returned multiple rows for assignment error.
How to I re-write this so that it pulls multiple records but allows me to update multiple records?
Thanks,
Matt
for(MTU_Location__c mtuRecord : addMTUList){
Billing_Account__c mtuUpdate = [Select ID, Address_1__c, Zip_Code__c, MTU_Location__c from Billing_Account__c where Zip_Code__c = :mtuRecord.Zip_Code__c];
if(mtuUpdate.Address_1__c.CONTAINS(mtuRecord.Street_Address__c)){
mtuUpdate.MTU_Location__c = mtuRecord.ID;}
updateBillingAccountList.add(mtuUpdate);
}
update updateBillingAccountList;
Replace this line :
Billing_Account__c mtuUpdate = [Select ID, Address_1__c, Zip_Code__c, MTU_Location__c from Billing_Account__c where Zip_Code__c = :mtuRecord.Zip_Code__c];
with
Billing_Account__c mtuUpdate = [Select ID, Address_1__c, Zip_Code__c, MTU_Location__c from Billing_Account__c where Zip_Code__c = :mtuRecord.Zip_Code__c limit 1];
Thanks,
AMit
You have fired a Query in a FOR loop which will break addMTUList>100. It will give you Too many SOQL Queries error ! Are you getting this error?
THanks
Amit
I'm not exactly sure why you are getting that particular error but my concern would be with the (potentially) repeated query inside your for loop.
I would suggest refactoring this code so that you only query for Billing_Account__c records once, something similar to the following:
Map<String, MTU_Location__c> zipCodeMap = new Map<String, MTU_Location__c>();
// build map from addMTUList
for (Billing_Account__c mtuUpdate : [SELECT Id, Address_1__c, Zip_Code__c FROM Billing_Account__c WHERE Zip_Code__c IN :zipCodeMap.keySet()])
{
if (mtuUpdate.Address_1__c.contains(zipCodeMap.get(mtuUpdate.Zip_Code__c).Street_Address__c))
{
mtuUpdate.MTU_Location__c = zipCodeMap.get(mtuUpdate.Zip_Code__c).Id;
updateBillingAccountList.add(mtuUpdate);
}
}
if (!updateBillingAccountList.isEmpty())
{
update updateBillingAccountList;
}