I have a trigger on Asset Object. Inside the trigger before update event, am creating another custom object(RelatedAssetLogicsHistory) record with incoming asset values(without any conditions). Actually, this will create a RelatedAssetLogicsHistory record for every update event on Asset object.

Then processing the RelatedAssetLogicsHistory records in batch process(scheduled 15 mins once) and updating one of the field in related Asset. This update call on Asset in batch apex causing the Triggers to fire again. So, again another RelatedAssetLogicsHistory record is creating for the Asset.

This is going infinitely. Any idea to avoid creating a record after batch update call?.

sample trigger code:

if(Trigger.isBefore && Trigger.isUpdate){
    List<AssetLogicHistory__c> logicsHistoryToInsert = new List<AssetLogicHistory__c>();
    for(Asset assetToProcess : (Trigger.new) {
         AssetLogicHistory__c logicsHistory = new AssetLogicHistory__c();
         logicsHistory.assetid__c = assetToProcess.id;
         logicsHistory.lastUpdate__c = system.now();
         logicsHistory.handleLogicText__c = '...concatenating some strings...';
    if(logicsHistoryToInsert.size > 0)
        insert logicsHistoryToInsert;

Sample batch update

     //querying the AssetLogicHistory__c records where batch processed equals to false
public void execute(Database.BatchableContext BC, List<AssetLogicHistory__c> scope){

    //querying asset from incoming AssetLogicHistory__c record AssetId
    List<Asset> assetToUpdate = new List<Asset>();
    List<AssetLogicHistory__c> batchProcessedForHistory = new List<AssetLogicHistory__c>();
    for(Asset assetLoop : queriedAsset){
        for(AssetLogicHistory__c logics : scope){
            if(assetLoop.id == logics.assetId__c){
                assetLoop.LogicsFromHistoryRecord = logics.handleLogicText__c;
                logics.IsBatchProcessed = true;
        update assetToUpdate;   -----> this update call again firing the above trigger and creating another record
    if(batchProcessedForHistory.size() > 0)
        update batchProcessedForHistory;
  • What did you try?. Post your code and ask for specific guidance – RedDevil Jun 28 '17 at 17:16
  • @RedDevil Updated with sample code block. I don't have any idea to avoid this. Will be really helpful if you provide solution for this. – Prince Jun 28 '17 at 17:38

Hi there is a system method System.isBatch() to check if it is running from a batch. You can use this with an IF condition.

if(Trigger.isBefore && Trigger.isUpdate && !System.isBatch()){
// your code here 
  • Exactly what I need. Thanks for the help. Learning everyday. – Prince Jun 28 '17 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.