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 

DATE Field Calculation

I have the field Contract_Start date in Contract object and Monthlysalary__c in the same object. I have an Apex class where I calculate salary proposals based on fields in the Contract object. Everything works fine except when the start date is in the middle of the month I need to calculate the number of days remaining in the month and divide it with Monthly salary so that I get the correct value. Any ideas to do this?? I need to figure out the if the condition that if Contract_Startdate is not beginning of the month then calculate the number of days left in the month. I input only month and year in the VF page and retrieve all active contracts. Any Idea??

```java
public with sharing class Ct_SearchEmployeeContracts
{

    //Our collection of the class/wrapper objects cContact 
    public List<cContract> wcontracts {get; set;}
    public String month { get; set; }
    public String year { get; set; }
    List<Employee_Contract__c> sel_contracts = new List<Employee_Contract__c>();

    // 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), y2 = String.valueOf(today.year() -2), y3 = String.valueOf(today.year() -3) ,y4 = String.valueOf(today.year() +1),y5 = String.valueOf(today.year() +2),y6 = String.valueOf(today.year() +3);
        return new List<SelectOption>
        {
            new SelectOption(y0, y0),
            new SelectOption(y1, y1),
            new SelectOption(y2, y2),
            new SelectOption(y3, y3),
            new SelectOption(y4, y4),
            new SelectOption(y5, y5),
            new SelectOption(y6, y6)
        };
}
    
    public PageReference searchContracts()
    {     
       List<Employee_Contract__c> temp_contracts = new List<Employee_Contract__c>();
        
        if (month!=Null && year!=Null)
        {
           temp_contracts =[Select Id,(Select id,Name from Salary_Proposals__r), 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,MonthlySalary__c,Retirement_Contribution__c,
                            Voluntary_Retirement_Contribution__c,CurrencyIsoCode
                            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)
        { 
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.WARNING, 'No Active contracts found in the given period'));
        }
        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));
                }  
             }                                            
        }
  
        return null;         
    }

    public PageReference insertSalaryProposals() 
    {
     //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 Contact to the selectedContacts list
           system.debug(wcontracts); 
      for(cContract Ccon: wcontracts) 
      {
        if(Ccon.selected == true) 
        {
            sel_contracts.add(cCon.con);
        }  
        if(Ccon.selected == False) 
        {
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select a contract to generate salry proposals')); 
        }          
       system.debug(sel_contracts); 
      }
    try
    {
        if(sel_contracts.size()>0)
        { 
        List<Salary_Proposal__c> sals= new list<Salary_Proposal__c>();
        for(Employee_Contract__c con: sel_contracts)
        {
        system.debug(con);
         Salary_Proposal__c prop = new Salary_Proposal__c
         (Employee_Contract__c=con.id,
         Name= month+'/'+ year +'/'+ con.First_Name__c+' '+con.Last_Name__c+'/'+con.Employee_ID__c,
         Monthly_Salary__c= con.Annual_Salary__c, 
         Retirement_Contribution__c=con.Retirement_Contribution__c,
         Voluntary_Retirement_Contribution__c=con.Voluntary_Retirement_Contribution__c,
         CurrencyIsoCode=con.CurrencyIsoCode,
         Month__c= month,
         Year__c=  year
         );        
         sals.add(prop);
        }
    system.debug(sals);
    insert sals;
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Salary Proposal for the selected contracts has been generated')); 
       }
    }
    catch(DmlException e) 
    {
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Check the contract(s)')); 
     }
      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;
    }
}
public pageReference reset()    {
  //  inputId = false;
      this.wcontracts =new List<cContract>();
      return null;    }
}

```
AnudeepAnudeep (Salesforce Developers) 
My suggestion would be to create a formula field that will calculate the number of days left in the month and use it in your code. Create a formula field with any of the below formulas
 
DAY(DATE(YEAR( CloseDate ),MONTH( CloseDate )+1,1)-1) - DAY(CloseDate)

OR
 
If( IsClosed ,
DAY(DATE(YEAR( CloseDate ),MONTH( CloseDate )+1,1)-1) - DAY(CloseDate),
today() - CloseDate
)

 
Meenakshi T RMeenakshi T R
Thank you Anudeep!!! Will try it out :)