0

I'm working on leave request SharePoint form. I have two lists :

  • a first list (list1) will record the request of form submission and calculate the remaining leave
  • then update the second list (list2) with the column emp_name,manager,balance leave.

I want to do is :

For example if an employee has a leave balance of 6 in list2 then when i open a new form and pick up the employee_name then the leave balance of 6 for that employee should show under "balance leave" . Is it possible to achieve it?

Field Type of remaining balance enter image description here

3 Answers 3

3

The example below for your reference, when use enter the value in emp_name field and blur, the value will auto fill in the balance leave field.

Add the code below into script editor web part in new form page in list2.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    var listname = "list2";
    $("input[title='emp_name']").blur(function(){
        var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listname + "')/items?$filter=emp_name eq '"+$(this).val()+"'";
        $.ajax({
            url: url,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                if(data.d.results.length>0){
                    var item = data.d.results[0];
                    $("input[title='balance leave']").val(item.balance_x0020_leave);
                }
            },
            error: function (data) {
            }
        });
    }); 
});
</script>

enter image description here

enter image description here

If the emp_name field is a lookup field, we need use the code below.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    var listname = "list2";
    $("select[title='emp_name']").change(function(){
        var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listname + "')/items?$filter=emp_name eq '"+$(this).val()+"'";
        $.ajax({
            url: url,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                if(data.d.results.length>0){
                    var item = data.d.results[0];
                    $("input[title='balance leave']").val(item.balance_x0020_leave);
                }
            },
            error: function (data) {
            }
        });
    }); 
});
</script>
8
  • Thank you for your answer brother, i very new to share point , may i know where i can enter the script you have mentioned?
    – Biswa
    Nov 28, 2018 at 9:05
  • LZ_MSFT Could you please show me with screenshots on where i need to add the script please?
    – Biswa
    Nov 28, 2018 at 9:08
  • Add code into script editor web part in newform.aspx page.
    – LZ_MSFT
    Nov 28, 2018 at 9:11
  • i know im doing it wrong. but i couldnt make it work. I have entered the script then selected the emp_name on list1 form but it doesnt picks up the remaining balance
    – Biswa
    Nov 28, 2018 at 9:26
  • Check the updated code. If still not works, I suggest you provide the screenshots about your form and fields type.
    – LZ_MSFT
    Nov 28, 2018 at 9:36
0

You can reach that througt REST API. Try to get the Balance leave depending in the employee control with a rest api query. then store the result in a variable and play with it with rules.

3
  • @ahemed Bauchaala , not sure whats that. its my second week working on share point. Can you give me an example on how i can achieve with REST API please?
    – Biswa
    Nov 27, 2018 at 11:25
  • Here an exemple johnliu.net/blog/2012/4/26/… Nov 27, 2018 at 13:03
  • You are welcome, give a vote if you found that it was helpfull Nov 30, 2018 at 16:09
0

I think you need to replace following line in LZ_MSFt code

$("input[title='balance leave']").val(item.balance_x0020_leave);

replace with:

$("input[title='balance leave']").val(item.Remaining_x0020_balance);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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