You need to sign in to do that
Don't have an account?
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...
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.
Is there any special purpos, for which you want to use a batch?
thanks for the reply...
if you have any sample code for that can post it for me..
thanks agian...
Hope it understandable:
Trigger:
Batch:
This is a basic example, hope it helps.
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,
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.