0

New at creating JavaScript Code in SharePoint.

I'm trying to create a customize default value input on my NewForm.aspx.

It's based on counting the number of entries so far (including 0) and then taking that value, add with some text ("RT-1" for example) and have it the default value for the next entry, so it will be incremental. Also, I'll have to create a condition that, if there are no items in a list that it starts at 0 count. Is this possible? Here's what I wrote so far but nothings working:

<script src="/siteassets/jsLib/jquery-3.3.1.min.js" type="text/javascript"></script>
<script type="text/javascript">

$SP().list("FormTest").get({fields:"ID"}, function(data) {
  var countData=data.length;
$("input[Title='AutoNumb Required Field']").val("RT-"+countData);

});

</script>
0

Yes, this is possible provided you run an additional method first that returns the count of entries in that particular list, more specifically a synchronous REST API call. In case the list entries are blank, it can start from 0 as per your requirement. Here is the code below -

<script src="/siteassets/jsLib/jquery-3.3.1.min.js" type="text/javascript"></script>
<script type="text/javascript">

 var countData = getListEntryCount("FormTest")

 $SP().list("FormTest").get({fields:"ID"}, function(data) {
       $("input[Title='AutoNumb Required Field']").val("RT-"+countData);

  });

function getListEntryCount(listname)
{
    var totalEntries = 0;
    var myURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle(' "+ listname +" ')/Items?$top=5000";
    $.ajax({
    url: myURL,
    async: false,
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
        if (data.d.results.length > 0) {
            totalEntries = data.d.results.length;
        }
    },
    error: function (error) {
        alert("Error: " + JSON.stringify(error));
    }
});
return totalEntries;
}

</script>
  • I thanks for your quick reply. I tried it but for some reason, the value isn't showing up in the textbox of my form. Is my portion of the code correct specifically "function(data) {" //sixth line? – adams-j Mar 12 at 10:50
  • try using alerts if you are getting the count first.. use alert(countData) after the 2nd line and see.. – Satyajit Paul Mar 12 at 11:01
  • yeah, alerts not even working so I may have to look at why my script isn't running. The only thing is if just add $("input[Title='AutoNumb Required Field']".val("RT-"+1);, it will show. – adams-j Mar 12 at 13:03
  • I was finally able to get the above to work. Just forgot to add function keyword. Then modified it a bit to get the last entry'd ID number then just do an increment from that. Thanks for pointing me in the right direction! – adams-j Mar 13 at 17:42
  • glad it worked! do upvote this answer :) – Satyajit Paul Mar 13 at 18:10

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.