1

I am trying to populate the list column with calculated value in code below.

While the alert & messages show the value being assigned to variable correctly but somehow field is not getting populated with that value.

<script type="text/javascript">
    var clientContext = null;
    var web = null;
    var count = null;
    var list ;
    ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
    function Initialize()
    {
        clientContext = new SP.ClientContext.get_current();
        web = clientContext.get_web();
        list = web.get_lists().getByTitle("Data Setup");
        clientContext.load(list);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess), 
        Function.createDelegate(this, this.onQueryFailed));
    }
    function onListItemsLoadSuccess(sender, args) {
       var count = list.get_itemCount();
        alert("Total item count in the list: " + count);
               var AutoIncremental = "MAA-SR-" + count;
               $("input[title^='*Request ID*']").val(AutoIncremental);
               $("input[title^='*Request ID*']").attr('disabled', 'disabled');

    }

    function onQueryFailed(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>
1
  • I think the issue is with your jquery selector for the input field. Can you please share the screenshot of HTML of input field by inspecting this element? I will suggest you the better selector for this. Commented Dec 10, 2020 at 3:58

2 Answers 2

1

Solution using JQuery:

Have you referenced jQuery file in your code (I suspect you haven't)? If Yes then below code should work for you:

$("input[title^='Request ID']").val(AutoIncremental);

If you have not referenced JQuery file then you can refer it like below in your HTML and then use same code as above:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Recommended is to download jQuery file, upload it in your SharePoint site and then refer it's URL in src attribute by replacing the web CDN link.

Solution using JavaScript:

If you don't want to use JQuery then you can achieve the same using plain JavaScript:

document.querySelector("input[title^='Request ID']").value = AutoIncremental;
document.querySelector("input[title^='Request ID']").disabled = "disabled";
0

Try to remove the asterisk characters around the Request ID as the below. It should work for you.

$("input[title^='Request ID']").val(AutoIncremental);
$("input[title^='Request ID']").attr('disabled', 'disabled');
1
  • I changed it as below but still no luck, it's not even giving me the second alert, is something wrong with syntax ? function onListItemsLoadSuccess(sender, args) { var count = list.get_itemCount(); var AutoIncremental = "FAM-SDSR-" + count; //alert(AutoIncremental); $("input[title^='Request ID']").val(AutoIncremental); $("input[title^='Request ID']").attr('disabled', 'disabled'); alert("X "+ AutoIncremental); } Commented Dec 10, 2020 at 14:46

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.