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
baseballkylebaseballkyle 

Trigger not running during batch apex

I have a batch apex class that runs on my custom object successfully - but my trigger doesn't seem to fire. If i edit/save the record manually, trigger fires just fine. What am i missing here? Why doesn't the trigger fire when batch apex runs? 
Best Answer chosen by baseballkyle
Anup ShindeAnup Shinde
It seems to be a problem with the logic in your code.  You shouldn't  really need line #19 in the trigger - that will most likely cause problems when updating more than 1 records

All Answers

Ramesh KallooriRamesh Kalloori
Hi,

Batch apex will be fiered when the resource is available when the batch fired automatically trigger will fiered.

we can check the apex job in:

Jobs>>Apex jobs after that all the batches completed triggers also completed.

thanks,
Ramesh 

baseballkylebaseballkyle
Batch runs, but the trigger does NOT run at all. Apex jobs confirms the batch job completed fine. Help - what's missing here?? 😄
Ramesh KallooriRamesh Kalloori
please send the batch and trigger code.

baseballkylebaseballkyle
batch - 

global class BatchForTrainedEngineerMVSAsset implements Database.Batchable<sObject>{
 
global final String gstrQuery = 'select ID, Refresh__c, Status__c from MVS_Asset__c where Status__c <> \'Retired\'';
     global Database.QueryLocator start(Database.BatchableContext BC){
     return Database.getQueryLocator(gstrQuery);
     }
    
global void execute(Database.BatchableContext BC, List<MVS_Asset__c> scope)
{
     for(MVS_Asset__c mvsAsset : scope)
     {
          mvsAsset.Refresh__c = TRUE;
     }
     update scope;
}

global void finish(Database.BatchableContext BC){
}

}
trigger - 

trigger trainedEngineerMVSAsset on MVS_Asset__c (before insert,before update) {

List<ID> mvaModel = New List<ID>();

for(MVS_Asset__c m : Trigger.new){
     mvaModel.add(m.Model_Name__c);
     m.Primary_Trained__c = false;
}   

List<Trained_Engineer__c> tEngList = [SELECT id, Model__c, FSE_Name__c FROM Trained_Engineer__c WHERE Model__c in :mvaModel];

  for(MVS_Asset__c mvaPrimFSE : trigger.new){
    for(integer i = 0 ; i < tEngList.size(); i++)
      if(mvaPrimFSE.Model_Name__c == tEngList[i].Model__c &&
         mvaPrimFSE.Primary_FSE__c == tEngList[i].FSE_Name__c){
              mvaPrimFSE.Primary_Trained__c = true;}
      else{mvaPrimFSE.Primary_Trained__c = false;}
  }    

}


souvik9086souvik9086
Is the object MVS_Asset__c is being updated on Batch apex update?
Can you check that?
baseballkylebaseballkyle
Yes, I have confirmed that it is being updated. Yer trigger still doesn't fire.
Anup ShindeAnup Shinde
It is highly unlikely that a trigger does not fire. In your trigger, after line 10 - add a line m.addError('Temp Error').    This will prevent a record being inserted or update - both from the UI  and you willl see the error in the Apex Batch Jobs (and debug logs).

Try this and see if the trigger is being invoked - or some IF condition failing.
baseballkylebaseballkyle
ok so i tried that, and yes it does show in the apex jobs log failing due ot the temp error - so trigger is running it would appear. BUT executing the batch, which fires the trigger, does not give me the intended result on the mvs asset record. however when i simply edit/save the record the trigger performs as expected and the primary trained field gets checked on mvs asset. ideas?
Anup ShindeAnup Shinde
It seems to be a problem with the logic in your code.  You shouldn't  really need line #19 in the trigger - that will most likely cause problems when updating more than 1 records
This was selected as the best answer
baseballkylebaseballkyle
thanks for the help Anup! everything is working now :)
Harshitha Shreeka 8Harshitha Shreeka 8

@baseballkyle Can you please let me know how the issue got resolved?  we are facing a similar issue.

Santosh UppalaSantosh Uppala
What could be general reasons for causing such issues? I am facing a similar issue, my code worked fine, however, after a certain period triggers not executing though batch apex executes and updates the records fine without any issue.