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
DipthiDipthi 

leave management app

I am new to development and I am working on Leave Management App where I need to add 4 leaves for field 'Total Leaves' on Contact Object at the end of every month for all Employees.

I am assuming I should go with Trigger. Can anyone help me with this trigger.
Best Answer chosen by Dipthi
Mohd ImranMohd Imran
Hi Deepthi,

If you want to add 4 leaves to all the employee at every month end then best approach would be Batch Class.
You can write a batch class and schedule to every month end that will run at each month end automatically and perform the logic written inside the batch class.
below is the code sample for reference.

Global class CalculateLeaveBatch implements Database.Batchable<SObject>
{
    
    Global Database.QueryLocator Start(Database.BatchableContext BC)
    {
        string contactQuery = 'select id, firstname, lastname,  total_leaves__c from contact ';
        return Database.getQueryLocator(contactQuery);
    }
    
    Global void Execute(Database.BatchableContext BC,List<SObject> contactsToProcess)
    {
      list<Contact> lstCon = new list<Contact>();
      for(SObject conRecord : contactsToProcess)    
        {
            Contact con = (Contact) conRecord;
            con.total_leaves__c = con.total_leaves__c + 4; // add the leaves here
            lstCon.add(con);
        }
        if(lstCon.size() > 0){
            Update lstCon;
        }
    }
    
    Global void Finish(Database.BatchableContext BC)
    {
        // Post logic like to send email etc...
    }
}

you can refer the below documentation of batch class for more details
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm 

All Answers

Mohd ImranMohd Imran
Hi Deepthi,

If you want to add 4 leaves to all the employee at every month end then best approach would be Batch Class.
You can write a batch class and schedule to every month end that will run at each month end automatically and perform the logic written inside the batch class.
below is the code sample for reference.

Global class CalculateLeaveBatch implements Database.Batchable<SObject>
{
    
    Global Database.QueryLocator Start(Database.BatchableContext BC)
    {
        string contactQuery = 'select id, firstname, lastname,  total_leaves__c from contact ';
        return Database.getQueryLocator(contactQuery);
    }
    
    Global void Execute(Database.BatchableContext BC,List<SObject> contactsToProcess)
    {
      list<Contact> lstCon = new list<Contact>();
      for(SObject conRecord : contactsToProcess)    
        {
            Contact con = (Contact) conRecord;
            con.total_leaves__c = con.total_leaves__c + 4; // add the leaves here
            lstCon.add(con);
        }
        if(lstCon.size() > 0){
            Update lstCon;
        }
    }
    
    Global void Finish(Database.BatchableContext BC)
    {
        // Post logic like to send email etc...
    }
}

you can refer the below documentation of batch class for more details
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm 
This was selected as the best answer
firdoss mohd 1firdoss mohd 1
Hi Deepthi,

It looks like our team of experts can help you resolve this ticket. We have Salesforce global help-desk support and you can log a case and our Customer Success Agents will help you solve this issue. You can also speak to them on live chat. Click on the below link to contact our help-desk. Trust me it is a support service that we are offering for free!

https://jbshelpdesk.secure.force.com

Thanks,
Jarvis SFDC team
DipthiDipthi

Hi Imran,

Thank you very much for your response. You are a savior !!

Sangharsh KambleSangharsh Kamble
@Dipthi which custom object you create for the leave management app