tylerzika
3/6/2017 - 9:03 PM

Controller.java

global class assignLeadToTerritoriesScheudle implements Schedulable
{
    global void execute(SchedulableContext ctx)
    {
        String strFields = '';
        for(String strField : Schema.SObjectType.Lead.fields.getMap().keySet())
        {
            strFields += strField + ', ';
        }
        if(test.isRunningtest())
        {
            strFields = 'Id, Name, Country, State, postalcode, ';
        }
        String m_strAllLeadFields = strFields.substring(0, strFields.length()-2);
        String strFilterInQuery = 'where No_Mass_Update__c != true';
        String strQuery = 'select ' + m_strAllLeadFields + ' from Lead ' + strFilterInQuery;
        Map<Id, Lead> leadmap = new Map<Id, Lead>();
        ReassignLeadsBatch reassignLeadBatchJob = new ReassignLeadsBatch(strQuery,leadmap);
        Database.executeBatch(reassignLeadBatchJob, 1);
    }
}
@isTest
public class ManageLeadTerritoryControllerTest{
    
    @isTest static void testManageLeadTerritoryController(){
        
        Territory_Setup__c LeadTerrSetup = new Territory_Setup__c(
            Next_Mass_Update_Date_Lead__c= Date.today(),
            Status__c= 'On'
        );
        insert LeadTerrSetup;
        
        MassUpdateLeadJOBID__c newJob = new MassUpdateLeadJOBID__c (
            Name = 'Test Job'
        );
        insert newJob;

        ManageLeadTerritoryController.massUpdateNow();
        ManageLeadTerritoryController.scheduleMassUpdate();
        
    }
    
    @isTest static void testManageLeadTerritoryController2(){
        
        Territory_Setup__c LeadTerrSetup = new Territory_Setup__c(
            Next_Mass_Update_Date_Lead__c= Date.today(),
            Status__c= 'on'
        );
        insert LeadTerrSetup;
        
        MassUpdateLeadJOBID__c newJob = new MassUpdateLeadJOBID__c (
            Name = 'Lead Job',
            Job_ID__c = '123124'
        );
        insert newJob;

        ManageLeadTerritoryController.massUpdateNow();
        ManageLeadTerritoryController.scheduleMassUpdate();
        
    }  
    
     
}
public class ManageLeadTerritoryController {

    
    @RemoteAction
    public static void massUpdateNow()
    {
                
        List<Territory_Setup__c> LeadTerrSetups = [select Id, Next_Mass_Update_Date_Lead__c, Status__c from Territory_Setup__c 
                                        order by lastmodifieddate desc limit 1];
        if(LeadTerrSetups.size() > 0 && LeadTerrSetups[0].STatus__c == 'on')
        {        
            String strFields = '';
            for(String strField : Schema.SObjectType.Lead.fields.getMap().keySet())
            {
                strFields += strField + ', ';
            }
            String m_strAllLeadFields = strFields.substring(0, strFields.length()-2);
            String strFilterInQuery = 'where No_Mass_Update__c != true';
            String strQuery = 'select ' + m_strAllLeadFields + ' from Lead ' + strFilterInQuery;
            map<Id, lead> leadmap = new Map<Id, Lead>();
            ReassignLeadsBatch reassignLeadBatchJob = new ReassignLeadsBatch(strQuery, leadmap);
            Database.executeBatch(reassignLeadBatchJob, 1);
        }

        //System.debug('massUpdateNow has fired');
    }

    @RemoteAction
    public static void scheduleMassUpdate(){


        List<Territory_Setup__c> LeadTerrSetup = [select Id, Next_Mass_Update_Date_Lead__c, Status__c
                                                   from Territory_Setup__c 
                                        		   order by lastmodifieddate 
                                                   desc limit 1];
        if(LeadTerrSetup.size() > 0 && LeadTerrSetup[0].STatus__c == 'on')
        {        
        
        if(LeadTerrSetup[0].Next_Mass_Update_Date_Lead__c == null){
            System.debug('Please entry the updated date');
        }           
            
        System.savePoint savepoint = Database.setSavePoint();
        try{
            upsert LeadTerrSetup;
            
            List<MassUpdateLeadJOBID__c> jobs = [select Job_ID__c from MassUpdateLeadJOBID__c];
            //System.debug(jobs);
            for(MassUpdateLeadJOBID__c job : jobs)
            {
                if(job.Job_ID__c != null)
                {
                    //System.debug('abort is firing');
                    System.abortJob(jobs[0].Job_ID__c); 
                }
            }
            if(jobs.size() > 0)
            {                
                delete jobs;
            }
            Date massUpdateDate = [select Next_Mass_Update_Date_Lead__c from Territory_Setup__c where Id = :LeadTerrSetup[0].Id].Next_Mass_Update_Date_Lead__c;
            //System.debug(massUpdateDate);
            String year = string.valueOf(massUpdateDate.year());
            String month = string.valueOf(massUpdateDate.month());
            String day = string.valueOf(massUpdateDate.day());
            assignLeadToTerritoriesScheudle m = new assignLeadToTerritoriesScheudle();
            String sch = '0 0 1 ' + day + ' ' + month + ' ? ' + year;
            String jobID = system.schedule('Mass Update Leads', sch, m);
            MassUpdateLeadJOBID__c newJob = new MassUpdateLeadJOBID__c(Name = 'Lead JOB', Job_ID__c = jobID);
            Database.SaveResult e = Database.insert(newJob, false);
            System.debug(e);
            //insert newJob;
            //System.debug('job scheduled successfully');
        }
        catch(Exception ex){
            //System.debug(ex);
            Database.rollBack(savepoint);
        }
        }
    }     

}