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
Community_NameCommunity_Name 

Trigger to run the Batch Apex

Hi

 

i have a scenario about trigger to run the batch apex,

 

can anyone suggest me how to do that..

 

here is the scenario...

 

 

we have a batch apex in our sandbox, currently we running that batch manually.

 

we have an object called Integration Updates, in that object we have a picklist field called Status. when the Informatica daily load are completed they will insert one record into integration updates object and put the status value to "INFA Load Completed" and save the record.

 

based on the that status field value trigger(after insert or after update) will fire and run the batch apex. once batch apex completed based on that  we have to update the status field to "Batch Apex Completed".

 

i have completed the trigger till batch apex to run. i could not able to find any solution that how to update the status field value to "Batch Apex Compelted". 

 

here is the code that i wrote..

 

trigger RunBatchApexProcess on Integration_Update__c (after insert, after update) {
  
  for(Integration_Update__c intgupdate : Trigger.new){
     
     if(intgupdate.Status__c=='INFA Load Completed'){
              
        UnitIntegrationUpdates obj = new UnitIntegrationUpdates();
        ID batchId = Database.executebatch(obj,100);
    
     }
   }     
}

 

can anyone suggest me the extension code for this..

 

 

thanks in advance...

Shashikant SharmaShashikant Sharma

I can suggest you one thing that

1)Cretae a public property in your batch

2) Assign this the id of the record that you want to update.

3)In the finish method of batch fetch it and update the status for the record.

 

I hope I could explain it to you.

Rahul SharmaRahul Sharma

Is there any special purpos, for which you want to use a batch?

Community_NameCommunity_Name

thanks for the reply...

 

if you have any sample code for that can post it for me..

 

 

thanks agian...

 

Rahul SharmaRahul Sharma

Hope it understandable:

 

Trigger:

 

trigger UpdateContactType on Account (after update)
{
Set<Id> SetAccountId = new Set<Id>();
for(Account objAccount: [Select Id from Account where Id in : Trigger.new])
{
SetAccountId.add(objAccount.Id);
}
if(!SetAccountId.isEmpty())
{
string strquery='SELECT Name ,Id ,UpdateContact_Type__c FROM Account Where Id IN :SetAccountId';
BatchToUpdateContactType objBatchToUpdateContactType=new BatchToUpdateContactType(strquery);
Database.executeBatch(objBatchToUpdateContactType);
}
}

Batch:

 

global class BatchToUpdateContactType implements Database.Batchable<sObject>, Database.stateful
{
global string strqry='';
global BatchToUpdateContactType(String q)
{
strqry=q;
}
global Database.Querylocator start (Database.Batchablecontext BC)
{
return Database.getQueryLocator(strqry);
}
global void execute (Database.Batchablecontext BC, list<sObject> scope)
{
// Perform your Action here
system.debug('All your Accounts are here'+scope);
 } global void finish(Database.Batchablecontext BC) { } }

This is a basic example, hope it helps.

 

 

 

Pal2011Pal2011

Rahul,

 

Thanks for this example. It really helped me to understand how to call Batch Apex from trigger.

 

But, in my case I need to call  batch apex after every update, and need to compare Trigger.old and Trigger.new values inside execute method. Can you help me, how to do that?

 

Thanks,

Jerun JoseJerun Jose

You could use static final variables in your batch apex and pass trigger.new and trigger.old to those final variables.

Check the batch apex documentation for the section on referencing intial state in batch apex. You should be able to check between trigger.new and trigger.old by doing something along those lines.