Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to update the MRVS data using Scripted Rest API after RITM creation

Lakshmi53
Tera Contributor

In form i have MRVS in that i have 2 variables

1) info

2) value

RITM will submitted through API. after RITM submitted values not attaching to MRVS. i am passing data using json fromat

{

"info":"123",

"value:"455"

}

i tried scripting in multiple ways.  but not working

 
    var mrvsGR = new GlideRecord('sc_multi_row_question_answer');
    mrvsGR.initialize();
    mrvsGR.request_item = '32f80adc3b3bd6102d68d88c24e45a1a';  
    mrvsGR.variable_set = 'eb4b3e413b2b1250a3c2baac24e45a6e';  
    mrvsGR.order = (index + 1) * 100;
    var mrvsSysId = mrvsGR.insert();

    for (var key in labels) {
        if (labels.hasOwnProperty(key)) {

            var optionKey = new GlideRecord('sc_item_option');
            optionKey.initialize();
            optionKey.item_option_new = 'eb4b3e413b2b1250a3c2baac24e45a6e';
            optionKey.value = key;
            var keySysId = optionKey.insert();

            var mtomKey = new GlideRecord('sc_item_option_mtom');
            mtomKey.initialize();
            mtomKey.request_item = '32f80adc3b3bd6102d68d88c24e45a1a';
            mtomKey.sc_item_option = keySysId;
            mtomKey.multi_row_question_answer = mrvsSysId;
            mtomKey.insert();

            var optionValue = new GlideRecord('sc_item_option');
            optionValue.initialize();
            optionValue.item_option_new = 'eb4b3e413b2b1250a3c2baac24e45a6e';
            optionValue.value = labels[key];
            var valueSysId = optionValue.insert();

            var mtomValue = new GlideRecord('sc_item_option_mtom');
            mtomValue.initialize();
            mtomValue.request_item = '32f80adc3b3bd6102d68d88c24e45a1a';
            mtomValue.sc_item_option = valueSysId;
            mtomValue.multi_row_question_answer = mrvsSysId;
            mtomValue.insert();
        }
    }
});
1 REPLY 1

JenniferRah
Mega Sage
Mega Sage

I created a Scripted REST API that works. Here is my payload I'm sending: 

{
   "number": "RITM0010020",
   "variables" : {
      "my_mrvs" : [{
        "info": "first one",
        "value": "1"
      },
      {
        "info": "second one",
        "value": "2"
      }]
   },
   "comments": "adding 2 rows."
}

 

And here is the code I used:

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var requestBody, responseBody, status, sm;
    var res = {};

    try {
        requestBody = request.body;
        var JSONdata = new global.JSON().decode(requestBody.dataString);

        //Get RITM 
        var ritm = new GlideRecordSecure('sc_req_item');
        if (JSONdata.sys_id == '' || JSONdata.sys_id == null) {
            ritm.addQuery('number', JSONdata.number);
        } else {
            ritm.addQuery('sys_id', JSONdata.sys_id);
        }
        ritm.query();
        var variables = [];
        if (ritm.next()) {
            //set variables
            var obj = JSONdata.variables;
            for (var key in obj) {
                variables.push(key);
                if (ritm.variables[key] != undefined) {
					for(x = 0; x < obj[key].length; x++){
						var newRow = ritm.variables[key].addRow();
						newRow.info = obj[key][x].info.toString();
						newRow.value = obj[key][x].value.toString();
						newRow.update();
					}
                } else {
                    return {
                        "status": "Error",
                        "message": "Variable " + key + " doesn't exist on record.",
                    };
                }
            }
			if(JSONdata.comments){
				ritm.comments = JSONdata.comments;
			}
            ritm.update();

            return {
                "status": "Success",
                "message": "Record updated",
            };
        } else {
            return {
                "status": "Error",
                "message": "Record not found",
            };
        }
    } catch (ex) {
        res["status"] = "500";
        res["message"] = "An error has occurred";
        response.setBody(JSON.stringify(res));
        status = '500';
    }

})(request, response);

This code is specialized to only look for MRVSs and add a row. If you wanted it to update existing rows, it would need to be modified. Hopefully it's enough to get you going, though.