how to update the data in MRVs using scripted rest api
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2025 11:04 PM
Below data i received in postman
"system": [{
"name":"123",
"variables": {
"label10": "avvv"
}
}],
Using scripted rest API created the form once API call through postman. in that i have MRV. which data i have in variables in need to store in MRV. in MRV i have 2 variables key and value label 10 need to store in key and avvv in value.
how to achieve this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2025 11:39 PM
you first need to understand how are you getting the MRVS data in the json body
1) get the array of json objects
2) then set it like this
// you should be able to form array of json object like this from the incoming json request body
//mrvsvariable1 and mrvsvariable2 are the names of variables within MRVS
var arr = [{
"mrvsvariable1": "myvalue1",
"mrvsvariable2": "myvalue2"
},
{
"mrvsvariable1": "myvalue3",
"mrvsvariable2": "myvalue4"
}
];
var gr = new GlideRecord("sc_req_item");
gr.addQuery("number", "RITM0001");
gr.query();
if (gr.next()) {
gr.variables.mrvsVariableSetName = JSON.stringify(arr);
gr.update();
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2025 11:46 PM
i will get the data from 3rd party tool and data i need to display as below
"system": [{
"name":"123",
"variables": {
"label10": "avvv",
"label11":""aww"
}
}],
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2025 11:58 PM - edited ‎02-09-2025 11:59 PM
I already told how the data can be set, so please enhance further
what script did you start with and where are you stuck?
something like this should help you and you can enhance it further based on your actual request body and your developer skills
// Sample JSON data
var jsonData = {
"system": [{
"name": "123",
"variables": {
"label10": "avvv",
"label11": "aww"
}
}]
};
// Function to extract label10 and label11 and return as an array of objects
function extractLabels(data) {
var result = [];
if (data.system && data.system.length > 0) {
var variables = data.system[0].variables;
if (variables.label10) {
result.push({
"label10": variables.label10
});
}
if (variables.label11) {
result.push({
"label11": variables.label11
});
}
}
return result;
}
// Extract labels
var extractedLabels = extractLabels(jsonData);
var gr = new GlideRecord("sc_req_item");
gr.addQuery("number", "RITM0001");
gr.query();
if (gr.next()) {
gr.variables.mrvsVariableSetName = JSON.stringify(extractedLabels);
gr.update();
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader