Examples

Every XML value is a string: an attribute from πŸ“• DOMElement, or a field returned by πŸ“• queryDef ExecuteQuery(). Before you can compare it, do arithmetic on it, or branch on it, you have to turn that text into a real number or boolean. JavaScript’s built-in parseInt and parseFloat are outdated on an AC server: they fall back to NaN without warning, and read a leading zero as octal: so parseInt("08") returns 0.

NL.XTK.parseInt, parseNumber, and parseBoolean are Campaign’s safe casting helpers, written for exactly this input. They validate strictly, forcing base 10, and returning the defaultReturn when the value is empty or malformed (or throwing when you omit it, so a bad value never slips through unnoticed).

parseInt

loadLibrary("/nl/core/shared/nl.js"); // import NL namespace
NL.require("/nl/core/shared/xtk.js"); // import NL.XTK namespace

logInfo(NL.XTK.parseInt("123") + " " + parseInt("123")); // 123 123
logInfo(NL.XTK.parseInt("") + " " + parseInt("")); // 0 NaN
logInfo(NL.XTK.parseInt(null) + " " + parseInt(null)); // 0 NaN
logInfo(NL.XTK.parseInt() + " " + parseInt()); // 0 Nan
// context.res11 = NL.XTK.parseInt("123abc") + " " + parseInt("123abc"); // NL.XTK.parseInt @throws "Cannot parse XTK number '123abc'."
// context.res12 = NL.XTK.parseInt(1.23) + " " + parseInt(1.23); // NL.XTK.parseInt @throws "Cannot parse XTK number '1.23'."
var query = NLWS.xtkQueryDef.create({
  queryDef: {
    schema: "nms:recipient",
    operation: "get",
    select: { node: [{ expr: "@id" }] },
  },
});
var recipient = query.ExecuteQuery(); // DOMElement
logInfo(NL.XTK.parseInt(recipient.$id) + " " + parseInt(recipient.$id)); // 1234 1234
logInfo(NL.XTK.parseInt("08") + " " + parseInt("08")); // 8 0
logInfo(NL.XTK.parseInt("010") + " " + parseInt("010")); // 10 8

See also

  • πŸ“• queryDef - Helper to run a SQL SELECT query with JSON and returns an XML element.
  • πŸ“• DOMElement - Adobe Campaign's XML element: the type returned by queryDef and SOAP calls. Can be read and traversed.
  • πŸ“• NL.is - Adobe Campaign's type helpers to check XML values.