Scripted rest API conditions based on state on incident table

Joshuu
Kilo Sage

Hi @Mohith Devatte ,

As discussed, I would like to update few fields based on incident state.

For example, the json will be different when the incident is 'New', and the json is different with different set of fields when it is 'On hold' and same as with 'Resolved'.

How can I add those state conditions here in the below code?

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

    ProcessInsertIncident: function(requestData, response) {

        var validJSON = this._checkJSONValidity(requestData);
        var reqeustJSONParser = JSON.parse(validJSON);

        var responseBody = {};

        if (validJSON) {

            var inc = new GlideRecord('incident');
            inc.addQuery('correlation_id', reqeustJSONParser.id);
            inc.query();

            if (inc.next()) {
                inc.caller_id = reqeustJSONParser.caller_id;
                inc.short_description = reqeustJSONParser.short_description;
                inc.description = reqeustJSONParser.description;
                inc.work_notes = reqeustJSONParser.work_notes;
                inc.category = reqeustJSONParser.category;

                inc.update();

                responseBody.Message = 'Incident is Updated';

            } else {

                inc.initialize();
                inc.caller_id = reqeustJSONParser.caller_id;
                inc.short_description = reqeustJSONParser.short_description;
                inc.description = reqeustJSONParser.description;
                inc.comments = reqeustJSONParser.comments;
                inc.correlation_id = reqeustJSONParser.id;

                inc.insert();

                responseBody.Message = 'Incident is Created';
                responseBody.Number = inc.number;
            }

            response.setBody(responseBody);
        }
    },

    _checkJSONValidity: function(validJSON) {
        try {
            return JSON.stringify(validJSON);
        } catch (e) {
            gs.info('parsing error' + e);
        }
    },
    type: 'InsertIncident'
};

could you please help me.

Thanks,

Priya.

1 ACCEPTED SOLUTION

@priyarao  good to hear that and for create issue follow those

@priyarao first thing is for creating an incident do you need to insert different values according to state?

for update i can see state:"${state}" which will work for update as your are accessing using below format

reqeustJSONParser.state="1"

But for create there is no state element in payload so you need to do like below by setting the state to "new" with out any conditions as it should be in new state while its getting created.

else {
var createInc =new GlideRecord('incident')
createInc.initialize();
 createInc.caller_id = reqeustJSONParser.caller_id;
                createInc.short_description = reqeustJSONParser.short_description;
                createInc.description = reqeustJSONParser.description;
                createInc.comments = reqeustJSONParser.comments;
                createInc.correlation_id = reqeustJSONParser.id;
                createInc.state="1";

                createInc.insert();

                responseBody.Message = 'Incident is Created';
                responseBody.Number = createInc.number;
            
			}

Please mark my answer correct if it helps you

 

 

View solution in original post

21 REPLIES 21

Prasad Pagar
Mega Sage

Hi,

Try this

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

    ProcessInsertIncident: function(requestData, response) {

        var validJSON = this._checkJSONValidity(requestData);
        var reqeustJSONParser = JSON.parse(validJSON);

        var responseBody = {};

        if (validJSON) {

            var inc = new GlideRecord('incident');
            inc.addQuery('correlation_id', reqeustJSONParser.id);
            inc.query();

            if (inc.next()) {
if(inc.state == 'ADD New CHOICE Backend value here')
{
//Set New fields
}
else if(inc.state == 'ADD Onhold CHOICE Backend value here')
{
//Set Onhold fields
}
else if(inc.state == 'ADD Resolved CHOICE Backend value here')
{
//Set Resolved fields
}
           
            } else {

                inc.initialize();
                inc.caller_id = reqeustJSONParser.caller_id;
                inc.short_description = reqeustJSONParser.short_description;
                inc.description = reqeustJSONParser.description;
                inc.comments = reqeustJSONParser.comments;
                inc.correlation_id = reqeustJSONParser.id;

                inc.insert();

                responseBody.Message = 'Incident is Created';
                responseBody.Number = inc.number;
            }

            response.setBody(responseBody);
        }
    },

    _checkJSONValidity: function(validJSON) {
        try {
            return JSON.stringify(validJSON);
        } catch (e) {
            gs.info('parsing error' + e);
        }
    },
    type: 'InsertIncident'
};

Thank you
Prasad

Mohith Devatte
Tera Sage
Tera Sage

HELLO @priyarao ,

YOU CAN DO THIS INSIDE THE QUERY LOOP AND AT THE INITIALISATION OF THE INCIDENT ALSO LIKE BELOW 

But while initialising you need to make sure the JSON contains state element value also then only you can compare whether its new state json or oh hold json or resolved json like below 

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

    ProcessInsertIncident: function(requestData, response) {

        var validJSON = this._checkJSONValidity(requestData);
        var reqeustJSONParser = JSON.parse(validJSON);

        var responseBody = {};

        if (validJSON) {

            var inc = new GlideRecord('incident');
            inc.addQuery('correlation_id', reqeustJSONParser.id);
            inc.query();

            if (inc.next()) {
if(inc.state=="your new state choice value")
{
     //map your fields according to your new state JSON 

                inc.update();
}
else if(inc.state=="your on hold choice value")
{
/map your fields according to your on hold JSON 

                inc.update();
}
else if(inc.state=="your resolved choice value")
{
/map your fields according to your resolved JSON 

                inc.update();
}


                responseBody.Message = 'Incident is Updated';

            } else {

                inc.initialize();

                if(reqeustJSONParser.state=="your JSON state element value should be new")
{
     //map your fields according to your new state JSON 

                inc.insert();
}
else if(reqeustJSONParser.state=="your JSON state element value  of on hold")
{
/map your fields according to your on hold JSON 

                inc.insert();
}
else if(reqeustJSONParser.state=="your JSON state element value  of  resolved")
{
/map your fields according to your resolved JSON 

                inc.insert();
}

               

                responseBody.Message = 'Incident is Created';
                responseBody.Number = inc.number;
            }

            response.setBody(responseBody);
        }
    },

    _checkJSONValidity: function(validJSON) {
        try {
            return JSON.stringify(validJSON);
        } catch (e) {
            gs.info('parsing error' + e);
        }
    },
    type: 'InsertIncident'
};

Hi @Mohith Devatte ,

I have updated the code as below. But Create/Update both are not working.

And if I add if(reqeustJSONParser.state == '3') instead of if(inc.state == '3') the update part is working. But create is not working.

Also, please note that I am using my other Personal Development Instance here as third party tool. And could you please let me know how can I add multiple JSONs for create and update at the same time in content field in HTTP method for testing.

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

    ProcessInsertIncident: function(requestData, response) {

        var validJSON = this._checkJSONValidity(requestData);
        var reqeustJSONParser = JSON.parse(validJSON);

        var responseBody = {};

        if (validJSON) {

            var inc = new GlideRecord('incident');
            inc.addQuery('correlation_id', reqeustJSONParser.id);
            inc.query();

            if (inc.next()) {
                if (inc.state == '3') {

                    inc.state = reqeustJSONParser.state;
                    inc.hold_reason = reqeustJSONParser.hold_reason;
                    inc.contact_type = reqeustJSONParser.contact_type;
                    inc.assignment_group = reqeustJSONParser.assignment_group;
                    inc.assigned_to = reqeustJSONParser.assigned_to;
                    inc.approval = reqeustJSONParser.approval;
                    inc.work_notes = reqeustJSONParser.work_notes;

                    inc.update();

                    responseBody.Message = 'Incident is Updated';
                }

            } else {
				
				inc.initialize();
				
				if (reqeustJSONParser.state == '1') {

                inc.caller_id = reqeustJSONParser.caller_id;
                inc.short_description = reqeustJSONParser.short_description;
                inc.description = reqeustJSONParser.description;
                inc.comments = reqeustJSONParser.comments;
                inc.correlation_id = reqeustJSONParser.id;

                inc.insert();

                responseBody.Message = 'Incident is Created';
                responseBody.Number = inc.number;
            }
			}
			
            response.setBody(responseBody);
        }
    },

    _checkJSONValidity: function(validJSON) {
        try {
            return JSON.stringify(validJSON);
        } catch (e) {
            gs.info('parsing error' + e);
        }
    },
    type: 'InsertIncident'
};

 

Thank you,

Priya.

Also @Mohith Devatte , could you please tell me how to do unicode conversion in this script. Like it should allow the special characters as well.

 

Thank you,

Priya.