Create a Standard Change request via custom REST API

mkdangi21
Giga Expert

Create a Standard Change request via custom REST API

2 REPLIES 2

DUGGI
Giga Guru

@mkdangi21 

To create a standard change request via a custom REST API, you can create a Scripted REST API in ServiceNow that accepts the necessary parameters and then creates a change request record. Here's a step-by-step guide to creating a Scripted REST API for this purpose:

Step 1: Create a Scripted REST API

  1. In ServiceNow, navigate to "System Web Services > Scripted REST APIs" and click "New".
  2. Provide a name and an API ID for your new Scripted REST API, then click "Submit".
  3. Click on "Resources" under the Related Links, then click "New".
  4. Provide the following details for the new resource:
    • HTTP Method: "POST"
    • Name: "Create Standard Change Request"
    • Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
  var requestBody = request.body.data;

  // Input validation
  if (!requestBody.short_description || !requestBody.description || !requestBody.assignment_group || !requestBody.requested_by) {
    response.setStatus(400);
    response.setBody({
      status: 'error',
      message: 'Missing required parameters. Ensure short_description, description, assignment_group, and requested_by are provided.'
    });
    return;
  }

  // Create the standard change request
  var changeRequest = new GlideRecord('change_request');
  changeRequest.initialize();
  changeRequest.short_description = requestBody.short_description;
  changeRequest.description = requestBody.description;
  changeRequest.assignment_group = requestBody.assignment_group;
  changeRequest.requested_by = requestBody.requested_by;
  changeRequest.type = 'standard'; // Set the change request type to "standard"
  changeRequest.setValue('state', 1); // Set the state to "New"
  var changeRequestSysId = changeRequest.insert();

  response.setStatus(201);
  response.setContentType('application/json');
  response.setBody({
    status: 'success',
    message: 'Standard change request created.',
    change_request_sys_id: changeRequestSysId
  });
})();

  1. Click "Submit" to save the resource.

Step 2: Call the Scripted REST API

You can now call the custom Scripted REST API to create a standard change request. Here's an example of calling the API using a REST client:

jsonCopy code
{
  "short_description": "New Standard Change Request",
  "description": "This is a description of the standard change request.",
  "assignment_group": "assignment_group_sys_id",
  "requested_by": "requested_by_user_sys_id"
}

Replace the placeholders in the endpoint and body with the actual values. The Scripted REST API will create a new standard change request with the provided parameters and return a response containing the sys_id of the created change request.

Christian_
Tera Guru

Hello,

 

Here is a link to the documentation for ServiceNow's Rest API for Change Management. Change Management API.

Utilizing this will spare you the need to create a custom end-point.

 

For Standard Change you could use the following method as this is based on a template: 

POST /sn_chg_rest/change/standard/{standard_change_template_id}