2

I am creating a leave system.

There are fields like:

  1. Type of leave as text
  2. From date as date
  3. To date as date and there is a calculated column named 'RequestedDays' which calculates the number of leave days requested excluding weekends.

Now, I need a field in the edit form which dynamically updates to 8*'RequestedDays' as soon as requestor enters his From date and To date.

I would also like to edit this 'leave hours' field manually and then submit to my manager for approval. How can I do this?

  • by the way I am using Sharepoint-online – Sahil Tiwari Jan 15 at 20:33
0

The following example code for your reference.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    var $fromDate=$("input[title='From date']");
    var $toDate=$("input[title='To date']");
    var $leaveHours=$("input[title='leave hours']");
    $leaveHours.val(getLeaveHours($fromDate.val(),$toDate.val()));  
    $fromDate.get(0).onvaluesetfrompicker = function(){
        $leaveHours.val(getLeaveHours($(this).val(),$toDate.val()));
    };
    $toDate.get(0).onvaluesetfrompicker = function(){
        $leaveHours.val(getLeaveHours($fromDate.val(),$(this).val()));
    };
})
function getLeaveHours(fromDate,toDate){
    var count = 0;
    var startDate = new Date(fromDate);
    var endDate = new Date(toDate);
    while (startDate <= endDate) {
        var dayOfWeek = startDate.getDay();
        if(!((dayOfWeek == 6) || (dayOfWeek == 0)))
           count++;
        startDate.setDate(startDate.getDate() + 1);
    }
    return count*8;
}
</script>

enter image description here

  • But how can I integrate this js code into sharepoint-online? I am unable to find any html file that can display my form layout. Do you know how to do this in power-apps? – Sahil Tiwari Jan 16 at 13:34
  • If you use modern UI in list form, we can't add JavaScript code to achieve it. We can't use JavaScript code in PowerApps. – LZ_MSFT Jan 17 at 1:33
0

I got the answer. It can be done in Powerapps where you can customize your form. I added that 'Calculated Hours' field and was able to get result dynamically. Here are following threads that can help:

https://www.youtube.com/watch?v=Bse4fBO2orE

https://www.youtube.com/watch?v=fqH1U0_TanI

https://powerapps.microsoft.com/hi-in/blog/excluding-weekends-and-holidays-in-date-differences-in-powerapps/

Note that the sharepoint formulas are not the same as Powerapps formulas

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.