I'm Having a Batch Class which I want to Run For Current Record On Screen .I'm Calling the Batch Class using Trigger on updation of a field.Is there any Way,i can run the Batch Class for that particular record on which field has been updated .
Batch Class -->
global with sharing class VGA_AssignDealertoPreLaunchModels implements Database.Batchable<sObject>,Database.Stateful{
Integer startTime = DateTime.newInstance(2007, 6, 23, 8, 3, 3).hour();
Integer currentHour = system.now().hour();
Integer endHour = DateTime.newInstance(2007, 6, 23, 19, 3, 3).hour();
list<lead> lstLead2update = new list<lead>();
global Database.querylocator start(Database.BatchableContext bc)
{
set<string> setFieldsLead = new set<string>
{
'id','VGA_Pre_launch_Model__c','VGA_Start_Day__c','VGA_End_Day__c','VGA_Number_of_Leads_per_Distribution__c',
'VGA_Time_Interval__c','VGA_Status__c'
};
string strLeadQuery = 'Select ';
for(String strFields : setFieldsLead)
{
strLeadQuery += strFields + ',';
}
strLeadQuery = strLeadQuery.removeEnd(',');
strLeadQuery += ' from VGA_Pre_Launch_Configuration__c' ;
return Database.getQueryLocator(strLeadQuery);
}
global void execute(Database.BatchableContext BC, List<VGA_Pre_Launch_Configuration__c> lstPreLaunchConfiguration)
{
if(lstPreLaunchConfiguration != null && !lstPreLaunchConfiguration.isEmpty() && currentHour>=startTime && currentHour<=endHour)
{
set<id> setModel = new set<id>();
Double noOfLeads = 0;
for(VGA_Pre_Launch_Configuration__c objPLC : lstPreLaunchConfiguration)
{
if(objPLC.VGA_Pre_launch_Model__c != null
&& (objPLC.VGA_Status__c.equalsignorecase('New') || objPLC.VGA_Status__c.equalsignorecase('In Progress')))
{
setModel.add(objPLC.VGA_Pre_launch_Model__c);
noOfLeads = objPLC.VGA_Number_of_Leads_per_Distribution__c != null?objPLC.VGA_Number_of_Leads_per_Distribution__c:0;
}
}
if(setModel != null && !setModel.isEmpty() && noOfLeads>0)
{
list<lead> lstLead = Database.query('select id from Lead where VGA_Model1__c in:'+setModel+' limit '+noOfLeads); ////Need to add all the Criterias and Fields
if(lstLead != null && !lstLead.isEmpty())
{
map<id,list<lead>> mapModelWrtLeads = new map<id,list<lead>>();
for(Lead objLead : lstLead)
{
if(!mapModelWrtLeads.containskey(objLead.VGA_Model1__c)
&& objLead.VGA_Model1__c != null && objLead.VGA_is_pre_launched_Vehicle__c)
mapModelWrtLeads.put(objLead.VGA_Model1__c,new list<lead>());
mapModelWrtLeads.get(objLead.VGA_Model1__c).add(objLead);
}
if(mapModelWrtLeads != null && !mapModelWrtLeads.isEmpty())
{
list<Lead> lstProfanityFilteredLeads = new list<Lead>();
list<Lead> lstSpamFilteredleads = new list<Lead>();
list<Lead> listofassignDealership = new list<Lead>();
//list<Lead> lstProfanityFilteredLeads = new list<Lead>();
for(VGA_Pre_Launch_Configuration__c objPLC : lstPreLaunchConfiguration)
{
if(objPLC.VGA_Pre_launch_Model__c != null && objPLC.VGA_Number_of_Leads_per_Distribution__c != null
&& objPLC.VGA_Number_of_Leads_per_Distribution__c >0 && mapModelWrtLeads.containskey(objPLC.VGA_Pre_launch_Model__c)
&& mapModelWrtLeads.get(objPLC.VGA_Pre_launch_Model__c) != null && mapModelWrtLeads.get(objPLC.VGA_Pre_launch_Model__c).size()>0)
{
/*for(Lead objLead : mapModelWrtLeads.get(objPLC.VGA_Pre_launch_Model__c))
{
//Do the operations on Lead and mark it as being processed
lstLead2update.add(objLead);
}
*/
lstProfanityFilteredLeads = VGA_LeadTriggerHandler.checkProfanity(mapModelWrtLeads.get(objPLC.VGA_Pre_launch_Model__c),true);
if(lstProfanityFilteredLeads != null && !lstProfanityFilteredLeads.isEmpty())
{
lstSpamFilteredleads = VGA_LeadTriggerHandler.checkSpam(lstProfanityFilteredLeads,true);
if(lstSpamFilteredleads != null && !lstSpamFilteredleads.isEmpty())
{
listofassignDealership = VGA_LeadTriggerHandler.assignDealership(lstSpamFilteredleads,true);
if(listofassignDealership != null && !listofassignDealership.isEmpty())
{
VGA_LeadTriggerHandler.userAssignment(listofassignDealership,true);
}
}
}
objPLC.VGA_Status__c='In Progress';
}
}
}
}
else
{
for(VGA_Pre_Launch_Configuration__c objPLC : lstPreLaunchConfiguration)
{
if(objPLC.VGA_Status__c == 'New' || objPLC.VGA_Status__c =='In Progress')
{
objPLC.VGA_Status__c='Completed';
}
}
}
}
}
}
global void finish(Database.BatchableContext BC)
{
if(lstLead2update != null && !lstLead2update.isEmpty())
{
try
{
update lstLead2update;
}
catch(exception e)
{
}
}
}
}