how-to-use-chatgpt-servicenow-javascript

How to use ChatGPT to generate ServiceNow JavaScript code

  • Post category:ESM / Servicenow
  • Reading time:8 mins read
  • Views:
    756 Views
  • Post comments:0 Comments

In this article, I will show you how I used ChatGPT to generate ServiceNow JavaScript code for a Business rule. This use case can be applied to other coding areas such as client scripts, script includes, jelly scripts, and scheduled jobs. If you are interested in the other integration possibilities of ChatGPT and ServiceNow, you can read this article.

In the following use case, we will generate code to ensure that no change record is closed until all its related change tasks are closed. We will use the “name” field of the business rule record to define our coding requirement. For example:

Generate ServiceNow business rule code that only allows change closure if all related change tasks are closed.

Once the business rule is named and its conditions are updated, the integration will trigger the code generation in the “Script” field of the business rule’s “Advanced” tab.

NOTE: You may need to adjust the generated code to meet your specific requirements. The more clear and precise your requirement is, the better the code generated by ChatGPT will be, and the less modification will be required.

6 steps to Integrate ServiceNow with ChatGPT:

1. Generate API secret key

Generate Secret API key from Open AI Chat GTP application, which will be used for authentication in the next step.

2. Create a rest message

Create a REST message with end point as > https://api.openai.com/v1/completions

Use the syntax :

Authorization – Barear <your_token_key>

In ServiceNow HTTP Request headers along with content type as application/json.

3. Create a “POST” method

Create a “POST” Method as shown above with same end point and “HTTP Request headers”, In the “HTTP Query Parameters” pass the content in the below json format. You can copy/past this code:

{
“model”: “text-davinci-003”,
“prompt”: “${question}”,
“max_tokens”: 1024,
“temperature”: 0
}

4. Auto-generate variables

Click on the ”Auto-generate variables” which will result in creation of a variable in the “variable substitutions” related list. Pass the value in the string format in the “Test value”.

5. Test and get HTTP status 200

Click on “Test”. HTTP Status 200 indicates success, you will be able to see the code generated by ChatGPT in “Response” field as shown below.

6. Use “PreviewScript Usage”

Use “PreviewScript Usage” UI action on the POST method to generate a javascript code which can be used to dynamically generate the code anywere needed. You can use JSON parser to prase the json response and extract the code as needed.

Code :

var r = new sn_ws.RESTMessageV2(‘GPT Magic’, ‘GET ANSWER’);
r.setStringParameterNoEscape(‘question’, ‘generate a servicenow business rule to allow to close the change only if all related change tasks are closed.’);
var response = r.execute();
var responseBody = response.getBody();
var str = JSON.parse(responseBody);
var script = str.choices[0].text;
gs.print(script);

I have used the above script as business rule in a business rule table itself to generate code in advance tab, if the “name” field is updated with the code requirement as shown below.

Questions about ChatGPT integration with ServiceNow?

In conclusion, using ChatGPT to generate ServiceNow JavaScript code is a powerful tool that can save time and effort. By following the steps outlined in this article, you can easily integrate ChatGPT with ServiceNow and generate code for various use cases. If you have any questions or need help with integrating ChatGPT with ServiceNow, don’t hesitate to contact us. We’re here to help you maximize the benefits of this cutting-edge technology.

Looking for ServiceNow expertise and support?

+35 Projects+50 ServiceNow Experts+12 Happy Clients
Discuss Your Project