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
Shruthi MN 88Shruthi MN 88 

Invalid loop variable type expected SObject was Account

Hi Everyone,

I have the below trigger senario
On the Account object, When an account is inserted, automatically account Billing address should populate into the account shipping address.

I have created the below handler class I am getting the attached error:

public class AccountAddressTrigger {
    public static void createAccount(List<Account> accounts) {
      
        for(Account a:trigger.new){
        if(a.ShippingAddress!=null){
            a.ShippingCity = a.BillingCity;
            a.ShippingCountry = a.BillingCountry;
            a.ShippingPostalCode = a.BillingPostalCode;
            a.ShippingStreet = a.BillingStreet;
            a.ShippingState = a.BillingState;
            
            
        }
        
    }
}
}User-added image
Nishant SoniNishant Soni
Hi  Shruthi MN 88,

In your handler class, you have defined the method createAccount that takes a parameter accounts of type List<Account>. However, based on the code, it seems like you are trying to access trigger context variables (trigger.new) directly within the handler method.

To properly use the trigger context variables within your handler, you need to pass them as arguments to the handler method from your trigger. Here's how you can do it:
public class AccountAddressTrigger {
    public static void createAccount(List<Account> accounts){
        
        for(Account a : accounts){
            if(a.ShippingAddress!=null){
                a.ShippingCity = a.BillingCity;
                a.ShippingCountry = a.BillingCountry;
                a.ShippingPostalCode = a.BillingPostalCode;
                a.ShippingStreet = a.BillingStreet;
                a.ShippingState = a.BillingState;  
            }
        }
    }
}

Thanks,
Nishant