The CreatorCon Call for Content is officially open! Get started here.

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  

8 REPLIES 8

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

sumanta pal
Kilo Guru

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

1. **Create an endpoint to receive the JSON request:**
- In ServiceNow, navigate to System Web Services > Scripted REST APIs.
- Click on New to create a new API, fill in the necessary details like Name, API ID, etc.
- Under the Resources related list, click on New to create a new resource.
- In the Script field, write a script to parse the incoming JSON and create an incident.

2. **Script to parse JSON and create an incident:**
- Use the request object to get the body of the incoming request.
- Parse the JSON using JSON.parse().
- Use the GlideRecord object to create a new incident.
- Here's a sample script:

javascript
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var body = request.body.data;
var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = body.short_description;
gr.description = body.description;
gr.insert();
})(request, response);


3. **Create an Incident Task:**
- After creating the incident, you can create an incident task associated with it.
- Here's a sample script:

javascript
var grTask = new GlideRecord('incident_task');
grTask.initialize();
grTask.short_description = body.task_short_description;
grTask.description = body.task_description;
grTask.parent = gr.sys_id; // sys_id of the incident created above
grTask.insert();


4. **Test the API:**
- You can use tools like Postman to send a JSON request to the API you created and check if the incident and incident task are created successfully.

Remember to replace the field names and values with the ones that match your requirement. Also, make sure to handle any errors and edge cases in your script.


nowKB.com

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

Amit Verma
Kilo Patron
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


Please mark this response as correct and helpful if it assisted you with your question.