You need to sign in to do that
Don't have an account?

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:
Thanks in advance!
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!
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.
or
Please mark resolved and best answer if this helped you. thanks.
All Answers
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.
or
Please mark resolved and best answer if this helped you. thanks.
Please update your code like below
Let us know if this will help you