Monday, June 15, 2015

How to integrate Nintex Form with JSON REST service using Jquery

Problem/Requirement: In one of my SharePoint project there was requirement to integrate with ATO (ABN Service) web services to validate ABN number provided by the user to be correct and in active state. User should not be able to provide inactive or expired ABN. The validation required to be done client side, before the Nintex form is submitted and sent for approval. Once the form is submitted, a workflow kicks in and the item goes for approval.

Solution: As a solution to this requirement I developed a JSON REST service which acts as a wrapper between external ATO web service and internal SharePoint Nintex Form. This REST service queries the ATO service, get the response, parses the response into an object and then return the object in JSON format to the caller, which in this case is a Nintex Form. User gets instant validation result in an interactive manner that the whether the number provided is valid or not. The benefits of using this approach are light weight data calls, quick real-time validation and ease of parsing results in JQuery.

Steps:
1. Create a WCF JSON REST service for fetching or validating the data input more detail to create such service is here.
2. Create SharePoint custom list.
3. Access the list in SharePoint and open Nintex Form designer from the ribbon control
4. Add two label controls on the form. Set the "CSS class" value to 'lblMessage' and 'lblInstruction' respectively. These will display message to the user.
5. Include following scripts in the custom JavaScript includes in the form settings. Better to keep these files in Site Assets library to refer them easily on the forms.
      jquery-1.11.1.min.js
      FormScript.js
6. Add a button on the form and set the following properties of the control in Nintex Form.

JSON

{
    "ABN": "12345678910",
    "EffectiveFrom": "27/04/1900",
    "EffectiveTo": "1/01/0001",
    "IsValid": true,
    "Status": "Active",
}

FormScript.js

var ABNValidationServiceURL = "http://xxx-xxx-xxx/ABNValidate.svc";
var abn = $('#' + txtABNNumber).val();


function ValidateABN(){

//support cross domain ajax calls
jQuery.support.cors = true;

//URL of the JSON REST service
var url = ABNValidationServiceURL + "/IsValid/" + abn;


//query ATO service for ABN

$.getJSON(url, function (atoData, status) {
  if(status == "success"){
     if(atoData["Status"] != "null" &&  atoData["Status"] == "Active"){
$('.lblMessage').show().fadeOut(4000, "linear");;
$('.lblInstruction').css('color','green');
$('.lblInstruction').css('font-size', 'small');
$('.lblInstruction').text("Please continue filling the address and bank details.").fadeOut(4000, "linear");
}
else
{
   $('.lblMessage').show(); 
$('#' + txtABNNumber).css('border','1px solid red');
$('.lblMessage').css('color','red');
$('.lblMessage').text("Please provide a valid ABN number").fadeOut(4000, "linear");
}
}
});
}
OUTPUT:

This approach has couple of benefits:

1. Data is light weight so quite fast processing.
2. Adds more usability to the form since validation is quick and done client side.
3. Straight forward access of JSON data in JQuery and cleaner JS code.

This is just an example but there can be various other ways in which JQuery can be leveraged in conjunction with Nintex Forms to make the forms more interactive, user friendly and flexible. Hope this helps.

Cheers
Happy coding!

1 comment:

Muthu Kumaran said...

Very useful article, thanks for sharing buddy.