0

I have a SP 2013 Custom List with 2 automatically generated numbers. Currently, they are queried and populated upon selection of input values for the first 2 fields. The form works great for a single user, but I'd like to accommodate simultaneous users if possible. As is, having simultaneous users can create duplicate tracking numbers.

I tried running the queries and populating the 2 fields which are each a portion of the tracking number (calculated field) within the PreSaveAction function, which does populate the fields on the new item form, however, the values do not get saved within the list item on the AllItems page.

Any ideas on a way to make this work? Here's my code:

// Query item ID for most recent item with the selected associated tracking number
var collListItems;
function QueryExistingLastID() {
    var rvID = $("input:text[title='Associated Tracking Number']").val();
    var clientContext = new SP.ClientContext();
    var TrackerList = clientContext.get_web().get_lists().getByTitle('Tracker');
    var query = new SP.CamlQuery();
    query.set_viewXml("<View><RowLimit>1</RowLimit><Query><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Eq><FieldRef Name='Status'/><Value Type='Text'>" + rvID + "</Value></Eq></Where></Query></View>");
    collListItems = TrackerList.getItems(query);
    clientContext.load(collListItems);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.ExistingQuerySucceed), Function.createDelegate(this, this.ExistingQueryFailed));
}

// Populate subject number by adding 1 to the previous subject number for the selected associated tracking number
function ExistingQuerySucceed(sender, args) {
    var listItemEnumerator = collListItems.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();       
    }
    if ($("select[id^='Status']").val() == "Existing")
    {
    $("input[id^='Subject_x0020_Num']").val(oListItem.get_item("Subject_x0020_Num")+1);
    }
}

function ExistingQueryFailed(sender, args) {
    // Alert error message if list item above fails to load
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

// Query last list item where Status = "New"
var collListItem;
function QueryLastNewItem() {
    var clientContext = new SP.ClientContext();
    var TrackerList = clientContext.get_web().get_lists().getByTitle('Tracker');
    var query = new SP.CamlQuery();
    query.set_viewXml("<View><RowLimit>1</RowLimit><Query><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Eq><FieldRef Name='Status' /><Value Type='Text'>New</Value></Eq></Where></Query></View>");
    collListItem = TrackerList.getItems(query);
    clientContext.load(collListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        
}

// Populate new tracking number by adding 1 to the most recent tracking number
function onQuerySucceeded(sender, args) {
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();       
    }
    if ($("select[id^='Status']").val() == "New")
        {
        $("input[id^='Tracking_x0020_Numbe']").val(oListItem.get_item('Tracking_x0020_Numbe')+1);
        }
} 

function onQueryFailed(sender, args) {
    // Alert error message if list item above fails to load
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

// Validate that an appropriate Associated Tracking Number has been selected if the first field says existing
function PreSaveAction() {
    var q = document.getElementById('Status$DropDownChoice');
    var question = q.options[q.selectedIndex].text;

    var rvtracking = $("input:text[title='Associated Tracking Number']").val();
    var re_rvtracking = /^RVFUF[0-9]{2,2}-[0-9]{4,4}$/;

    if((!re_rvtracking.test(rvtracking)) && question == "Existing")
    {
    $(".ms-metadata:contains('You must search and select the associated tracking number from the drop down options.')").css("color", "FireBrick");
    return false;
    }

    else
    {
    $(".ms-metadata:contains('You must search and select the associated tracking number from the drop down options.')").css("color", "DimGray");
    // Run the queries and populate the fields as indicated above
    QueryLastNewItem();
    QueryExistingLastID();
    return true;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.