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

@Mohith Devatte After creating rest message, we will create http method with content body right. In that part, I just want to test both create and update at once. 

Joshuu
Kilo Sage

@Mohith Devatte ,

below is the example one for update.find_real_file.png

 

you can do one thing 

you can createJSON of JSON's like below 

{"create":"{}" , "update" : "{}"} 

 

But while accessing in script you would have to access it in a different way as there are multiple JSONs using key value pair 

key is create and create holds your create pay load and same goes with update too 

 

@Mohith Devatte ,

ok, so for example 

reqeustJSONParser.create.short_description

reqeustJSONParser.update.contact_type

 

something like this is it ?

@priyarao exactly ! it might work try this