InfoPath: A proper way to blank out Whole Number, Date using JScript

By | April 29, 2010

Problem:

When you blank out data in InfoPath using JScript, you would probably use the following method.

XDocument.DOM.selectSingleNode("my:xpath/my:node").text = "";

Above code works well when “my:node” is of data type String. If the data type is Number or Date, above code will work but on the user interface you will see an error “Only integers allowed.” and “Only date allowed.”

Only integer allowed

Solution:

This problem is caused because Number and Date type node must have extra “xsi:nil” attribute set if node is blank. When you are removing any values from user interface, InfoPath automatically adds this attribute but when you are doing it manually, you have to manually set this attribute. This is how you would do it.

function GetNilAttribute()
{
var xmlNil = XDocument.DOM.createNode(2, "xsi:nil", "http://www.w3.org/2001/XMLSchema-instance");
xmlNil.text = "true";
return xmlNil;
}

var nodeToSet = XDocument.DOM.selectSingleNode("my:xpath/my:node");
nodeToSet.text = "";
nodeToSet.setAttributeNode(GetNilAttribute());

You may download the InfoPath template example to properly blank out Number, Date using JScript.

function GetNilAttribute() { var xmlNil = XDocument.DOM.createNode(2, “xsi:nil”, “http://www.w3.org/2001/XMLSchema-instance”); xmlNil.text = “true”; return xmlNil; }

Leave a Reply

Your email address will not be published. Required fields are marked *