- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 12:57 AM - edited 03-13-2023 08:34 AM
Hello Experts,
How to configure the scripted rest API for updation of incident record producer.
If possible give me an example of a put method script
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 08:57 AM
To configure a Scripted REST API for updating an incident record in ServiceNow, you can follow these general steps:
- Create a Scripted REST API resource in ServiceNow by navigating to "System Web Services" -> "Scripted Web Services" -> "Scripted REST APIs" in the left-hand navigation pane.
- Define the endpoint URL for your resource, which will include the incident number or sys_id of the record you want to update.
- Define the HTTP method that your resource will respond to. In this case, you will use the PUT method to update the incident record.
- Write a script that will update the incident record. This script should extract the incident number or sys_id from the endpoint URL, retrieve the incident record using a GlideRecord query, and update the necessary fields.
- Test your Scripted REST API by sending a PUT request to the endpoint URL you defined, passing in the necessary parameters.
Here's an example of a PUT method script for updating an incident record in ServiceNow:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var incidentNumber = request.pathParams.number; // Extract incident number from endpoint URL
var incident = new GlideRecord('incident');
incident.addQuery('number', incidentNumber);
incident.query();
if (incident.next()) {
// Update incident fields
incident.short_description = request.body.data.short_description;
incident.description = request.body.data.description;
incident.state = request.body.data.state;
incident.update();
// Return updated incident record
return incident;
} else {
// Return error if incident not found
response.setError(new sn_ws_err.NotFoundError('Incident not found'));
return response;
}
})(request, response);
This script retrieves the incident number from the endpoint URL using the request.pathParams object. It then performs a GlideRecord query to retrieve the incident record with that number. If the incident is found, the script updates the short_description, description, and state fields with values passed in the request body, and saves the record using the update() method. Finally, the script returns the updated incident record.
Note that this is a simple example and you may need to modify the script to suit your specific requirements.
Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 08:57 AM
To configure a Scripted REST API for updating an incident record in ServiceNow, you can follow these general steps:
- Create a Scripted REST API resource in ServiceNow by navigating to "System Web Services" -> "Scripted Web Services" -> "Scripted REST APIs" in the left-hand navigation pane.
- Define the endpoint URL for your resource, which will include the incident number or sys_id of the record you want to update.
- Define the HTTP method that your resource will respond to. In this case, you will use the PUT method to update the incident record.
- Write a script that will update the incident record. This script should extract the incident number or sys_id from the endpoint URL, retrieve the incident record using a GlideRecord query, and update the necessary fields.
- Test your Scripted REST API by sending a PUT request to the endpoint URL you defined, passing in the necessary parameters.
Here's an example of a PUT method script for updating an incident record in ServiceNow:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var incidentNumber = request.pathParams.number; // Extract incident number from endpoint URL
var incident = new GlideRecord('incident');
incident.addQuery('number', incidentNumber);
incident.query();
if (incident.next()) {
// Update incident fields
incident.short_description = request.body.data.short_description;
incident.description = request.body.data.description;
incident.state = request.body.data.state;
incident.update();
// Return updated incident record
return incident;
} else {
// Return error if incident not found
response.setError(new sn_ws_err.NotFoundError('Incident not found'));
return response;
}
})(request, response);
This script retrieves the incident number from the endpoint URL using the request.pathParams object. It then performs a GlideRecord query to retrieve the incident record with that number. If the incident is found, the script updates the short_description, description, and state fields with values passed in the request body, and saves the record using the update() method. Finally, the script returns the updated incident record.
Note that this is a simple example and you may need to modify the script to suit your specific requirements.
Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2023 05:15 AM
Hi @Rajveer ,
To configure a Scripted REST API for updating an incident record in ServiceNow, you can follow these steps:
Create a new Scripted REST API resource by navigating to System Web Services > Scripted REST APIs and clicking New.
Enter a name and a short description for the API resource.
In the Resource Script field, define the logic for your PUT method. Here is an example script that updates an incident record in ServiceNow:
(function process(request, response) {
var gr = new GlideRecord('incident');
var requestBody = request.body.data;
// Check if the mandatory parameters are present
if (!requestBody.sys_id || !requestBody.short_description) {
response.setStatus(400);
response.setBody({
error: true,
message: "Missing mandatory parameters"
});
return response;
}
// Get the record with the specified sys_id
if (gr.get(requestBody.sys_id)) {
// Update the short_description field
gr.short_description = requestBody.short_description;
// Save the changes
gr.update();
// Set the response body
response.setBody({
error: false,
message: "Incident record updated successfully"
});
} else {
response.setStatus(404);
response.setBody({
error: true,
message: "Incident record not found"
});
}
return response;
})(request, response);
4. Save the Scripted REST API resource and activate it. You can then use a tool like cURL or Postman to send a PUT request to the API resource URL with the updated data in the request body. Here is an example cURL command:
curl --location --request PUT 'https://instance.service-now.com/api/<resource-name>/<sys_id>' \
--header 'Content-Type: application/json' \
--data-raw '{
"data": {
"sys_id": "d8291df2db01001030a6a0d3cd4bcb8f",
"short_description": "Updated short description"
}
}'
In this example, replace <resource-name> with the name of your Scripted REST API resource and <sys_id> with the sys_id of the incident record you want to update. Also, make sure to replace https://instance.service-now.com with the URL of your ServiceNow instance.
If my response helps you to resolve the issue close the question by ✅Accepting solution and hit 👍thumb icon. From Correct answers others will get benefited in future.
Thanks,
Ratnakar