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
Benedict Yeo 6Benedict Yeo 6 

Trigger to update value on Order from Account

Hi, I'm trying to update addresses on my Orders record from the Account selected. This is my code, but somehow it's getting very low code coverage, and I'm not sure what I'm doing wrong.. Can someone please point me in the right direction? Thanks!
 
trigger fillAddresses on Order (before insert) {
    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
            try {
                Order[] orders = Trigger.new;
                
                prefillFields.applyAddress(orders);
            }
            catch (Exception ex) {
                System.debug('Exception caught: ' + ex.getMessage());
            }
        }
    }
}


 
public class prefillFields {
    
    public static void applyAddress(Order[] orders) {
        for (Order o : orders) {
            Account[] accounts = [SELECT BillingCity, BillingCountry, BillingPostalCode, BillingState, BillingStreet, ShippingCity, ShippingCountry, ShippingPostalCode, ShippingState, ShippingStreet, Department_NS__c, ReportingCountry_NS__c FROM Account WHERE AccountNumber =: o.AccountId];
            
            // System.Debug('a.BillingCountry');
            // for (Account a : accounts) {
                o.BillingCity = 'City';
                o.BillingCountry = 'Country';
                o.BillingPostalCode = '12345';
                o.BillingState = '';
                o.BillingStreet = 'Street';
                o.ShippingCity = 'City';
                o.ShippingCountry = 'Country';
                o.ShippingPostalCode = 'Country';
                o.ShippingState = '';
                o.ShippingStreet = 'Street';
                // Still need to get set fields for Department and Shipping Country
            // }
            
            update o;
        }
    }   
}

 
Benedict Yeo 6Benedict Yeo 6
Just a note: I tried it both with and without the for loop line in prefillFields (line 8 and 20).. Thanks!