How to send payload using setRequestBody()?

harun_isakovic
Mega Guru

Hello  community,

 

I am trying to build a dynamic object of data, stringify it then set it as request body before sending it to another instance.

That means I collect for example short description and description from the source instance and insert an incident in the target instance with the same short description and description. But for some reason only the short description gets sent.

This is my code

	var fields = ["short_description", "description"];
    var reqObj = {};
    for (var i = 0; i < fields.length; i++) {
        var key = fields[i];
        var value = fields[i];
        reqObj[key] = current[value].toString();
    }

    var reqObjStr = JSON.stringify(reqObj);
    //gs.info(reqObjStr); Result -> {"short_description":"Testing field sending12345","description":"desc testing field sending12345"}

    try {
        var r = new sn_ws.RESTMessageV2('Custom Message', 'insert');
        r.setStringParameterNoEscape('instanceURL', 'https://dev1152.service-now.com');
        r.setStringParameterNoEscape('tableName', 'incident');
        r.setRequestHeader("Accept", "application/json");
        r.setRequestHeader('Content-Type', 'application/json');
        r.setRequestBody(reqObjStr);
        r.setAuthenticationProfile('basic', 'ae7135014760211039c1b616536d430f');


        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
    } catch (ex) {
        gs.debug('[dev1152]' + ex.message);
    }

 Attached picture of how the incident arrives from this.

I cant seem to figure out what am I doing wrong that nothing except the first key gets sent.

Ive also just tried to add a raw/hardcoded payload for example same one from the log it would have the same result.

Does anyone else see a mistake I dont?

 

Thanks in advance!

0 REPLIES 0