You need to sign in to do that
Don't have an account?
Not able to throw error upon entering duplicate name field using triggers.
Hello Masters,
I am trying to write a trigger that is supposed to throw an error when a field Name is populated with a pre-existing value. Please have a look at my code below and advise. The code although saves successfully but doesn't work as desired.
Code Logic:
if(trigger.IsInsert && trigger.IsUpdate)
{
List<Disasters__c> DisList = [select Id,Name from Disasters__c];
for (Disasters__c dis : Trigger.new)
{
for (Disasters__c lis: DisList)
{
if(lis.Name == dis.Name)
{
lis.Name.AddError('Name already exists. Try another name');
}
}
I am trying to write a trigger that is supposed to throw an error when a field Name is populated with a pre-existing value. Please have a look at my code below and advise. The code although saves successfully but doesn't work as desired.
Code Logic:
if(trigger.IsInsert && trigger.IsUpdate)
{
List<Disasters__c> DisList = [select Id,Name from Disasters__c];
for (Disasters__c dis : Trigger.new)
{
for (Disasters__c lis: DisList)
{
if(lis.Name == dis.Name)
{
lis.Name.AddError('Name already exists. Try another name');
}
}
See below code which is updated code of @Raj.