• matthew.w.hampton.ax1837
  • NEWBIE
  • 10 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 6
    Replies
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;

Good Afternoon:

 

I am in the need of a little guidance on the trigger below.

 

I have a custom object (MTU_Location__c) that is the parent in a master-detail to another custom object (MTU_Address__c).

 

I have a trigger bult to create child records based upon certain criteria on the parent object. But what I cannot figure out is how to create multiple child records upon the insertion of the parent.

 

There could be as few as 0 or as many as 15-20 child records that need created upon creation of the parent.

 

Below is the trigger. Any help is greatly appreciated.

 

Thanks,

 

Hampton

 

trigger MTUAddressCreation on MTU_Location__c (after insert, after update) {

    Set<String> mtuAddress = new Set<String>();
     for(MTU_Location__c mtu: trigger.new) {
     mtuAddress.add(mtu.Street_Address__c);
   }
    
    Map<String, Billing_Account__c> billingAccount = new Map<String, Billing_Account__c>();
    for(Billing_Account__c billingAccount1: [Select ID, Name, Account__c, Address_1__c, City__c, State__c, Zip_Code__c, Billing_Account_ID__c from Billing_Account__c where Billing_System__c='CRIS' AND Billing_Account_Status__c='Live' AND Address_1__c in: mtuAddress])
    billingAccount.put(billingAccount1.Address_1__c, billingAccount1); 
    
    List<MTU_Address__c> newAddresses = new List<MTU_Address__c>();
        for(MTU_Location__c location: [Select ID, Name, Street_Address__c, City__c, State__c, Zip_Code__c from MTU_Location__c where Street_Address__c in : billingAccount.keyset()]){
            newAddresses.add(new MTU_Address__c(
                Address__c = location.Street_Address__c,
                City__c = location.City__c,
                State__c = location.State__c,
                Zip_Code__c = location.Zip_Code__c,
                Account__c = billingAccount.get(location.Street_Address__c).Account__c,
                Billing_Account__c=billingAccount.get(location.Street_Address__c).ID,
                MTU_Location__c = location.ID));
  
    }
   insert newAddresses;
}