You need to sign in to do that
Don't have an account?
Robert Lange 4
I have been trying to write an Apex trigger that displays an error message on the Account record if the user enters Baltimore for BillingCity. Any thoughts? Here is the code I am trying to use:
trigger BaltimoreNo on Account (before insert) {
for (Account acc : Trigger.new){
if (acc.BillingCity='Baltimore'){
alert ('Company XYZ will not do business in city of Baltimore');
}
}
}
for (Account acc : Trigger.new){
if (acc.BillingCity='Baltimore'){
alert ('Company XYZ will not do business in city of Baltimore');
}
}
}
The problem here is you're using '=' operator which is assignment operator instead of this you need to use comparision operator '==' and additionally you need use addError() method here. alert is a Javascript command to show popup on browser.
And yes as Devi & Manto said, you should use available functionality. But it seems like you're new to development and you're practising in you Dev Org.
Please try below code:
Best Regards,
Ashish Singh.
All Answers
No need of trigger for this.You can simply create a validation rule.
Give the following formula in validation rule condition
As a best practice always go for configuration whenever possible without writing code.
Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.
Thanks and Regards
And for any reason if you still want to write apex: replace your alert(...) with acc.addError('...');
The problem here is you're using '=' operator which is assignment operator instead of this you need to use comparision operator '==' and additionally you need use addError() method here. alert is a Javascript command to show popup on browser.
And yes as Devi & Manto said, you should use available functionality. But it seems like you're new to development and you're practising in you Dev Org.
Please try below code:
Best Regards,
Ashish Singh.