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
Sandeep M 1Sandeep M 1 

write trigger on before update for a custom field

Hi, 

I had three custom fields like available_casual_leaves__c,available_sick_leaves__c and days_off __c on leave_request__c object.
the default values of available_Casual_leaves__c is 20 and available_sick_leaves__c is 10. Now i have to update the default values in both available_casual_leaves__c and available_sick_leaves__c. for that i am writing

trigger availableLeavesTrigger on Leave_Request__c (before Update) {
    List<Leave_Request__c> lr= new List<Leave_Request__c>();
    List< Leave_Request__c> IdsForUpdate = new List< Leave_Request__c>();
    Leave_Request__c lqr=new Leave_Request__c();
    List<leave_request__c> leaveList=new LIST<leave_request__c>();   
    if(trigger.isUpdate)
    {
        for(Leave_Request__c l:trigger.new)
        {
            IdsForUpdate.add(l);
        }       
    }
    for(leave_request__C leave:[Select id,name, type_of_leave__c,Available_casual_leaves__c,
        Available_sick_leaves__c,  days_off__c from Leave_Request__c where id=:IdsForUpdate])
    {
       System.debug('isjfh 0 -- > '+leave);
        if(leave.type_of_leave__C=='casual leave')
        {
            System.debug('0 -- > '+leave);
            leave.available_casual_leaves__c=leave.available_casual_leaves__c-leave.days_off__c;
            System.debug('1 -- > '+leave.available_casual_leaves__c);
            leaveList.add(leave);          
        }
        else if(leave.type_of_leave__c=='sick leave')
        {
            leave.available_sick_leaves__c=leave.available_sick_leaves__c-leave.days_off__c;
            System.debug('2 -- > '+leave.available_sick_leaves__c);
            leaveList.add(leave);
        }
      }
      update leavelist;
  }

but i am not able to see any debug logs and ofcourse the values are not updating could any one tell me where i am going wroing
Vinit_KumarVinit_Kumar
What do you mean by Logs are not getting generated??

Your trigger is on update event,whenever you will update Leave_Request__c record,trigger will fire and logs will be generated.

Are you updaing or inserting record ??
Daniel B ProbertDaniel B Probert
pretty sure it's easier to do what your doing here with a workflow field update. seeing as your still updating the same record i don't feel like a trigger is needed for this one.

i've built a similar app in my org and have ended up seperating this information out of the leave request object but creating a leave allowance object that is the master relationship to the leave request. then i have a couple of roll up summary fields that calculate this detail for me.

This left me with

User > Leave_Allowance__c > Leave_Request__c

Can replace user with your own custom hr employee object if wanted.


Sandeep M 1Sandeep M 1
@vinit_kumar i want to do it both
Vinit_KumarVinit_Kumar
You need to add insert event in your Trigger operation to execute it on insertion of record ,just put a before insert event and then have another condition like

if(Trigger.Isinsert())
{
     // your logic
}
Sandeep M 1Sandeep M 1
@arun kumar what in the case of insert ?
Sandeep M 1Sandeep M 1
@arun kumar it s working fine, the main issue is when i insert new record the available_casual_leaves__c is 20 by default and then days_off__C is getting subtracted from 20. but i has to get the previously updated available_casual_leaves__c and then subtract the days_off__c 
Shashikant SharmaShashikant Sharma
Hi,

Use this.
trigger availableLeavesTrigger on Leave_Request__c (before Update) {
    if(trigger.isUpdate)
    {
        for(Leave_Request__c leave : trigger.new)
        {
           if( leave.type_of_leave__c != null ) { 
            if(leave.type_of_leave__c.equalsIgnoreCase( 'casual leave') )
           {
            leave.available_casual_leaves__c=leave.available_casual_leaves__c-leave.days_off__c;
           }
           else if(leave.type_of_leave__c.equalsIgnoreCase( 'sick leave') )
           {
            leave.available_sick_leaves__c=leave.available_sick_leaves__c-leave.days_off__c;
           }
            }
        }       
    }
}
Thing to understand :

1. You do not need to update the record in before update or before insert triggers.
2. I used equalsIgnoreCase method to check just to make sure Case Sensitive value.
3. In case you get any null pointer exception add null checks.

let me know if still face issue.
Daniel B ProbertDaniel B Probert
not sure this trigger is going to give him the previously taken leave_requests?

wouldn't you need to do a lookup on the object for the last leave_request to copy over the value.

i.e. 

User submits record 1 for 3 days - default value is 20 so leave days change to 17
User submits record 2 for 3 days - default value is stil 20 so leave days changes to 17

i think what you want is for the leave days to aggregate so that you see the total for record 1 and record 2 so the leave days for the example should = 14
Sandeep M 1Sandeep M 1
@daniel you were right. but cant we do that in a trigger with out aggregate ?
Daniel B ProbertDaniel B Probert
you can do it with aggregate i just found that with a couple of additional business requirements i needed more than one object to easily manage it.

Just to explain some of my Additional Requirement:

1. Each year the allowance resets back to 20 
2. For every 5 years of employment your allowance increase by 2 if your above a certain level 
3. Each year you can carry over up to 5 days leave which need to be taken by march or you loose them
4. If someone cancels their leave the total should reflect based on the status changed to cancelled on the object - equally this is the same for the approval process if a leave request is rejected it should not reflect in available days.

dan




Lakshmi Prasanna 9Lakshmi Prasanna 9
paste all the remaining code inside the if block of if is update and check its once is it working or not
Lakshmi Prasanna 9Lakshmi Prasanna 9
your code is wrong. In your query  in where condition you passed a list this will not work.


trigger availableLeavesTrigger on Leave_Request__c (before Update) {
    List<Leave_Request__c> lr= new List<Leave_Request__c>();
     Leave_Request__c lqr=new Leave_Request__c();
    List<leave_request__c> leaveList=new LIST<leave_request__c>();  
    if(trigger.isUpdate)
    {
        for(Leave_Request__c leave:trigger.new)
        {
           System.debug('isjfh 0 -- > '+leave);
          if(leave.type_of_leave__C=='casual leave')
          {
            System.debug('0 -- > '+leave);
            leave.available_casual_leaves__c=leave.available_casual_leaves__c-leave.days_off__c;
            System.debug('1 -- > '+leave.available_casual_leaves__c);
            leaveList.add(leave);         
          }
          else if(leave.type_of_leave__c=='sick leave')
          {
            leave.available_sick_leaves__c=leave.available_sick_leaves__c-leave.days_off__c;
            System.debug('2 -- > '+leave.available_sick_leaves__c);
            leaveList.add(leave);
          }
        }
      update leavelist;
}
Sandeep M 1Sandeep M 1
@lakshmi prasanna we cant update trigger.new or trigger.old values directly right

Lakshmi Prasanna 9Lakshmi Prasanna 9
see this link this may be helpful for you...

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htm
Lakshmi Prasanna 9Lakshmi Prasanna 9
remove that 'update leavelist ' and check it once
Lakshmi Prasanna 9Lakshmi Prasanna 9
You also must not call the update DML in the trigger as all data will be committed by salesforce after the trigger completes.If we want we can simply change the value.