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...';
logicsHistoryToInsert.add(logicsHistory);
}
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;
batchProcessedForHistory.add(logics);
}
}
assetToUpdate.add(assetLoop);
}
if(assetToUpdate.size()>0)
update assetToUpdate; -----> this update call again firing the above trigger and creating another record
if(batchProcessedForHistory.size() > 0)
update batchProcessedForHistory;
}