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
Karthik jKarthik j 

I am having total leaves, applied leaves and remaining leaves as fields in my Leave custom object and remaining leaves is a formula field

I am having total leaves, applied leaves and remaining leaves as fields in my Leave custom object and remaining leaves is a formula field. I am trying to get remaining leaves every time applied leaves changes i want remaining leaves field to be updated.
i used this formula but  i am getting syntax error. If anyone having any idea how to resolve it please help me.​​​​​​​User-added image
ShirishaShirisha (Salesforce Developers) 
Hi Karthik,

Greetings!

You can simply use the substraction to get the remaining leaves as below:
 
Total Days - Applied Leaves

which will update the Remaining leaves field while saving the record.

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri
Karthik jKarthik j
Hey Shirisha , thank you for your reply.
If i subtract applied leaves from total leaves(days) like you suggested, I'll get total remaining leaves, Next time when applied leaves changes i want it to subtract from remaining leaves not again from total leaves and finally if applied leaves is greather than remaining leaves i have show one error message.I hope now you got my question clearly. If you have any answer for this, please help me. 
Suraj Tripathi 47Suraj Tripathi 47

Hi Karthik,

As per your requirement you want to update remainigLeave after changing applied field value ,so for this purpose you can use trigger instead of formula field.
Solution is given below:

if(trigger.isBefore && trigger.isUpdate){
        remainingLeaves.updateRem(trigger.new);
    }


 for(Leave__c lc:LeaveList){
          lc.RemainingLeaves__c=lc.totalLeaves__c-lc.appliedLeaves__c;
          lc.totalLeaves__c=lc.totalLeaves__c-lc.appliedLeaves__c;
        }
 

 Please mark it as Best Answer,if it helps!

Thanks