Scripted Rest api for incident create

Ketan Pandey
Tera Expert

I am working a requirement where i want to create a incident and incident task when an request(JSON) received ,

Please suggest  

6 REPLIES 6

cloudops
Tera Expert


Sure, you can achieve this by using ServiceNow's REST API. Here's a high-level overview of the steps you would need to follow:

1. Create an endpoint in ServiceNow to receive the JSON request:
- Navigate to System Web Services > Scripted Web Services > New.
- Fill in the necessary fields and write a script to parse the incoming JSON request.

2. Create an Incident record:
- Use the new method of the GlideRecord class to create a new incident record.
- Set the necessary fields using the setValue method.
- Use the insert method to save the new record.

3. Create an Incident Task record:
- Similar to the incident record, use the new method of the GlideRecord class to create a new incident task record.
- Set the necessary fields using the setValue method.
- Use the insert method to save the new record.

 

Here's a sample code snippet:

javascript
// Parse the incoming JSON request
var requestBody = request.body.data;
var parsedBody = JSON.parse(requestBody);

// Create a new incident record
var incident = new GlideRecord('incident');
incident.initialize();
incident.short_description = parsedBody.short_description;
incident.description = parsedBody.description;
incident.insert();

// Create a new incident task record
var incidentTask = new GlideRecord('incident_task');
incidentTask.initialize();
incidentTask.short_description = parsedBody.task_short_description;
incidentTask.description = parsedBody.task_description;
incidentTask.parent = incident.sys_id; // Link the task to the incident
incidentTask.insert();

 

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - nowgpt.ai

Ahana 01
Tera Expert


Sure, you can achieve this by using ServiceNow's REST API and Scripted REST API. Here's a high-level overview of the steps you need to follow:

1. **Create a Scripted REST API in ServiceNow:**
- Navigate to System Web Services > Scripted REST APIs.
- Click on New to create a new API.
- Provide a name, API ID, and version for your API.
- Click on Submit to create the API.

2. **Create a Resource for the API:**
- Open the API you just created.
- Click on Resources related list and then click on New to create a new resource.
- Provide a name, HTTP method (POST in your case), and script for the resource.
- In the script, you need to parse the incoming JSON request and create an incident and incident task using GlideRecord.

3. **Sample Script for the Resource:**
- Here's a sample script that creates an incident and incident task based on the incoming JSON request:

javascript
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var data = request.body.data;
var grIncident = new GlideRecord('incident');
grIncident.initialize();
grIncident.short_description = data.short_description;
grIncident.description = data.description;
grIncident.insert();
var grIncidentTask = new GlideRecord('incident_task');
grIncidentTask.initialize();
grIncidentTask.short_description = data.task_short_description;
grIncidentTask.description = data.task_description;
grIncidentTask.parent = grIncident.sys_id;
grIncidentTask.insert();
})(request, response);


4. **Test the API:**
- You can test the API using any API testing tool like Postman.
- Send a POST request to the API endpoint with the JSON body containing the details of the incident and incident task.

5. **Handle Errors:**
- Make sure to handle any errors that might occur during the creation of the incident and incident task.
- You can use try-catch blocks in your script to handle errors.

Remember to replace the field names in the script with the actual field names in your incident and incident task tables.


nowKB.com
For a good and optimistic result, and solving ServiceNow-related issues please visit this website.https://nowkb.com/home

vermaamit16
Kilo Patron

Hi @Ketan Pandey 

 

Do you have a pre-defined JSON response to be received by the ServiceNow instance or an external system will make a REST Call towards the ServiceNow instance ?

 

Thanks & Regards

Amit Verma

Thanks and Regards
Amit Verma

Hi Amit,

 

Thank you for your response.

Here in my case ,External system will make the REST call