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

Simple Trigger not firing
I have below trigger to throw the error when the test__c value is null (I understand that I can use a validation rule but I don't want to use it).
It is not firing. can someone let me know the reason.
trigger CandidateCityRequired on Account (before insert) {
list acc = [select id,test__c from Account WHERE Id IN:Trigger.new];
for (Account Candidate : acc) {
if (Candidate.test__c == null)
Candidate.addError('City cannot be NULL');
}
}
Since the trigger is not firing, more specifically is there any logic error on the trigger.new SOQL statement. ??
I want to bulkify a trigger and its very urgent.
Please help me by correcting my code.
Use following code and let me if does not work.
1) In before Trigger you dnt need to query the record again
2) You dnt need to execute the DML again
3) In before insert ID will always be null
. For more information Please check below blog and post. I hope that will help you.
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html
1) isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or anexecuteanonymous() API call.
2) isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or theAPI.
3) isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or theAPI.
4) isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or theAPI.
5) isBefore Returns true if this trigger was fired before any record was saved.
6) isAfter Returns true if this trigger was fired after all records were saved.
7) isUndelete Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
8) new Returns a list of the new versions of the sObject records.Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
9) newMap A map of IDs to the new versions of the sObject records. Note that this map is only available in before update, after insert, and after update triggers.
10) old Returns a list of the old versions of the sObject records.Note that this sObject list is only available in update and delete triggers.
11) oldMap A map of IDs to the old versions of the sObject records.Note that this map is only available in update and delete triggers.
12) size The total number of records in a trigger invocation, both old and new.
Please let us know if this will help you.
Thanks
Amit Chaudhary
//this will bulkify our trigger :D
List<Account> acc = Trigger.new;
for(Account candidate: acc) {
if ( candidate.test__c == null) {
candidate.addError('City cannot be NULL'); }
}
}
Try the following code, it will be work.