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

@priyarao any follow up required ?

did it work for the new record also with the above script ?

Hi @Mohith Devatte ,

I have just removed the below condition from the script for create and it was working fine(creating incident in the target instance with New state).

if (reqeustJSONParser.state == '1')

Thank you for your help.

Just want to know how can we add multiple JSONs in the content field in HTTP method. like can we add create and update JSONs at the same time?

 

Thanks.

Priya.

@priyarao sure glad it helped !

if it helped please close the thread by marking the answer correct so that it goes to solved queue and will help future readers

Good day!

Hi @Mohith Devatte ,

Just want to know how can we add multiple JSONs in the content field in HTTP method. like can we add create and update JSONs at the same time?

@priyarao you mean in the script rest API or the REST message ?

you need multiple JSON's ?