How to use variable set variables in a payload for a Rest Message?

Jesus Nava
Tera Guru

Hello experts please I need your help, I have a catalog item that used regular variables, once the item ordered, a BR would execute to post a message via REST i, here is the piece of code and it is actually working:

find_real_file.png

as you see in the payload there is the "variables" that will fetch from the catalog item variables.

My issue here is that I modified the catalog item and instead of variables I used a variable set and it is not fetching the variables from the variable set, how can I add the variables from the variable set?

this is the current Outbound HTTP result:

{"Variables":"{\"Variables\":[{\"Requested For\":\"XXXXX\",\"Description\":\"Testing\",\"Reminder: please open a new case for this\":\"\"}]}","attachments":[{"Caller":"XXXXXX","Short Description":"Request Delivery","title":"SCTASK0000001","title_link":"https://mycompany.service-now.com/api/now/table/sc_task?sys_id=f99a926a87c5d51074c731dd3fbb3569"}],"text":"Request has been assigned to the group: ServiceDesk"}

As you see only 3 variables show Requested for, Description and Reminder. I'm missing the variables from the vatiable set I created, coyld you guide me on how to do it?

Thank you

1 ACCEPTED SOLUTION

Hi @Jesus Nava 

then you need to parse the json string and get the value

something like this

var ritmSysId = current.request_item;

var jsonString;
var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", ritmSysId);
gr.query();
if (gr.next()) {
   jsonString = gr.variables.variableSetName; // give MRVS variable set name here
}

var parser = JSON.parse(jsonString);

var variableValue = parser[0].variableName; // give name of variable within MRVS

Regards
Ankur

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron

Hi,

can you share your complete script?

Regards
Ankur

Hello Ankur, sure here it is:

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

var jsonObj = {};
var ritmSysId = current.request_item;
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritmSysId);
set.load();
var vs = set.getFlatQuestions();
var arr = [];
var obj = {};

for(var i=0;i<vs.size();i++){
var label = vs.get(i).getLabel(); 
var value = vs.get(i).getDisplayValue();
obj[label] = value.toString();
}

arr.push(obj);

jsonObj.Variables = arr;

gs.log(JSON.stringify(jsonObj),"test");
//}

    var short_desc = current.short_description.toString();
    var desc = current.description.toString();


    var payload = {
        'text':  'Request has been assigned to the group: ' + current.assignment_group.name,
        'Variables':  JSON.stringify(jsonObj),
        'attachments': [{
            'title': current.number.toString(),
             'title_link': 'https://' + gs.getProperty('instance_name') + '.service-now.com/api/now/table/sc_task?sys_id='+ current.sys_id,
             'Caller': current.request_item.u_requested_for_usr.getDisplayValue(),
            'Short Description': current.short_description.toString(),
            
        }]
    };

    send(payload);
    gs.log(payload, 'youngforestACL');


    function send(_payload) {
        // Encode the payload as JSON
        var SNJSON = JSON; // Workaround for JSLint warning about using JSON as a constructor
        var myjson = new SNJSON();
        var encoded_payload = myjson.encode(_payload);

        // Create and send the REST Message
        var msg = new sn_ws.RESTMessageV2();
        msg.setLogLevel('all');
        msg.setRequestHeader('Accept', 'Application/json');
        msg.setRequestHeader('Content-Type', 'text/plain');
        msg.setEndpoint('https://xxxxxxxxxx');
        msg.setHttpMethod('post');
        msg.setRequestBody(encoded_payload);
        var res = msg.execute();
         gs.log(res, 'youngforestACLres');
        return res;
        
    }

})(current, previous);

Thois one is in the sctask table, there is another one created in the RITM table, the one in the RITM is similar to tho sone I posted,

Regards

@Jesus Nava 

the variables which are not getting fetched are within multi-row variable set?

If it's a single row variable set then your script will work fine

If your variable set is Multi-row variable set then your script won't work directly; you will have to tweak it

Regards
Ankur