Getting "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 this a bug, are there any workarounds?

1 ACCEPTED SOLUTION

BryceG
ServiceNow Employee
ServiceNow Employee

Hi there, you need to import the namespace in sys modules as they are not just exposed in global like other scripting environments in SN.  Think of this more as typical module development.

import { RESTMessageV2 } from "@servicenow/glide/sn_ws"





View solution in original post

5 REPLIES 5

Shivalika
Mega Sage

Hello @Xerrion 

 

Actually for these cases, I would recommend - first run this in system - get the exact Python script and just convert it to "Typescript". I am not sure, But from my experience I know these languages work perfectly fine - [ServiceNow Script][cURL][Python][Ruby][JavaScript][Perl][Powershell]

 

It might very well be an issue because of Typescript and things are not accurately available for that yet. 

 

I hope this answered your query. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Xerrion 

which code is running fine in background script? share that and share screenshot of the script output

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

BryceG
ServiceNow Employee
ServiceNow Employee

Hi there, you need to import the namespace in sys modules as they are not just exposed in global like other scripting environments in SN.  Think of this more as typical module development.

import { RESTMessageV2 } from "@servicenow/glide/sn_ws"





Wow @BryceG 

 

That's a new insight for me as well. Thanks !