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
rao venkyrao venky 

Field trigger

Hai to all,
My question is Can we make the field is required by the trigger?

if yes please send the code
Shrikant BagalShrikant Bagal

Use following code:


trigger accountTestTrggr on Account (before insert, before update) {
for(Account acc : Trigger.new)
{
     if(acc.MailingCity == null)
    {
            acc.addError('Mailing City Should not be Empty');
    }
}
}

Advice:

why you are not using Validation rule or Use PageLayout Required / Field Level required Options.

Hope its help!!
Thanks!
 
ManojjenaManojjena

HI Venky ,

Hi Venky ,
Srikant is correct basically you should go for other option as you have .
Still as you want that ,you can write trigger in two different ways to get error in two different level .

Please try with below code it will help !!
 
//Error will display in field level
trigger ValidationTrigger on Account (before insert,before update) {
   for(Account acc :Trigger.new){
     if (acc.Type == null)
     acc.Type.addError('Type Mandatory ');
   }
}
//Error will display in record level
trigger ValidationTrigger on Account (before insert,before update) {
   for(Account acc :Trigger.new){
	if (acc.Type == null)
     acc.addError('Type Mandatory');
   }
}
Let me know if it helps !!
Thanks
Manoj
Shrikant BagalShrikant Bagal
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
​Thanks!