0

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)
            {

            }
        }
    }
}
2
  • Why do you want to run a Batch for just one record (since the on-screen record)?
    – Mahmood
    Commented Nov 6, 2017 at 7:55
  • As That particular Record is used to specify No. of Leads to process and Filter Criteria For the Leads.I'm adding my Batch Class in my Question too.
    – Rishi
    Commented Nov 6, 2017 at 8:35

2 Answers 2

0

You could modify the batch class to support an optional Id value to add to the query in the start method:

global with sharing ...

    public Id specificId;

    global Database.querylocator start(Database.BatchableContext bc) {

        strLeadQuery += ' from VGA_Pre_Launch_Configuration__c' ;
        if (specificId != null) {
            strLeadQuery += ' where Id = :specificId';
        }
        return Database.getQueryLocator(strLeadQuery);
    }

    ...
}

and set that field before the batch is executed.

0
0

Thanks Keith For the Idea. I took Another Variable and Defined it in Constructor of Batch Class and Passing ID From my Trigger.

static string recordId;
public VGA_AssignDealertoPreLaunchModels(string recordId)
{
    recordId = recordId;
}
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 and id =:recordId' ; 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .