paul.crawford

Ensure Unique Reference On New Item

Blog Post created by paul.crawford Champion on Feb 2, 2017

Prompted by Cassy Freeman and this question raised by Chintan Desai, 2 workflow instance executes at same time, I thought I would share how I got around this very problem.

My scenario is slightly different as I wanted the new reference number displayed on the new form for users info even before the record saved. Initially i thought us JavaScript to get the next number from the GenerateID list and then use workflow on save to increment the next number.

This actually made Murphy's law (read the rest of the comments on the question!!) more likely to happen which was pointed out to me by Cassy Freeman in a workshop so i had to come up with something else.

 

Solution:

When the new form is loaded use Javascript to get the next number and then increment by one and then write back to the GenerateID list immediately. This will ensure that the next number will be different and therefore no chance of Murphy getting his way!!!

 

The fun part:

Title field set to have a Client ID JavaScipt variable of varReference.

Title field has a rule on it to be disable when populated using the formula not(isNullOrEmpty({Self})).

Second field on the form set to have a Client ID JavaScipt variable of varSecondField. (Change as required)

 

The really fun part (!?!?!):

Custom JavaScript to do the rest.

//Ensure form is initialised
NWF.FormFiller.Events.RegisterAfterReady(function () {
    NWF$(document).ready(function () {
        //Ensure required files loaded
        ExecuteOrDelayUntilScriptLoaded(GetNextNumber, 'sp.js');
    });
});

function GetNextNumber() {
    //Get reference value
    var reference = NWF$('#' + varReference).val();
    //Ensure current reference is empty (new item)
    if (reference == '') {
        jQuery('#errorMsg').empty();
        jQuery('#outputData').empty();
        //Get Next Number from GenerateID list
        var requestUri = _spPageContextInfo.webAbsoluteUrl + '/_api/Web/Lists/getByTitle(\'GenerateID\')/items?$select=Id,Title';
        try {
            jQuery.ajax({
                url: requestUri,
                type: 'GET',
                headers: { 'ACCEPT': 'application/json;odata=verbose' },
                success: ReferenceGetSuccess,
                error: ReferenceGetError
            });
        }
        catch (err) {
            jQuery('#errorMsg').html('getListData Error: ' + err);
        }
    }
}

function ReferenceGetSuccess(data) {
    //Ensure data returned
    if (data.d.results.length > 0) {
        //Update Title column to the Next Number
        NWF$('#' + varReference).val(data.d.results[0].Title);
        //Give Title column focus
        NWF$('#' + varReference).focus();
        //Give second field focus to invoke the rule on the Title field to disable if populated
        NWF$('#' + varSecondField).focus();
        //Convert next number value to a number
        var nextNumber = parseInt(data.d.results[0].Title);
        //Increment the number by 1
        nextNumber = nextNumber + 1;
        //Get current context
        var ctx = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);
        //Get the GenerateID list
        var oList = ctx.get_web().get_lists().getByTitle('GenerateID');
        //Get the item by id
        var oListItem = oList.getItemById(1);
        //Udpdate the item to the incemented number
        oListItem.set_item('Title', nextNumber);
        //Commit the update
        oListItem.update();
        ctx.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
    }
}

function ReferenceGetError() {
    alert('Failed to load Lookup Items');
}

function onQuerySucceeded() {
    //Do post processing if required
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

 

Now you have an immediately incrementing number so duplicates can be avoided.

 

Video: Writing Custom JavaScript In Nintex Forms for SharePoint

 

Hope this helps.

Outcomes