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
Brian KoBrian Ko 

Validation Rule Preventing Apex Trigger From Copying Billing Country To Shipping Country

Hey all,

I'm trying to use Apex Trigger to populate a Shipping Address from the Billing Address if no Shipping Country is provided. However, I have a validation rule in place that requires the Shipping Country to be filled out before Account creation. From what I read, before triggers should fire before validation rules. However, I keep getting the validation rule error message when trying to create an Account with a Billing Address but no Shipping Address.

Here's the code:
trigger AccountShippingAddressUpdate on Account (before insert) {
    for (Account a : trigger.new)
    {
        if(a.ShippingCountry == '')
        {
            a.ShippingCity = a.BillingCity;
            a.ShippingCountry = a.BillingCountry;
            a.ShippingCountryCode = a.BillingCountryCode;
            a.ShippingState = a.BillingState;
            a.ShippingStateCode = a.BillingStateCode;
            a.ShippingStreet = a.BillingStreet;
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}

Thanks in advance!
Best Answer chosen by Brian Ko
Ashish KeshariAshish Keshari
Hi Brian,
The empty ShippingCountry returns a null and not a blank string. Hence your "" returns false always and values are not set in trigger properly and hence the validation rule is failing. Please use the below code.
trigger AccountShippingAddressUpdate on Account (before insert) {
    for (Account a : trigger.new)
    {
        if(a.ShippingCountry == null)
        {
            a.ShippingCity = a.BillingCity;
            a.ShippingCountry = a.BillingCountry;
            a.ShippingCountryCode = a.BillingCountryCode;
            a.ShippingState = a.BillingState;
            a.ShippingStateCode = a.BillingStateCode;
            a.ShippingStreet = a.BillingStreet;
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}

or 
trigger AccountShippingAddressUpdate on Account (before insert) {
    for (Account a : trigger.new)
    {
        if(String.isBlank(a.ShippingCountry))
        {
            a.ShippingCity = a.BillingCity;
            a.ShippingCountry = a.BillingCountry;
            a.ShippingCountryCode = a.BillingCountryCode;
            a.ShippingState = a.BillingState;
            a.ShippingStateCode = a.BillingStateCode;
            a.ShippingStreet = a.BillingStreet;
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}

Please mark resolved and best answer if this helped you. thanks.

All Answers

Ashish KeshariAshish Keshari
Hi Brian,
The empty ShippingCountry returns a null and not a blank string. Hence your "" returns false always and values are not set in trigger properly and hence the validation rule is failing. Please use the below code.
trigger AccountShippingAddressUpdate on Account (before insert) {
    for (Account a : trigger.new)
    {
        if(a.ShippingCountry == null)
        {
            a.ShippingCity = a.BillingCity;
            a.ShippingCountry = a.BillingCountry;
            a.ShippingCountryCode = a.BillingCountryCode;
            a.ShippingState = a.BillingState;
            a.ShippingStateCode = a.BillingStateCode;
            a.ShippingStreet = a.BillingStreet;
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}

or 
trigger AccountShippingAddressUpdate on Account (before insert) {
    for (Account a : trigger.new)
    {
        if(String.isBlank(a.ShippingCountry))
        {
            a.ShippingCity = a.BillingCity;
            a.ShippingCountry = a.BillingCountry;
            a.ShippingCountryCode = a.BillingCountryCode;
            a.ShippingState = a.BillingState;
            a.ShippingStateCode = a.BillingStateCode;
            a.ShippingStreet = a.BillingStreet;
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}

Please mark resolved and best answer if this helped you. thanks.
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8

Please update your code like below
trigger AccountShippingAddressUpdate on Account (before insert ,before update) 
{
    for (Account a : trigger.new)
    {
        if( a.ShippingCountry== null || a.ShippingCountry =='' )
        {
            a.ShippingCity = a.BillingCity;
            a.ShippingCountry = a.BillingCountry;
            a.ShippingCountryCode = a.BillingCountryCode;
            a.ShippingState = a.BillingState;
            a.ShippingStateCode = a.BillingStateCode;
            a.ShippingStreet = a.BillingStreet;
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}

Let us know if this will help you
Brian KoBrian Ko
Thank you Ashish and Amit! That worked like a charm!