You need to sign in to do that
Don't have an account?
Apex Trigger not working
What is wrong with this trigger , it is not working .
trigger RestrictContactByName on Contact (before insert,before update) {
List<Contact> cons = [Select Id from Contact where (Id IN :Trigger.New)];
for (Contact c : cons) {
if(c.LastName == 'INVALIDNAME')
{
c.AddError('The Last Name "'+c.LastName+'" is not acceptable');
}
}
}
trigger RestrictContactByName on Contact (before insert,before update) {
List<Contact> cons = [Select Id from Contact where (Id IN :Trigger.New)];
for (Contact c : cons) {
if(c.LastName == 'INVALIDNAME')
{
c.AddError('The Last Name "'+c.LastName+'" is not acceptable');
}
}
}
This trigger will work for before update but not for before insert as there is no id for record so cons List will be always empty for before insert trigger.
and Trigger.new itself is contact list so no need to query
modify your code to following.
Best Regards,
Amit Ghadage
1) https://developer.salesforce.com/forums/?id=906F00000005GIKIA2
Let us know if this will help you