How to allow special characters from JSON payload data

Joshuu
Kilo Sage

Hello All,

I am configuring an integration through scripted rest api.

The requirement is to allow special characters as well from the JSON payload.

Below is the code. Could you please help me how can I modify the script to allow special characters/how can I do unicode conversion.

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 (reqeustJSONParser.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();

                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'
};

 Thanks,

Priya.

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

@priyarao i think that should not be the case .

iT SHOULD EVEN MAP THE VALUES EVEN MAP THE VALUES TO THE FIELD WITH SPECIAL CHARACTERS UNTIL AND LESS WE DO REGEX TO RESTRICT IT .

please mark sure your field that you are trying to map is a string field and hope you are accessing the JSON value correctly like below 

reqeustJSONParser.your_Json_element_value;

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU

View solution in original post

6 REPLIES 6

Pavankumar_1
Mega Patron

Hi,

can you share the payload structure?

 

Regards,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi Pavan,

This is the sample json for creating the incident which I am trying in pdi.

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

}

For testing purpose, I have just created an incident from other pdi instance including special characters in short_description. And the incident is not created in the target instance. Please help.

Hi,

Can you below script: 

 

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

    ProcessInsertIncident: function(requestData, response) {
        
        var parameters = requestData.body.data;
        var state = parameters.state.toString();
        var hold_reason = parameters.hold_reason.toString();
        var contact_type = parameters.contact_type.toString();
        var assignment_group = parameters.assignment_group.toString();
        var caller_id = parameters.caller_id.toString();
        var assigned_to = parameters.assigned_to.toString();
        var approval = parameters.approval.toString();
        var work_notes = parameters.work_notes.toString();
        var id = parameters.id.toString();
        var comments = parameters.comments.toString();
        var short_description = parameters.short_description.toString();
        var description = parameters.description.toString();


        var responseBody = {};

        if (validJSON) {

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

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

                    inc.state = state;
                    inc.hold_reason = hold_reason;
                    inc.contact_type = contact_type;
                    inc.assignment_group = assignment_group;
                    inc.assigned_to = assigned_to;
                    inc.approval = approval;
                    inc.work_notes = work_notes;
                    inc.update();

                    responseBody.Message = 'Incident is Updated';
                }

            } else {

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

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

            }

            response.setBody(responseBody);
        }
    },
    type: 'InsertIncident'
};

 

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Thanks for your response.