"sn_ws" is not defined. When running a Business Rule script created with the SDK

Xerrion
Tera Contributor

I am trying to use the RESTMessageV2 when developing using the ServiceNow SDK. The project is made with TypeScript, and everything works but the sn_ws namespace.

 

The is the fetchWarranty function that should be used. All of the code is running when using the Background Scripts.

import { gs } from "@servicenow/glide";

/**
 * Fetches warranty information for a given device based on its serial number.
 *
 * This function utilizes the "Lenovo Warranty API" to retrieve the warranty details
 * for a specific hardware device. It constructs and executes a REST API request by
 * setting required headers and parameters.
 *
 * @Param {string} serialNumber - The serial number of the device for which the warranty information is to be fetched.
 * @returns {any} The response from the Lenovo Warranty API, or undefined if the API call fails.
 *
 * @throws Will log an error message if the REST API call fails due to exceptions.
 */
export const fetchWarranty = (serialNumber: string) => {
  try {
    const request = new sn_ws.RESTMessageV2(
      "APP_NAME.Lenovo Warranty API",
      "getWarranty",
    );
    request.setRequestHeader(
      "ClientID",
      gs.getProperty("APP_NAME.client_id"),
    );
    request.setStringParameterNoEscape("serialNumber", serialNumber);

    return request.execute();
  } catch (ex) {
    const message = (ex as Error).message;
    gs.error("REST call failed: " + message);
    return;
  }
};

I've tried importing sn_ws or RESTMessageV2 from @Servicenow/glide, but no effect.

Neither VSCode (with all relevant plugins installed) or WebStorm can autocomplete the import so it seems that it might be missing from the SDK. And as I can understand you cannot use fetch as its not allowed to use Async/Await/Promises. Is there a workaround, or do I have to use XMLHttpRequest?

0 REPLIES 0