- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
I have a variable set attached to 1 catalog item and there is flow designer that is integrated with othe 3rd party for 2 catalog items. i have variable which might have value for that variable set sometime but sometime it might be empty. so the code is failing to concatenate both arrays of both MRVS if 2nd one is empyt or is not present in catalog item because it is being shown based on need using UI policy. please advase what should we do to combine both arryas if MRVSJSON1 is empty or undefined in below code. right now flow designer is throwing inexpected error if we have mrvsjson1 as empty or not present in catalog item because of which payload is not getting sent over for 1st MRVS even if it has a value.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
8 hours ago
can you give me your business requirement and explain in detail what's required?
I assume this is what you want
-> 2nd MRVS or 1st MRVS if empty should not be appended
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
try this
// MRVS 1
var mrvsJson = task.request_item.variables.ordervariable1 || '[]';
var arr = [];
try {
var parsedData = JSON.parse(mrvsJson);
if (parsedData && parsedData.length) {
for (var i = 0; i < parsedData.length; i++) {
arr.push({
Part: parsedData[i].part,
Qty: parsedData[i].quantity
});
}
}
} catch (e) {
gs.error('Error parsing ordervariable1 MRVS: ' + e.message);
}
// MRVS 2 (may be missing / empty)
var arr1 = [];
var mrvsJson1 = task.request_item.variables.request_standalone_monitor;
// only parse if it exists and is non-empty
if (mrvsJson1) {
try {
var parsedData1 = JSON.parse(mrvsJson1);
if (parsedData1 && parsedData1.length) {
for (var j = 0; j < parsedData1.length; j++) {
arr1.push({
Part: parsedData1[j].part1,
Qty: parsedData1[j].qty
});
}
}
} catch (e2) {
gs.error('Error parsing request_standalone_monitor MRVS: ' + e2.message);
}
}
// both arr and arr1 are guaranteed arrays here
var combinedArr = arr.concat(arr1);
// assign JSON string to output
source_variables.OrderVariable1 = JSON.stringify(combinedArr);
gs.info('Final combined OrderVariable1: ' + source_variables.OrderVariable1);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
Hi @Ankur Bawiskar i tried this, it is returing empty value for combined array if both variables are presnet
and error if 2nd one is missing or not presnet. please help.
below is the full code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
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;
// 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(v) {
if (v != null && v != undefined)
return v.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();
// Collect the variable data from the RITM
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') { // 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') { // List Collector
varValueArray = [];
if (question_answers.sc_item_option.value != '') {
var list = question_answers.sc_item_option.value.split(',');
for (var i = 0; i < list.length; i++) {
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') { // 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;
}
}
// ---------- MRVS HANDLING START ----------
function normalizeMrvs(v) {
if (!v) return []; // null / undefined / ''
if (typeof v === 'object') { // already array/object
return v;
}
// string -> try JSON
try {
var parsed = JSON.parse(v);
return Array.isArray(parsed) ? parsed : [];
} catch (e) {
gs.error('Bad MRVS JSON: ' + e.message + ' value=' + v);
return [];
}
}
// MRVS 1: ordervariable1
var raw1 = task.request_item.variables.ordervariable1;
gs.info('Raw MRVS ordervariable1 type=' + (typeof raw1) + ' value=' + raw1);
var rows1 = normalizeMrvs(raw1);
var arr = [];
for (var i1 = 0; i1 < rows1.length; i1++) {
arr.push({
Part: rows1[i1].part,
Qty: rows1[i1].quantity
});
}
// MRVS 2: request_standalone_monitor
var raw2 = task.request_item.variables.request_standalone_monitor;
gs.info('Raw MRVS request_standalone_monitor type=' + (typeof raw2) + ' value=' + raw2);
var rows2 = normalizeMrvs(raw2);
var arr1 = [];
for (var j = 0; j < rows2.length; j++) {
arr1.push({
Part: rows2[j].part1,
Qty: rows2[j].qty
});
}
// Combine both arrays
var combinedArr = arr.concat(arr1);
source_variables.OrderVariable1 = JSON.stringify(combinedArr);
gs.info('Final combined OrderVariable1: ' + source_variables.OrderVariable1);
// ---------- MRVS HANDLING END ----------
}
function getReferenceValue(table, sys_id) {
var gr = new GlideRecord(table);
if (gr.get(sys_id))
return gr.getDisplayValue();
return '';
}
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 as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday