You need to sign in to do that
Don't have an account?
Chinna Sfdc
How to bypass the Trigger logic in Batch Apex
Hi All,
I want to know bypass the Logic in my Batch apex.Problem is when we are running the Batch apex some records are updating some are not.Because we have custom settings are firing in my logic.Can any one help us how to bypass the custom setting logic in my Batch apex.Please check my Below Batch Apex Logic which we are using Currently.
global class OpportunityDays implements Database.Batchable<Sobject>{
Public List<Opportunity> lstOpp;
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'Select id, Name, StageName, Software_Product_Count__c, CW_in_Last_30_Days_Formula__c, CW_in_Last_30_Days_Workflow__c, CW_in_Last_730_Days_Formula__c, CW_in_Last_730_Days_Workflow__c from Opportunity Where StageName = \'S8- Closed Won\' AND Software_Product_Count__c > 0 AND ( ((CW_in_Last_30_Days_Formula__c = TRUE AND CW_in_Last_30_Days_Workflow__c = False) OR (CW_in_Last_30_Days_Formula__c = False AND CW_in_Last_30_Days_Workflow__c = True)) OR ((CW_in_Last_730_Days_Formula__c = TRUE AND CW_in_Last_730_Days_Workflow__c = False) OR (CW_in_Last_730_Days_Formula__c = False AND CW_in_Last_730_Days_Workflow__c = True )))';
system.debug('START Method...');
return Database.getQueryLocator(query);
}
/* EXECUTE Method */
global void execute(Database.BatchableContext BC, List<Opportunity> batch){
system.debug('EXECUTE Method...');
lstOpp = new List<Opportunity>();
for(Opportunity objOpp : batch){
boolean shouldUpdate = false;
if(objOpp.CW_in_Last_30_Days_Formula__c == True){
objOpp.CW_in_Last_30_Days_Workflow__c = True;
shouldUpdate = true;
system.debug('CW 30 WF True...'+ objOpp.CW_in_Last_30_Days_Workflow__c);
}
else if(objOpp.CW_in_Last_30_Days_Formula__c == False){
objOpp.CW_in_Last_30_Days_Workflow__c = False;
shouldUpdate = true;
system.debug('CW 30 WF False...'+ objOpp.CW_in_Last_30_Days_Workflow__c);
}
if(objOpp.CW_in_Last_730_Days_Formula__c == True){
objOpp.CW_in_Last_730_Days_Workflow__c = True;
shouldUpdate = true;
system.debug('CW 730 WF True...'+ objOpp.CW_in_Last_730_Days_Workflow__c);
}
else if(objOpp.CW_in_Last_730_Days_Formula__c == False){
objOpp.CW_in_Last_730_Days_Workflow__c = False;
shouldUpdate = true;
system.debug('CW 730 WF False...'+ objOpp.CW_in_Last_730_Days_Workflow__c);
}
if(shouldUpdate==true){
lstOpp.add(objOpp);
}
//update lstOpp;
}
if(lstOpp.size()>0)
List<Database.SaveResult> resultList = Database.update(lstOpp, false);
}
/* FINISH Method */
global void finish(Database.BatchableContext BC){
system.debug('EXECUTE Method...');
}
}
Please help us on this.
Thanks in Advance
I want to know bypass the Logic in my Batch apex.Problem is when we are running the Batch apex some records are updating some are not.Because we have custom settings are firing in my logic.Can any one help us how to bypass the custom setting logic in my Batch apex.Please check my Below Batch Apex Logic which we are using Currently.
global class OpportunityDays implements Database.Batchable<Sobject>{
Public List<Opportunity> lstOpp;
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'Select id, Name, StageName, Software_Product_Count__c, CW_in_Last_30_Days_Formula__c, CW_in_Last_30_Days_Workflow__c, CW_in_Last_730_Days_Formula__c, CW_in_Last_730_Days_Workflow__c from Opportunity Where StageName = \'S8- Closed Won\' AND Software_Product_Count__c > 0 AND ( ((CW_in_Last_30_Days_Formula__c = TRUE AND CW_in_Last_30_Days_Workflow__c = False) OR (CW_in_Last_30_Days_Formula__c = False AND CW_in_Last_30_Days_Workflow__c = True)) OR ((CW_in_Last_730_Days_Formula__c = TRUE AND CW_in_Last_730_Days_Workflow__c = False) OR (CW_in_Last_730_Days_Formula__c = False AND CW_in_Last_730_Days_Workflow__c = True )))';
system.debug('START Method...');
return Database.getQueryLocator(query);
}
/* EXECUTE Method */
global void execute(Database.BatchableContext BC, List<Opportunity> batch){
system.debug('EXECUTE Method...');
lstOpp = new List<Opportunity>();
for(Opportunity objOpp : batch){
boolean shouldUpdate = false;
if(objOpp.CW_in_Last_30_Days_Formula__c == True){
objOpp.CW_in_Last_30_Days_Workflow__c = True;
shouldUpdate = true;
system.debug('CW 30 WF True...'+ objOpp.CW_in_Last_30_Days_Workflow__c);
}
else if(objOpp.CW_in_Last_30_Days_Formula__c == False){
objOpp.CW_in_Last_30_Days_Workflow__c = False;
shouldUpdate = true;
system.debug('CW 30 WF False...'+ objOpp.CW_in_Last_30_Days_Workflow__c);
}
if(objOpp.CW_in_Last_730_Days_Formula__c == True){
objOpp.CW_in_Last_730_Days_Workflow__c = True;
shouldUpdate = true;
system.debug('CW 730 WF True...'+ objOpp.CW_in_Last_730_Days_Workflow__c);
}
else if(objOpp.CW_in_Last_730_Days_Formula__c == False){
objOpp.CW_in_Last_730_Days_Workflow__c = False;
shouldUpdate = true;
system.debug('CW 730 WF False...'+ objOpp.CW_in_Last_730_Days_Workflow__c);
}
if(shouldUpdate==true){
lstOpp.add(objOpp);
}
//update lstOpp;
}
if(lstOpp.size()>0)
List<Database.SaveResult> resultList = Database.update(lstOpp, false);
}
/* FINISH Method */
global void finish(Database.BatchableContext BC){
system.debug('EXECUTE Method...');
}
}
Please help us on this.
Thanks in Advance
You can bypass trigger by checking if the code that initiated the trigger is from a Batch Job, In Your trigger you can add following condition:
trigger OpportuntiyTrigger on Opportuntiy (before delete, before update, before insert, after insert, after update, after delete) {
if( System.isBatch() ) {
return;
}
// Do your trigger operations
}
Thanks
If you're satisfied with the answers provided, please don't forget to select a Best Answer.
Thanks for your reply..Actually we have an Trigger (TriggerHandler) in that trigger they were used below logic.Same logic i want to use in my Batch.Please check below bit of logic.
private static Boolean bypassTriggerCodeMain()
{
// bypass logic if custom setting is enabled for user or profile
CustomPrivilege__c cpriv_profile = CustomPrivilege__c.getInstance(UserInfo.getProfileId());
CustomPrivilege__c cpriv_user = CustomPrivilege__c.getInstance(UserInfo.getUserId())
if(cpriv_user != Null)
{
if (cpriv_user.OverrideAll__c || cpriv_profile.OverrideAll__c || cpriv_user.OverrideAccounts__c || cpriv_profile.OverrideAccounts__c)
return true;
else
return false;
}
else
return false;
}
Is there any possibility is there to bypassing the trigger when running the Batch ?Can you please help me on this.
Thanks
One quick solution is to bypass the trigger when called from batch [As I mentioned] and add the logic you want in batch class itself.
You can move above code to a util class and call the util class in the batch itself.
What i have undestood,As you mentioend above we need to create separate class and that class we need to call from Batch? Please correct me? .I have created one class like "OpportunityTrigger".So please let me know where can i call this class in my batch apex.
Thanks
Add the method in it
Mark the method as public and use the same in trigger and batch as required
Thanks for your help.I have tried but when we are trying to run the batch we are facing error like "First error: Update failed. First exception on row 20 with id 006C000000v1X0jIAE; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [End_User_Contact__c]".Can you please help me on this.
Thanks
The error shows that there is a Filter Criteria (sort of validation rule) on field End_User_Contact__c which restrict the record from being saved, go to this field and see the criteria and udpate the records again.
Thanks