Need to set value in MRVS via script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 08:26 AM
Requirement:
We have a multi-row variable set on a form. If the user enters more than 1 row on the MRVS, then create a RITM for each row after row 0.
Development so far:
I've been able to script a solution that will kick off a RITM for each row in the MRVS based on a great article by @Vishal Birajdar (Create multiple RITM under single request dependin... - ServiceNow Community)
The script will create the RITM and what I need is to take data from a row in the MRVS and populate the MRVS on the new RITM
Example:
Bob is requesting software for 4 of his colleagues.
On the software form is a MRVS for Bob to enter the users who are getting the software:
In the example above, 3 new RITMs will be created, started with Abe Lincoln, then Quintin, then Zack.
The script will create the 3 RITMs as expected.
I'm able to grab the data for each user, but my script to set the MRVS in the RITM is no populating via a Run Script in workflow editor.
Here's a snippet of the script that I'm trying to use to populate the MRVS:
var v1 = parseMrvs[i].customer_name.toString();
var v2 = parseMrvs[i].network.toString();
var v3 = parseMrvs[i].business_need.toString();
var v4 = parseMrvs[i].justification.toString();
var v5 = parseMrvs[i].nro_customer.toString();
var v6 = parseMrvs[i].customer_phone_number.toString();
var v7 = parseMrvs[i].customer_email.toString();
gs.info('MM in here line 164');
gs.info('MM RITM sys_id ' + ritmSysId);
gs.info('MM v1 ' + v1);
gs.info('MM v2 ' + v2);
var gr = new GlideRecord('sc_req_item');
gr.get(ritmSysId);
gr.query();
gs.info('MM count: ' + gr.getRowCount());
if (gr.next()) {
var mrvs = gr.variables.u_customer_information;
for (var rc = 0; rc < 1; rc++) {
var newRow = mrvs.addRow();
newRow.setCellValue('customer_name', v1);
newRow.setCellValue('network', v2);
newRow.setCellValue('business_need', v3);
newRow.setCellValue('justification', v4);
newRow.setCellValue('nro_customer', v5);
newRow.setCellValue('customer_phone_number', v6);
newRow.setCellValue('customer_email', v7);
// newRow.customer_name = v1;
// newRow.network = v2;
// newRow.business_need = v3;
// newRow.justification = v4;
// newRow.nro_customer = v5;
// newRow.customer_phone_number = v6;
// newRow.customer_email = v7;
}
gr.update();
}
The log statements in the script show up perfectly so the data is there.
Here's the kicker:
When I run the code above in a background script, the MRVS will populate perfectly w/ the data (variables) I'm expecting.
So why would the script run in a background script but not a Run Script in Editor?
Any help is greatly appreciated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 08:49 PM
Hi @magee
If possible can you share the whole run script from workflow...!!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2023 05:02 AM
Hey @Vishal Birajdar
Here's the code so far. Please note there are lots of comments for logs and other stuff
/* ============Start of run script activity===============*/
/* 1. Get values from user list variable */
var v1;
var v2;
var v3;
var v4;
var v5;
var v6;
var v7;
var mrvs = current.variables.u_customer_information.toString(); //customer MRVS name
var parseMrvs = JSON.parse(mrvs); //parse out mrvs
//var array = list.split(',');
gs.info('MM parse mrvs: ' + JSON.stringify(parseMrvs));
/* 2. Get current ritm’s request this will be used to set request on multiple Ritm to be created*/
var request = current.request;
gs.info('MM request: ' + request);
/* 3. Store sys_id of our catalog item */
//var catalogItem = '3cecd2350a0a0a6a013a3a35a5e41c07'; // Not to hardcode (use system property)
var catalogItem = current.cat_item.toString(); //sys_id of catalog item
//var catalogItem = gs.getProperty(‘name of property where sysid of cat item stored’);
/* 4. loop through array we have created in 1 and create RITM */
/* Loop will initialize from 1 because we have creating one ritm from main request. */
for (var i = 1; i < parseMrvs.length; i++) { //if there are 5 records in the MRVS (0 1 2 3 4), this for loop will run 4 times (it will start at row 1)
gs.info('MM parseMrvs length: ' + parseMrvs.length);
/* 4a. Create RITM*/
//Requester Information variables//
var roomNum = current.variables.room_number.toString();
var secEmail = current.variables.secure_email.toString();
var building = current.variables.building.toString();
var userName = current.variables.user_name.toString();
var valOrg = current.variables.validating_organization.toString();
var nonSecPhone = current.variables.non_secure_phone.toString();
var secPhone = current.variables.secure_phone.toString();
gs.info('MM Requester info: ' + 'Room Num: ' + roomNum + ' - ' + 'Secure Email: ' + secEmail + ' - ' + 'Building: ' + building + ' - ' + 'User Name: ' + userName + ' - ' + 'Validating Org: ' + valOrg + ' - ' + 'Non Sec Phone: ' + nonSecPhone + ' - ' + 'Secure Phone: ' + secPhone);
//MRVS variables//
v1 = parseMrvs[i].customer_name.toString();
v2 = parseMrvs[i].network.toString();
v3 = parseMrvs[i].business_need.toString();
v4 = parseMrvs[i].justification.toString();
v5 = parseMrvs[i].nro_customer.toString();
v6 = parseMrvs[i].customer_phone_number.toString();
v7 = parseMrvs[i].customer_email.toString();
gs.info('MM Customer name: ' + v1 + ' Network: ' + v2 + ' Business Need: ' + v3 + ' Justificaiton ' + v4 + ' NRO Customer: ' + v5 + ' Phone: ' + v6 + ' Email: ' + v7);
var grRitm = new GlideRecord("sc_req_item"); //this creates a new RITM
grRitm.initialize();
grRitm.cat_item = catalogItem;
grRitm.price = current.price;
grRitm.request = request;
grRitm.requested_for = v1;
grRitm.due_date = current.due_date;
//Store the sys Id in another variable
var ritmSysId = grRitm.insert(); //insert new RITM
/*End of RITM creation*/
ritmSysId = ritmSysId.toString();
setMRVSvalues(ritmSysId);
/* 4b. Store the variables from catalog Item in an array */
//var itemName = grRitm.cat_item.name;
var itemVar = new GlideRecord('item_option_new'); //this is the variable table
itemVar.addEncodedQuery('variable_set.title=Requester Information^ORvariable_set.title=Customer Information'); //query variables for our variable sets
itemVar.query();
gs.info('MM itemVar length: ' + itemVar.getRowCount());
var itemVarList = [];
while (itemVar.next()) {
itemVarList.push(itemVar.getUniqueValue('name')); //push variable name to itemVarList array. In the example above, there are 14 variables between the two variable sets
}
gs.info('MM itemVarList: ' + itemVarList);
gs.info('MM itemVarList length: ' + itemVarList.length);
/*End of Store the variables from catalog Item in an array */
/* 4c.Create records in sc_item_option */
/* This table contains value of variable filled by end user , In this case we will update it through script */
var itemOption = new GlideRecord('sc_item_option'); //values entered into portal form in the variables
for (var j = 0; j < itemVarList.length; j++) { //if there are 14 variables in the itemVarList array above, this loop will run 14 times
itemOption.initialize(); //create new record on sc_item_option table
itemOption.order = j;
itemOption.item_option_new = itemVarList[j]; //this sets the variable to the variable name in the arrary above
//declare requester variable information for the RITM being created//
if (itemOption.item_option_new == '6dd8b36e47123110d61db7f3846d43b0') { //variable for building
itemOption.value = building; //set variable value for building
}
if (itemOption.item_option_new == 'fcf8fbae47123110d61db7f3846d4358') { //variable for non sec phone
itemOption.value = nonSecPhone; //set variable value for non secure phone
}
if (itemOption.item_option_new == '58a8fb6e47123110d61db7f3846d43fb') { //variable for room number
itemOption.value = roomNum; //set variable value for non secure phone
}
if (itemOption.item_option_new == '6a29bfae47123110d61db7f3846d43cd') { //variable for sec email
itemOption.value = secEmail; //set variable value for secure email
}
if (itemOption.item_option_new == 'e019f36e47123110d61db7f3846d430a') { //variable for sec phone
itemOption.value = secPhone; //set variable value for secure phone
}
if (itemOption.item_option_new == 'cb97736e47123110d61db7f3846d43f4') { //variable for user name
itemOption.value = userName; //set variable value for user name
}
if (itemOption.item_option_new == 'ed58f36e47123110d61db7f3846d4339') { //variable for validating org
itemOption.value = valOrg; //set variable value for validating org
}
// save the record in sc_item_option
var optSysID = itemOption.insert();
/* 4d. Create a relationship of RITM with variables value */
var ritmM2M = new GlideRecord('sc_item_option_mtom');
ritmM2M.initialize();
ritmM2M.request_item = ritmSysId; // Parent Item
ritmM2M.sc_item_option = optSysID; // Dependent Value
ritmM2M.insert();
}
/* 4e. Start the workflow (sys_id of workflow from "wf_workflow" table “Copy of Procurement Process Flow - Hardware”)*/
startWorkflow('492da4c347163110d61db7f3846d4388');
/*for startWorkflow() – Taken reference from OOTB Business rule from RITM table
Business rule : Start Workflow
*/
}
/* 4. End of loop through array we have created in 1 and create RITM */
/* Attach workflow to Ritm */
function startWorkflow(id) {
var grRitm1 = new GlideRecord("sc_req_item");
grRitm1.addQuery("sys_id", ritmSysId);
grRitm1.query();
if (grRitm1.next()) {
var w = new Workflow();
var context = w.startFlow(id, grRitm1, grRitm1.operation(), getVars(grRitm1));
if (context != null)
grRitm1.context = context.sys_id;
workflow.info("context = " + grRitm1.context);
}
grRitm1.update();
}
/* Send the RITM variables to workflow*/
function getVars(grRitm1) {
var vars = {};
for (var n in grRitm1.variables)
vars[n] = grRitm1.variables[n];
return vars;
}
function setMRVSvalues(ritmSysId) {
//set MRVS values in RITM created
gs.info('MM in here line 164');
gs.info('MM RITM sys_id ' + ritmSysId);
gs.info('MM v1 ' + v1);
gs.info('MM v2 ' + v2);
var gr = new GlideRecord('sc_req_item');
gr.get(ritmSysId);
gr.query();
gs.info('MM count: ' + gr.getRowCount());
if (gr.next()) {
var mrvs = gr.variables.u_customer_information;
for (var rc = 0; rc < 1; rc++) {
var newRow = mrvs.addRow();
newRow.setCellValue('customer_name', v1);
newRow.setCellValue('network', v2);
newRow.setCellValue('business_need', v3);
newRow.setCellValue('justification', v4);
newRow.setCellValue('nro_customer', v5);
newRow.setCellValue('customer_phone_number', v6);
newRow.setCellValue('customer_email', v7);
// newRow.customer_name = v1;
// newRow.network = v2;
// newRow.business_need = v3;
// newRow.justification = v4;
// newRow.nro_customer = v5;
// newRow.customer_phone_number = v6;
// newRow.customer_email = v7;
}
gr.update();
}
}
/*Update User list for main (current) RITM*/
//current.variables.user_list = parseMrvs[0];
//current.requested_for = parseMrvs[0];
/*====================End of run script Activity===============================*/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 03:58 AM
Hi @magee
I tried with MRVS , unfortunately I did not find a way to attach MRVS to new RITM from script.
but below is workaround not sure if that helps you or not...
Workaround :
Step 1 : Create new normal variables same as present on MRVS such as Name,Network,Business need etc.
Step 2 : Hide these on Catalog form & make Visible on RITM & Catalog task.
Step 3 : In workflow run script, loop through MRVS (we will get the Data from MRVS) and update the variables created in Step 1 for each RITM (RITM Created through script).
With this we will have data on RITM for separate row of MRVS.
Hope this helps...!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2023 03:21 AM
I'm going to try calling a script include to see if that will add the data to the MRVS. Stand by