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
matthew.w.hampton.ax1837matthew.w.hampton.ax1837 

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;
yogeshsharmayogeshsharma
you would need to add all the records in a list and then update the list..
Amit BangadAmit Bangad
Hi,

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
matthew.w.hampton.ax1837matthew.w.hampton.ax1837
That's what I thought but when I tried to add it to a list, I get an error. I know I am missing something simple, I just cannot figure it out.
Amit BangadAmit Bangad
Hey Mattew,

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
GSBassoGSBasso
My understanding of what you are attempting is that whenever MTU_Location__c records are created, associate Billing_Account__c records with any of these MTU_Location__c records per your business rules (i.e. zip codes match and have similar addresses). I'm not sure if it matters whether the Billing_Account__c already has such an association (your posted code suggests not).

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;
}
Amit BangadAmit Bangad
+1 GSBasso