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
hramanihramani 

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.
Prabhat Kumar12Prabhat Kumar12
It's simple. You don't need to query the in before trigger.

Use following code and let me if does not work.
 
trigger firon on Account (before insert) {

for (Account Candidate : trigger.new) {
if (Candidate.test__c == null) 
Candidate.addError('City cannot be NULL');
}
}

 
Amit Chaudhary 8Amit Chaudhary 8
In Before Trigger :-
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
.
trigger firon on Account (before insert) {

for (Account Candidate : trigger.new) {
if (Candidate.test__c == null) 
Candidate.addError('City cannot 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
 
Jayson Faderanga 14Jayson Faderanga 14
trigger firon on Account (before insert) {
//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'); }
}
}
 
Vijay NagarathinamVijay Nagarathinam
Hi,

Try the following code, it will be work.
 
trigger checkCandidate on Account (before insert) {

for (Account Candidate : trigger.new) {
if (Candidate.test__c != null) 
   // do some operations
}
else{
    Candidate.addError('City Can't be Null');
}
}
}