Calling NL.require() or any NL.* method in an Adobe Campaign JavaScript activity throws NL is not defined — the namespace is not loaded by default in workflow scripts.

Add loadLibrary("/nl/core/shared/nl.js") before any NL.* call to make the namespace available.

Preparation

Get the NL is not defined exception

10/10/2019 9:56:45 AM	js3	JST-310000 Error while compiling script 'WKF1096688/js3' line 2: NL is not defined.
NL.require("/nl/core/sql.js"); // @throws Exception

var enumName = "city";
var enumId = sqlGetDouble("SELECT iEnumId FROM xtkEnum WHERE sName=" + NL.SQL.escape(enumName));

Walkthrough

Add loadLibrary("/nl/core/shared/nl.js"); first:

loadLibrary("/nl/core/shared/nl.js");
NL.require("/nl/core/sql.js"); // OK

var enumName = "city";
var enumId = sqlGetDouble("SELECT iEnumId FROM xtkEnum WHERE sName=" + NL.SQL.escape(enumName));

Going further

All NL functions are then available:

function getEnumValue(enumName, valueName) {
  loadLibrary("/nl/core/shared/nl.js");
  NL.require("/nl/core/sql.js");

  var enumId = sqlGetDouble("SELECT iEnumId FROM xtkEnum WHERE sName=" + NL.SQL.escape(enumName));
  var value = sqlGetString(
    "SELECT sLabel from xtkEnumValue WHERE iEnumId=" +
      enumId +
      " AND sName=" +
      NL.SQL.escape(valueName)
  );
  return value;
}

getEnumValue("dictionaryLocale", "en_US"); // "English (United States)"