- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2025 03:58 AM
I have requirement that value of the variable should be stored in this format with name and quantity of hardware equipment.
[
{
"Part": "Standard Laptop",
"Qty": "1"
}
]
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2025 08:54 AM
try this
(function execute(inputs, outputs) {
var body = {};
var all_comments = [];
var all_worknotes = [];
var all_attachments = [];
var source_info = {};
var source_contact = {};
var source_variables = {};
var varValueArray = [];
var varValue;
var OrderVariable1 = {};
// Capture all the comment information on ticket
var comments = new GlideRecord('sys_journal_field');
comments.addQuery('element', 'comments');
comments.addQuery('element_id', inputs.task_id);
comments.addQuery('sys_created_by', 'DOES NOT CONTAIN', 'insight');
comments.query();
while (comments.next()) {
var comment = {};
comment.value = comments.value.toString();
comment.time = comments.sys_created_on.toString();
comment.created_by = comments.sys_created_by.toString();
all_comments.push(comment);
}
// Capture all the work note information on ticket
var work_notes = new GlideRecord('sys_journal_field');
work_notes.addQuery('element', 'work_notes');
work_notes.addQuery('element_id', inputs.task_id);
work_notes.addQuery('sys_created_by', 'DOES NOT CONTAIN', 'insight');
work_notes.query();
while (work_notes.next()) {
var worknote = {};
worknote.value = work_notes.value.toString();
worknote.time = work_notes.sys_created_on.toString();
worknote.created_by = work_notes.sys_created_by.toString();
all_worknotes.push(worknote);
}
// Add comments and work notes to the body
body.comments = all_comments;
body.work_notes = all_worknotes;
// Add in mandatory fields to the body
body.description = checkForNull(inputs.description);
body.short_description = checkForNull(inputs.short_description);
body.correlation_id = checkForNull(inputs.correlation_id);
body.correlation_display = checkForNull(inputs.correlation_display);
body.sys_id = checkForNull(inputs.integrated);
function checkForNull(inputs) {
if (inputs != null || inputs != undefined) {
return inputs.toString();
} else {
return '';
}
}
// Capture all attachments associated to this ticket
var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments('sc_task', inputs.task_id);
while (agr.next()) {
var attachment_body = {};
attachment_body.attachment_name = agr.file_name.toString();
attachment_body.attachment_type = agr.content_type.toString();
attachment_body.attachment_content = attachment.getContentBase64(agr);
all_attachments.push(attachment_body);
}
body.attachments = all_attachments;
// Populates the source_info, source_contact and source_variables objects
var task = new GlideRecord('sc_task');
task.addQuery('sys_id', inputs.task_id);
task.query();
if (task.next()) {
source_info.number = task.number.toString();
source_info.raised_by = task.sys_created_by.toString();
source_info.priority = task.priority.toString();
source_info.ritm = task.request_item.number.toString();
source_contact.name = task.opened_by.name.toString();
source_contact.email = task.opened_by.email.toString();
source_contact.phone = task.opened_by.mobile_phone.toString();
// Collects the variable data from the RITM ticket. Works for Variables and variable sets
var question_answers = new GlideRecord('sc_item_option_mtom');
question_answers.addQuery('request_item', task.request_item.sys_id.toString());
question_answers.orderBy('sc_item_option.order');
question_answers.query();
while (question_answers.next()) {
var varName = question_answers.sc_item_option.item_option_new.name;
if (question_answers.sc_item_option.item_option_new.type == '8') { // 8 = reference
varValue = getReferenceValue(question_answers.sc_item_option.item_option_new.reference, question_answers.sc_item_option.value);
} else if (question_answers.sc_item_option.item_option_new.type == '21') { // 21 = List Collector
if (question_answers.sc_item_option.value != '') {
var list = question_answers.sc_item_option.value.split(",");
for (i = 0; i < list.length; i++) {
if (i == 0) {
varValueArray.push(getReferenceValue(question_answers.sc_item_option.item_option_new.list_table, list[i]));
} else {
varValueArray.push(' ' + getReferenceValue(question_answers.sc_item_option.item_option_new.list_table, list[i]));
}
}
varValue = varValueArray.toString();
} else {
varValue = '';
}
} else if (question_answers.sc_item_option.item_option_new.type == '18') { // 18 = Lookup Select Box
varValue = getReferenceValue(question_answers.sc_item_option.item_option_new.lookup_table, question_answers.sc_item_option.value);
} else {
varValue = question_answers.sc_item_option.value.getDisplayValue();
}
if (varName != "" && varValue != "") {
source_variables[varName] = varValue;
gs.info('name of field ' + varName + ' and value ' + varValue);
}
}
var mrvsJson = task.request_item.variables.ordervariable1;
var parsedData = JSON.parse(mrvsJson);
var arr = [];
for (var i = 0; i < parsedData.length; i++) {
var obj = {};
obj["Part"] = parsedData[i].part; // give the part variable name
obj["Qty"] = parsedData[i].quantity; // give the quantity variable name
arr.push(obj);
gs.info('value of final MRVS ' + JSON.stringify(arr));
}
// Concatenate MRVS data into source_variables
source_variables['OrderVariable1'] = arr;
}
function getReferenceValue(table, sys_id) {
var gr = new GlideRecord(table);
gr.get(sys_id);
return gr.getDisplayValue();
}
body.source_variables = source_variables;
body.source_info = source_info;
body.source_contact = source_contact;
outputs.body = JSON.stringify(body);
gs.info('value of output ' + outputs.body);
})(inputs, outputs);
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
03-05-2025 03:25 AM
Hi @Ankur Bawiskar My logic is failing because there is no value getting populated in 'sc_item_option' for MRVS variables and logic written to get variable values from this table, i also check PDI and there also values for MRVS is not getting populated, so looks like it is OOTB behaviour. shall i create single line text and number variables in this case, if yes, how can we pass two different variable values as Object. can you please advise?
attaching original code for your reference.
(function execute(inputs, outputs) {
/* Create the following inputs in your flow to use this script:
task_id = sys_id of your catalog task
short_description = short description of your catalog task
description = description of your catalog task
correlation_id = sys_id of your catalog task
correlation_display = number of your catalog task
integrated = the correlation_id on your catalog task. This assumes that you are storing the Insight fulfillment task sys_id in the correlation_id field on your catalog task.
*/
var body = {};
var all_comments = [];
var all_worknotes = [];
var all_attachments = [];
var source_info = {};
var source_contact = {};
var source_variables = {};
//Capture all the comment information on ticket
var comments = new GlideRecord('sys_journal_field');
comments.addQuery('element', 'comments');
comments.addQuery('element_id', inputs.task_id);
comments.addQuery('sys_created_by', 'DOES NOT CONTAIN', 'insight');
comments.query();
while (comments.next()) {
var comment = {};
comment.value = comments.value.toString();
comment.time = comments.sys_created_on.toString();
comment.created_by = comments.sys_created_by.toString();
all_comments.push(comment);
}
//Capture all the work note information on ticket
var work_notes = new GlideRecord('sys_journal_field');
work_notes.addQuery('element', 'work_notes');
work_notes.addQuery('element_id', inputs.task_id);
work_notes.addQuery('sys_created_by', 'DOES NOT CONTAIN', 'insight');
work_notes.query();
while (work_notes.next()) {
var worknote = {};
worknote.value = work_notes.value.toString();
worknote.time = work_notes.sys_created_on.toString();
worknote.created_by = work_notes.sys_created_by.toString();
all_worknotes.push(worknote);
}
//Add comments and work notes to the body
body.comments = all_comments;
body.work_notes = all_worknotes;
//Add in mandatory fields to the body
body.description = checkForNull(inputs.description);
body.short_description = checkForNull(inputs.short_description);
body.correlation_id = checkForNull(inputs.correlation_id);
body.correlation_display = checkForNull(inputs.correlation_display);
body.sys_id = checkForNull(inputs.integrated);
function checkForNull(inputs) {
if (inputs != null || inputs != undefined) {
return inputs.toString();
} else {
return '';
}
}
//Capture all attachments associated to this ticket
var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments('sc_task', inputs.task_id);
while (agr.next()) {
var attachment_body = {};
attachment_body.attachment_name = agr.file_name.toString();
attachment_body.attachment_type = agr.content_type.toString();
attachment_body.attachment_content = attachment.getContentBase64(agr);
all_attachments.push(attachment_body);
}
body.attachments = all_attachments;
//Populates the source_info, source_contact and source_variables objects
var task = new GlideRecord('sc_task');
task.addQuery('sys_id', inputs.task_id);
task.query();
if (task.next()) {
//fields in the source_info and source_contact here are illustrative, you will likely want to amend this to refelct the data you wish to share
source_info.number = task.number.toString();
source_info.raised_by = task.sys_created_by.toString();
source_info.priority = task.priority.toString();
source_info.ritm = task.request_item.number.toString();
source_contact.name = task.opened_by.name.toString();
source_contact.email = task.opened_by.email.toString();
source_contact.phone = task.opened_by.mobile_phone.toString();
//Collects the variable data from the RITM ticket. Works for Variables and variable sets
var question_answers = new GlideRecord('sc_item_option_mtom');
question_answers.addQuery('request_item', task.request_item.sys_id.toString());
question_answers.orderBy('sc_item_option.order');
question_answers.query();
while (question_answers.next()) {
var varName = question_answers.sc_item_option.item_option_new.name;
var varValue;
//for each variable, check to see if it is a reference type
if (question_answers.sc_item_option.item_option_new.type == '8') { //8 = reference
//is reference, so need to look up the record to get display value
varValue = getReferenceValue(question_answers.sc_item_option.item_option_new.reference, question_answers.sc_item_option.value);
} else if (question_answers.sc_item_option.item_option_new.type == '21'){ //21 = List Collector
if(question_answers.sc_item_option.value != ''){
//is List Collector, so need to look up the record to get display values
var varValueArray = [];
var list = question_answers.sc_item_option.value.split(",");
for (i = 0; i < list.length; i++) {
if(i == 0){
varValueArray.push(getReferenceValue(question_answers.sc_item_option.item_option_new.list_table, list[i]));
}
else{
varValueArray.push(' ' + getReferenceValue(question_answers.sc_item_option.item_option_new.list_table, list[i]));
}
}
var varValue = varValueArray.toString();
}else{
varValue = ''; }
}else if (question_answers.sc_item_option.item_option_new.type == '18') { //18 = Lookup Select Box
varValue = getReferenceValue(question_answers.sc_item_option.item_option_new.lookup_table, question_answers.sc_item_option.value);
} else {
//is not reference, so can grab that value as is
varValue = question_answers.sc_item_option.value.getDisplayValue();
}
if (varName != "" && varValue != "")
source_variables[varName] = varValue;
}
}
function getReferenceValue(table, sys_id) {
var gr = new GlideRecord(table);
gr.get(sys_id);
return gr.getDisplayValue();
}
body.source_variables = source_variables;
body.source_info = source_info;
body.source_contact = source_contact;
outputs.body = JSON.stringify(body);
})(inputs, outputs);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 03:48 AM
I already informed MRVS data is stored in other table and not in "sc_item_option"
So you can use the logic I shared above to get MRVS and get the JSON
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
03-06-2025 08:45 AM
Thank you for marking my response as helpful.
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
03-06-2025 08:49 AM
Hi @Ankur Bawiskar @Chaitanya naram I am able to get the MRVS data in different array but now I want to concatenate that as part of source_variable object itself as all variables we need to pass together along with it's labe : 'OrderVariable1' , i tried a lot but when i try it removes lable, as well removes more than 1 rows from MRVS and keeps only last one. please help, it would be much appreciated.
(function execute(inputs, outputs) {
/* Create the following inputs in your flow to use this script:
task_id = sys_id of your catalog task
short_description = short description of your catalog task
description = description of your catalog task
correlation_id = sys_id of your catalog task
correlation_display = number of your catalog task
integrated = the correlation_id on your catalog task. This assumes that you are storing the Insight fulfillment task sys_id in the correlation_id field on your catalog task.
*/
var body = {};
var all_comments = [];
var all_worknotes = [];
var all_attachments = [];
var source_info = {};
var source_contact = {};
var source_variables = {};
var varValueArray = [];
var varValue;
var OrderVariable1 = {};
//Capture all the comment information on ticket
var comments = new GlideRecord('sys_journal_field');
comments.addQuery('element', 'comments');
comments.addQuery('element_id', inputs.task_id);
comments.addQuery('sys_created_by', 'DOES NOT CONTAIN', 'insight');
comments.query();
while (comments.next()) {
var comment = {};
comment.value = comments.value.toString();
comment.time = comments.sys_created_on.toString();
comment.created_by = comments.sys_created_by.toString();
all_comments.push(comment);
}
//Capture all the work note information on ticket
var work_notes = new GlideRecord('sys_journal_field');
work_notes.addQuery('element', 'work_notes');
work_notes.addQuery('element_id', inputs.task_id);
work_notes.addQuery('sys_created_by', 'DOES NOT CONTAIN', 'insight');
work_notes.query();
while (work_notes.next()) {
var worknote = {};
worknote.value = work_notes.value.toString();
worknote.time = work_notes.sys_created_on.toString();
worknote.created_by = work_notes.sys_created_by.toString();
all_worknotes.push(worknote);
}
//Add comments and work notes to the body
body.comments = all_comments;
body.work_notes = all_worknotes;
//Add in mandatory fields to the body
body.description = checkForNull(inputs.description);
body.short_description = checkForNull(inputs.short_description);
body.correlation_id = checkForNull(inputs.correlation_id);
body.correlation_display = checkForNull(inputs.correlation_display);
body.sys_id = checkForNull(inputs.integrated);
function checkForNull(inputs) {
if (inputs != null || inputs != undefined) {
return inputs.toString();
} else {
return '';
}
}
//Capture all attachments associated to this ticket
var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments('sc_task', inputs.task_id);
while (agr.next()) {
var attachment_body = {};
attachment_body.attachment_name = agr.file_name.toString();
attachment_body.attachment_type = agr.content_type.toString();
attachment_body.attachment_content = attachment.getContentBase64(agr);
all_attachments.push(attachment_body);
}
body.attachments = all_attachments;
//Populates the source_info, source_contact and source_variables objects
var task = new GlideRecord('sc_task');
task.addQuery('sys_id', inputs.task_id);
task.query();
if (task.next()) {
//fields in the source_info and source_contact here are illustrative, you will likely want to amend this to refelct the data you wish to share
source_info.number = task.number.toString();
source_info.raised_by = task.sys_created_by.toString();
source_info.priority = task.priority.toString();
source_info.ritm = task.request_item.number.toString();
source_contact.name = task.opened_by.name.toString();
source_contact.email = task.opened_by.email.toString();
source_contact.phone = task.opened_by.mobile_phone.toString();
//Collects the variable data from the RITM ticket. Works for Variables and variable sets
var question_answers = new GlideRecord('sc_item_option_mtom');
question_answers.addQuery('request_item', task.request_item.sys_id.toString());
question_answers.orderBy('sc_item_option.order');
question_answers.query();
while (question_answers.next()) {
var varName = question_answers.sc_item_option.item_option_new.name;
//for each variable, check to see if it is a reference type
if (question_answers.sc_item_option.item_option_new.type == '8') { //8 = reference
//is reference, so need to look up the record to get display value
varValue = getReferenceValue(question_answers.sc_item_option.item_option_new.reference, question_answers.sc_item_option.value);
} else if (question_answers.sc_item_option.item_option_new.type == '21'){ //21 = List Collector
if(question_answers.sc_item_option.value != ''){
//is List Collector, so need to look up the record to get display values
var list = question_answers.sc_item_option.value.split(",");
for (i = 0; i < list.length; i++) {
if(i == 0){
varValueArray.push(getReferenceValue(question_answers.sc_item_option.item_option_new.list_table, list[i]));
}
else{
varValueArray.push(' ' + getReferenceValue(question_answers.sc_item_option.item_option_new.list_table, list[i]));
}
}
varValue = varValueArray.toString();
}else{
varValue = ''; }
}else if (question_answers.sc_item_option.item_option_new.type == '18') { //18 = Lookup Select Box
varValue = getReferenceValue(question_answers.sc_item_option.item_option_new.lookup_table, question_answers.sc_item_option.value);
} else {
//is not reference, so can grab that value as is
varValue = question_answers.sc_item_option.value.getDisplayValue();
}
if (varName != "" && varValue != ""){
source_variables[varName] = varValue;
gs.info('name of field '+ varName+ ' and value ' + varValue);
}
}
var mrvsJson = task.request_item.variables.ordervariable1;
var parsedData = JSON.parse(mrvsJson);
var arr = [];
for (var i = 0; i < parsedData.length; i++) {
var obj = {};
obj["Part"] = parsedData[i].part; // give the part variable name
obj["Qty"] = parsedData[i].quantity; // give the quantity variable name
arr.push(obj);
gs.info('value of final MRVS ' + JSON.stringify(arr));
}
}
function getReferenceValue(table, sys_id) {
var gr = new GlideRecord(table);
gr.get(sys_id);
return gr.getDisplayValue();
}
body.source_variables = source_variables;
body.OrderVariable1 = arr;
body.source_info = source_info;
body.source_contact = source_contact;
outputs.body = JSON.stringify(body);
gs.info('value of output '+ outputs.body);
})(inputs, outputs);
attaching result and code snapshot with current code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2025 08:54 AM
try this
(function execute(inputs, outputs) {
var body = {};
var all_comments = [];
var all_worknotes = [];
var all_attachments = [];
var source_info = {};
var source_contact = {};
var source_variables = {};
var varValueArray = [];
var varValue;
var OrderVariable1 = {};
// Capture all the comment information on ticket
var comments = new GlideRecord('sys_journal_field');
comments.addQuery('element', 'comments');
comments.addQuery('element_id', inputs.task_id);
comments.addQuery('sys_created_by', 'DOES NOT CONTAIN', 'insight');
comments.query();
while (comments.next()) {
var comment = {};
comment.value = comments.value.toString();
comment.time = comments.sys_created_on.toString();
comment.created_by = comments.sys_created_by.toString();
all_comments.push(comment);
}
// Capture all the work note information on ticket
var work_notes = new GlideRecord('sys_journal_field');
work_notes.addQuery('element', 'work_notes');
work_notes.addQuery('element_id', inputs.task_id);
work_notes.addQuery('sys_created_by', 'DOES NOT CONTAIN', 'insight');
work_notes.query();
while (work_notes.next()) {
var worknote = {};
worknote.value = work_notes.value.toString();
worknote.time = work_notes.sys_created_on.toString();
worknote.created_by = work_notes.sys_created_by.toString();
all_worknotes.push(worknote);
}
// Add comments and work notes to the body
body.comments = all_comments;
body.work_notes = all_worknotes;
// Add in mandatory fields to the body
body.description = checkForNull(inputs.description);
body.short_description = checkForNull(inputs.short_description);
body.correlation_id = checkForNull(inputs.correlation_id);
body.correlation_display = checkForNull(inputs.correlation_display);
body.sys_id = checkForNull(inputs.integrated);
function checkForNull(inputs) {
if (inputs != null || inputs != undefined) {
return inputs.toString();
} else {
return '';
}
}
// Capture all attachments associated to this ticket
var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments('sc_task', inputs.task_id);
while (agr.next()) {
var attachment_body = {};
attachment_body.attachment_name = agr.file_name.toString();
attachment_body.attachment_type = agr.content_type.toString();
attachment_body.attachment_content = attachment.getContentBase64(agr);
all_attachments.push(attachment_body);
}
body.attachments = all_attachments;
// Populates the source_info, source_contact and source_variables objects
var task = new GlideRecord('sc_task');
task.addQuery('sys_id', inputs.task_id);
task.query();
if (task.next()) {
source_info.number = task.number.toString();
source_info.raised_by = task.sys_created_by.toString();
source_info.priority = task.priority.toString();
source_info.ritm = task.request_item.number.toString();
source_contact.name = task.opened_by.name.toString();
source_contact.email = task.opened_by.email.toString();
source_contact.phone = task.opened_by.mobile_phone.toString();
// Collects the variable data from the RITM ticket. Works for Variables and variable sets
var question_answers = new GlideRecord('sc_item_option_mtom');
question_answers.addQuery('request_item', task.request_item.sys_id.toString());
question_answers.orderBy('sc_item_option.order');
question_answers.query();
while (question_answers.next()) {
var varName = question_answers.sc_item_option.item_option_new.name;
if (question_answers.sc_item_option.item_option_new.type == '8') { // 8 = reference
varValue = getReferenceValue(question_answers.sc_item_option.item_option_new.reference, question_answers.sc_item_option.value);
} else if (question_answers.sc_item_option.item_option_new.type == '21') { // 21 = List Collector
if (question_answers.sc_item_option.value != '') {
var list = question_answers.sc_item_option.value.split(",");
for (i = 0; i < list.length; i++) {
if (i == 0) {
varValueArray.push(getReferenceValue(question_answers.sc_item_option.item_option_new.list_table, list[i]));
} else {
varValueArray.push(' ' + getReferenceValue(question_answers.sc_item_option.item_option_new.list_table, list[i]));
}
}
varValue = varValueArray.toString();
} else {
varValue = '';
}
} else if (question_answers.sc_item_option.item_option_new.type == '18') { // 18 = Lookup Select Box
varValue = getReferenceValue(question_answers.sc_item_option.item_option_new.lookup_table, question_answers.sc_item_option.value);
} else {
varValue = question_answers.sc_item_option.value.getDisplayValue();
}
if (varName != "" && varValue != "") {
source_variables[varName] = varValue;
gs.info('name of field ' + varName + ' and value ' + varValue);
}
}
var mrvsJson = task.request_item.variables.ordervariable1;
var parsedData = JSON.parse(mrvsJson);
var arr = [];
for (var i = 0; i < parsedData.length; i++) {
var obj = {};
obj["Part"] = parsedData[i].part; // give the part variable name
obj["Qty"] = parsedData[i].quantity; // give the quantity variable name
arr.push(obj);
gs.info('value of final MRVS ' + JSON.stringify(arr));
}
// Concatenate MRVS data into source_variables
source_variables['OrderVariable1'] = arr;
}
function getReferenceValue(table, sys_id) {
var gr = new GlideRecord(table);
gr.get(sys_id);
return gr.getDisplayValue();
}
body.source_variables = source_variables;
body.source_info = source_info;
body.source_contact = source_contact;
outputs.body = JSON.stringify(body);
gs.info('value of output ' + outputs.body);
})(inputs, outputs);
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