Create standard change request via scripted rest api

punisher
Tera Contributor

Hello,

my requirement is to create a scripted rest api in instance A to  allow instance B to create change request record in instance A.

So for the scripted rest api I created with the below code. If i create a standard change request in instance B it should also create a record with standard type but its always creating normal type only. When I try the scripted rest api with Rest API explorer and manually passing the type as standard also, its creating normal type only. Can anybody help with this?

 

[code]

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var ans = [];
    var body = request.body.data;

    gs.info("type of the change request : "+body.type);

    if (!body.short_description && !body.type && !body.state && !body.priority) {
        response.setStatus(400);
        response.setBody({
            status: 'error',
            message: 'Missing required parameters. Ensure short_description, description, assignment_group, and requested_by are provided.'
        });
        return;
    }
    try {
        var grCR = new GlideRecord('change_request');
        grCR.initialize();
        if (body.type == 'standard') {
            grCR.type = 'standard';
        } else {
            grCR.setValue('type', body.type);
        }
        grCR.setValue('short_description', body.short_description);
        grCR.setValue('state', body.state);
        grCR.setValue('priority', body.priority);
        var chSysId = grCR.insert();

        var result = {
            number: grCR.getValue('number'),
            sysId: chSysId
        };
        ans.push(result);
        response.setStatus(201);
        response.setBody(ans);

    } catch (error) {
        var serviceError = new sn_ws_err.ServiceError();
        serviceError.setMessage('Failed');
        serviceError.setDetail(error + 'Failed to process the data');
        serviceError.setStatus(404);
        response.setError(serviceError);
    }
})(request, response);
 
[/code]
3 REPLIES 3

Kieran Anson
Kilo Patron

Have you reviewed the OOB API for change requests? Specifically there is a standard change version

.https://developer.servicenow.com/dev.do#!/reference/api/yokohama/rest/change-management-api#change-P...

Hi Kieran,

Thanks a lot for your response. I'm still new to ServiceNow and not very familiar with API integrations, especially when it comes to Change Requests. This is my first time working with this type of requirement, so it's a bit overwhelming.

I did go through the Change Management API documentation you shared, but I'm having some trouble fully understanding how to implement it. Would it be possible for you to walk me through the steps to create a Standard Change Request via the API or share an example?

I'd really appreciate any additional guidance you can provide.

Thanks again!

punisher
Tera Contributor

Hi Kieran,

Thanks a lot for your response. I'm still new to ServiceNow and not very familiar with API integrations, especially when it comes to Change Requests. This is my first time working with this type of requirement, so it's a bit overwhelming.

I did go through the Change Management API documentation you shared, but I'm having some trouble fully understanding how to implement it. Would it be possible for you to walk me through the steps to create a Standard Change Request via the API or share an example?

I'd really appreciate any additional guidance you can provide.

Thanks again!