Script Include Debug

reddyvenugo
Tera Contributor

Hi Team,

 

We have script Include defined using API to fetch group details from an external site, I need to debug more on this script include to check from where these group details come from and to edit data of group details as per user requirement like to add/remove existing data in it , there I can see restmessage v2 used in it,  please help me on this as early as possible.

2 REPLIES 2

harishk07
Giga Guru

Hi ,

 

A few things that should unblock you. First, a quick clarification on where the data comes from. The group details are not actually coming from the Script Include itself. They come from the REST Message that the Script Include references. If you look at the REST Message record and its HTTP Method records, that is where the endpoint URL, authentication, headers and the request are all defined. The Script Include is just invoking that REST Message and probably doing some variable substitution on it. So if you want to understand the data source, start there rather than in the script.

 

To debug, here is the easiest path:

  1. Open the REST Message record and use the Test link on the HTTP Method. This fires the call and shows you the raw request . It is the quickest way to see exactly what the external site returns.
  2. To debug the script logic itself, run the Script Include directly in Scripts - Background. Log StatusCode, Body and ErrorMessage. Logging only the final parsed result may not indicate where things actually break, so capture all three.  if the Script Include is scoped, run the background script in the same application scope, otherwise it will not behave the same way.
  3. Check the outbound HTTP log to see the actual request that left the instance. This confirms what was really sent versus what you think was sent.

On the part about editing the group data, could you clarify what you are trying to do? There are two very different things here. 

If you mean transforming the response inside ServiceNow before you use it, that is straightforward. You just parse the JSON or XML body and manipulate the object in script.

If you mean writing changes back to the external system (add or remove group data on their side), that is a separate operation. It needs a different HTTP method such as POST, PUT or PATCH defined on the REST Message, and it depends entirely on what that external API actually supports and how its contract is defined. I would confirm that before building anything.

Regards,

Harish

Tanushree Maiti
Giga Patron

Hi @reddyvenugo 

 

  • Locate the Record: Go to System Web Services > Outbound > REST Message. Look for the record name that matches the first parameter of the sn_ws.RESTMessageV2('Name', 'Method') call in your script.
  • Check the Endpoint: Inside that record, the Endpoint URL identifies the external site or service providing the group details.

 

       To see the actual data being exchanged, you can use logging.

  • : Add gs.info() or gs.log() statements to your Script Include to print the response body:

 

                  var response = r.execute();

                 var responseBody = response.getBody();

                 gs.info("DEBUG - External API Response: " + responseBody);

 

  • Check Logs: View these outputs by navigating to System Logs > System Log > All.
  • REST Message Logs: For a more detailed look at the raw JSON sent and received, check System Logs > Outbound HTTP Requests.

 

Lastly once you check ResponseBody , you can understand the data structure (usually JSON), you can edit it within the script before it is used. 

 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: