Write Logic in Scripted REST API

Lucky1
Tera Guru

Hello all,

 

I need to create a Scripted REST API for creating a Change request and I will pass on this API to the customer to create CR in service-now.

But the change request should not get created with few Configuration items.

I have created a system property "exclude_cis" and now how can I restrict the customer to create a Change request when selecting a configuration item from one of those added in the system property before creating?

 

I was asked to write the logic in scripted REST API only but not use business rule.

 

 

So, can someone help me how can I write the logic before creating a Change request from the API.

 

 

Regards,

Lucky

 

 

 

 

1 ACCEPTED SOLUTION

Rajesh Chopade1
Mega Sage

hi @Lucky1 

you can try bellow sample script once and make changes as per your payload:

var requestBody = request.body.data;
    var ciSysId = requestBody.cmdb_ci; // Assumes sys_id of CI is passed

    if (!ciSysId) {
        response.setStatus(400);
        return { error: 'Missing required field: cmdb_ci (Configuration Item sys_id)' };
    }

    // 1. Get system property
    var excludeCIsProp = gs.getProperty('exclude_cis', '');
    var excludedCIs = excludeCIsProp.split(',').map(function(item) {
        return item.trim();
    });

    // 2. Check if provided CI is in the excluded list
    if (excludedCIs.indexOf(ciSysId) !== -1) {
        response.setStatus(403);
        return { error: 'Change Request creation is not allowed for this Configuration Item.' };
    }

    // 3. Create Change Request
    var gr = new GlideRecord('change_request');
    gr.initialize();
    gr.short_description = requestBody.short_description || 'Created via Scripted REST API';
    gr.cmdb_ci = ciSysId;
    gr.category = requestBody.category || 'software'; // optional
    gr.assignment_group = requestBody.assignment_group; // optional
    gr.priority = requestBody.priority || 3; // optional

    var sys_id = gr.insert();

    response.setStatus(201);
    return {
        message: 'Change Request created successfully',
        change_request_sys_id: sys_id
    };

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

 

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucky1 

in scripted rest api you can check the CI name after parsing the JSON and see if it's the excluded one

if yes then throw error via the API response.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Rajesh Chopade1
Mega Sage

hi @Lucky1 

you can try bellow sample script once and make changes as per your payload:

var requestBody = request.body.data;
    var ciSysId = requestBody.cmdb_ci; // Assumes sys_id of CI is passed

    if (!ciSysId) {
        response.setStatus(400);
        return { error: 'Missing required field: cmdb_ci (Configuration Item sys_id)' };
    }

    // 1. Get system property
    var excludeCIsProp = gs.getProperty('exclude_cis', '');
    var excludedCIs = excludeCIsProp.split(',').map(function(item) {
        return item.trim();
    });

    // 2. Check if provided CI is in the excluded list
    if (excludedCIs.indexOf(ciSysId) !== -1) {
        response.setStatus(403);
        return { error: 'Change Request creation is not allowed for this Configuration Item.' };
    }

    // 3. Create Change Request
    var gr = new GlideRecord('change_request');
    gr.initialize();
    gr.short_description = requestBody.short_description || 'Created via Scripted REST API';
    gr.cmdb_ci = ciSysId;
    gr.category = requestBody.category || 'software'; // optional
    gr.assignment_group = requestBody.assignment_group; // optional
    gr.priority = requestBody.priority || 3; // optional

    var sys_id = gr.insert();

    response.setStatus(201);
    return {
        message: 'Change Request created successfully',
        change_request_sys_id: sys_id
    };

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

 

Thank you, Rajesh,

 

I will start working on this code and get back.

 

 

Regards,

Lucky