You need to sign in to do that
Don't have an account?
Sandeep 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
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
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 ??
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.
if(Trigger.Isinsert())
{
// your logic
}
Use this. 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.
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
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
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;
}
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htm