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 in your JSON you have state value stored ?

can you share your exact JSON i may be able to help you

@priyarao send me the payload which involved the special characters also 

lets debug that also usually it should not happen if you are trying to map it to any specific string field it should accept the special characters too .

anyways send the exact payload please

Hi @Mohith Devatte ,

I am using the below JSON.

For create:

{
"caller_id":"${caller}",
"short_description":"${short_description}",
"description":"${description}",
"comments":"${comments}",
"id":"${number}"
}

For Update:

{
"state":"${state}",
"hold_reason":"${hold_reason}",
"contact_type":"${contact_type}",
"assignment_group":"${assignment_group}",
"assigned_to":"${assigned_to}",
"approval":"${approval}",
"work_notes":"${work_notes}",
"id":"${number}"
}

And the business rule that I have created is:

Business Rule: Before Insert and Update

(function executeRule(current, previous /*null when async*/) {

	var request = new sn_ws.RESTMessageV2('Sending Data Create','Post');
	request.setStringParameterNoEscape('caller',current.caller_id);
	request.setStringParameterNoEscape('short_description',current.short_description);
	request.setStringParameterNoEscape('description',current.description);
	request.setStringParameterNoEscape('comments',current.comments);
	request.setStringParameterNoEscape('work_notes',current.work_notes);
	request.setStringParameterNoEscape('number',current.number);
	request.setStringParameterNoEscape('contact_type',current.contact_type);
	request.setStringParameterNoEscape('assignment_group',current.assignment_group);
	request.setStringParameterNoEscape('assigned_to',current.assigned_to);
	request.setStringParameterNoEscape('approval',current.approval);
	request.setStringParameterNoEscape('state',current.state);
	request.setStringParameterNoEscape('hold_reason',current.hold_reason);
	
	var response=request.execute();
	var requestBody=request.getRequestBody();
	var responseBody=response.getBody();
	var httpStatus=response.getStatusCode();
	gs.log(responseBody);
	
	responseJSON=responseBody.substring(10,responseBody.length-1);
	parsedJSON=JSON.parse(responseJSON);

})(current, previous);

Also, how can we add multiple JSONs for create and update at the same time in content field in HTTP method.

 

Thanks,

Priya.

Hi @Mohith Devatte ,

I just checked for short description field again, It is creating incident even if I add special characters. I hope that part is working.

@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