Rest Inbound Integration

Rosy14
Tera Guru

Hi,

the senario is : there are two Scripted Rest resource one for incident create and another is incident update.

 

In Update if the incident state is closed it can not reopen again. it need to create a new incident with the given payload details. How to call the create script from update?

Resource path for create: /XXXX/CreationOfIncident

 

Thanks in advance.

3 REPLIES 3

AnveshKumar M
Tera Sage
Tera Sage

Hello @Rosy14 

I recommend move the script to a Script Include instead of keeping it in the Scripted REST API itself. So you can call the ScriptInclude from both create and update resources.

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

How to do that?

@Rosy14 see the following example.

 

I have created a Scripted REST API, POST Resource for create and PATCH resource for update. And I have created ScriptInclude to centralize the code and to improve the re usability.

 

Script Include: (System Definition -> Script Includes

AnveshKumarM_0-1702296870693.png

 

var IncidentIntegration = Class.create();
IncidentIntegration.prototype = {
    initialize: function() {},

    createIncident: function(data) {
        var incGr = new GlideRecord('incident');
        incGr.initialize();
		incGr.caller_id = gs.getUserID();
        incGr.short_description = data.summary;
        incGr.description = data.description;
        incGr.insert();

        var resp = {
            sys_id: incGr.getUniqueValue(),
            number: incGr.getValue('number'),
            status: 201
        };

        return resp;
    },

    updateIncident: function(data) {
        var incGr = new GlideRecord('incident');
        var resp;
        if (incGr.get('number', data.number)) {
            //If the traget INC state is clsoed (7) call the create function otherwise update the incident
            if (incGr.getValue('state') == '7') {
                return this.createIncident(data);
            } else {
                incGr.short_description = data.summary;
                incGr.description = data.description;
                incGr.state = data.state;
                incGr.close_code = data.resolution_code;
                incGr.close_notes = data.resolution_notes;
                incGr.update();
                resp = {
                    sys_id: incGr.getUniqueValue(),
                    number: incGr.getValue('number'),
                    status: 200
                };
                return resp;
            }
        }
        resp = {
            status: 404
        };

        return resp;
    },

    type: 'IncidentIntegration'
};

 

Create Scripted resource:

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

    var inc_data = request.body.data;
	//call the script include and pass the data
	var incUtils = new IncidentIntegration().createIncident(inc_data);
	response.setBody(incUtils);
	response.setStatus(201);

})(request, response);

 

Update Scripted resource:

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

    var inc_data = request.body.data;
	//call the script include and pass the data
	var incUtils = new IncidentIntegration().updateIncident(inc_data);
	response.setBody(incUtils);
	response.setStatus(incUtils.status);

})(request, response);

 

 

If you observer the updateIncident method in the Script Include, I'm checking the target incident for closed state or not, if closed I'm calling the create function otherwise it will continue with the update actions.

 

This is a simple example for your understanding.

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh