0

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 wrong

1

Trigger.new will contain all necessary information about Leave_Request__c. Therefore a query is not needed. On the other hand you should never query the records that are provided by trigger new.

Your records will be committed to the database after the triggers are executed. Therefore your query will return "old" values.

Just try it this way ;-) Here is a video how it works: http://youtu.be/LSOR8hBUmug

trigger AvailableLeavesTrigger on Leave_Request__c (before insert, before update)
{

    // for all new leave requests
    for(Leave_Request__c leave : trigger.new)
    {
        Leave_Request__c oldLeave = new Leave_Request__c(days_off__c = 0, type_of_leave__c = 'sick leave');
        if(!Trigger.isInsert)
        {
            oldLeave = Trigger.oldMap.get(leave.id);
        }

        System.debug('leave - > '+leave);
        // set standard values in case the user deletes the default value
        if(leave.available_casual_leaves__c == null)
        {
            leave.available_casual_leaves__c = 20;
        }
        if(leave.available_sick_leaves__c == null)
        {
            leave.available_sick_leaves__c = 10;
        }
        if(leave.days_off__c == null)
        {
            leave.days_off__c = 0;
        }
        if(leave.type_of_leave__c != 'casual leave' && leave.type_of_leave__c != 'sick leave') 
        {
            leave.type_of_leave__c = 'casual leave';
        }

        // old values

        if(oldLeave.type_of_leave__c=='casual leave')
        {
            leave.available_casual_leaves__c=leave.available_casual_leaves__c+oldLeave.days_off__c;
        }
        if(oldLeave.type_of_leave__c=='sick leave')
        {
            leave.available_sick_leaves__c=leave.available_sick_leaves__c+oldLeave.days_off__c;
        }


        // action for casual leave
        if(leave.type_of_leave__c=='casual leave')
        {
            leave.available_casual_leaves__c=leave.available_casual_leaves__c-leave.days_off__c;
        }
        // action for stick leave
        else if(leave.type_of_leave__c=='sick leave')
        {
            leave.available_sick_leaves__c=leave.available_sick_leaves__c-leave.days_off__c;
        }
    }
}
| improve this answer | |
  • no its not updaing field @christian Deckert , its not showing any debug logs – Salesforce developer May 29 '14 at 7:08
  • I've changed the code above. Let's try it again. – Christian Deckert May 29 '14 at 7:13
  • no use still remaining in the same way – Salesforce developer May 29 '14 at 7:15
  • Just have a look at the video I've posted on youtube. I think it works as intended: youtu.be/LSOR8hBUmug – Christian Deckert May 29 '14 at 7:28
  • for me days_off__c is formula field which will calculate difference between two dates. you trigger is working fine but the issue is. Lets say available casual leaves as 20 for the first time and it is reduced by 3 days off. now the avaiable casual leaves will be 17 for the next record right. but for i am getting 20 as again when i want to insert new record. – Salesforce developer May 29 '14 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.