function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Meenakshi T RMeenakshi T R 

Batch class suggestion

I need an idea. I have VF page and controller. When I enter Year and Month in the VF page I get the all the records that are active from an object called contract. I have also included the checkbox script in my code and controller has a wrapper list that holds the id's of selected contracts.
My main concern is to to pass the selcted contracts id to a batch class where I should calculate the fields of another object that has master detail relation with contract object. Is it possible? Any ideas??

My Controller code is

Code on 17/08/2020--- Before batch class execution


public with sharing class Ct_SearchEmployeeContracts
{

    //Our collection of the class/wrapper objects cContract 
    public List<cContract> wcontracts {get; set;}
    public String month { get; set; }
    public String year { get; set; }

    // Constructor
    public Ct_SearchEmployeeContracts(){  
        year      = String.valueOf(System.today().year());
        month     = String.valueOf(System.today().month());
      //  contracts = new List<Employee_Contract__c>();
        wcontracts= new List<cContract>();
    }
    public List<SelectOption> getyearList () 
    {
        Date today = System.today();
        String y0 = String.valueOf(today.year()), y1 = String.valueOf(today.year() -1);
        return new List<SelectOption>
        {
            new SelectOption(y0, y0),
            new SelectOption(y1, y1)
        };
}    
    public PageReference searchContracts()
    {     
       List<Employee_Contract__c> temp_contracts = new List<Employee_Contract__c>();
        
        if (month!=Null && year!=Null)
        {
           temp_contracts =[Select Id, Name, First_Name__c, Last_Name__c, Employee_ID__c,Contract_End_Date__c,Contract_End_Month__c, Contract_End_Year__c,Contract_Start_Date__c,Contract_Start_Month__c, Contract_Start_Year__c,Active__c
                            from Employee_Contract__c 
                            where Active__c = True AND
                            Contract_Start_Year__c <= :year AND Contract_End_Year__c >= :year ];
                            system.debug('Temp contracts size:' +temp_contracts);
        }
         System.debug('tEMP_cONTRACTS SIZE:' + temp_contracts); 
        
        if (temp_contracts.size() > 0)
        {
            for (Employee_Contract__c tc : temp_contracts)
            {
                Integer given_year_month = (Integer.valueOf(year) * 100) + Integer.valueOf(month);
                Integer tc_contract_end = ((Integer.valueOf(tc.Contract_End_Year__c)) * 100) + Integer.valueOf(tc.Contract_End_Month__c);
                Integer tc_contract_start=((Integer.valueOf(tc.Contract_Start_Year__c)) * 100) + Integer.valueOf(tc.Contract_Start_Month__c);
                system.debug('given_year_month:' +given_year_month);
                  system.debug('tc_contract_end :' +tc_contract_end );
                if (tc_contract_end >= given_year_month && tc_contract_start<=given_year_month )
                {
                    wcontracts.add(new cContract(tc));
                }
                else{
                     ApexPages.addMessage(new apexpages.message(ApexPages.Severity.WARNING, 'No contracts found in the given period'));
                }    
             }                                            
        }
  
        return null;         
    }
    public PageReference generateSalaryProposals() 
    {
        //We create a new list of Contracts that we be populated only with Contracts if they are selected
         List<Employee_Contract__c> sel_contracts = new List<Employee_Contract__c>();
        //We will cycle through our list of Contracts and will check to see if the selected property is set to true, if it is we add the Contract to the selectedContracts list
       for(cContract Ccon: wcontracts) {
        if(Ccon.selected == true) {
            sel_contracts.add(cCon.con);
        }
    }
        for(Employee_Contract__c con: sel_contracts) {
        system.debug(con);
    }
        wcontracts=null;
        return null;
    }
    public class cContract {
    public Employee_Contract__c con {get; set;}
    public Boolean selected {get; set;}

    //This is the contructor method. When we create a new cContract object we pass a Contract that is set to the con property. We also set the selected value to false
    public cContract(Employee_Contract__c c) {
        con = c;
        selected = false;
    }
}
}