Script not working in Flow Action

mpmacconnell
Tera Guru

I am trying to convert a workflow I have over to using Flow Designer. In our Change Management process, I have a script in the workflow the creates RITM's for a keycard request. I can't use the Submit Catalog Item Request because some of the variables are List Collectors, so need to us a script.

 

I have the following action:

find_real_file.png

In the script, it is emulating the person submitting the RFC to use that account to create the RITM's. This works in the workflow but when I updated the script to use the inputs for Flow actions I get a nullpointer error. If I comment out the impersonation lines, there are no errors, but the RITMS do not get created. Wonder if anyone has ideas. Using the gs.log statements, it does look like it should be working.

 

Here is the script:

 

(function execute(inputs, outputs) {

var requestor = inputs.assignedTo.getDisplayValue();
var cartowner = inputs.assignedTo;
var mgr = inputs.mgr;
var pm = inputs.prgmgr;
var userloc = inputs.usrLocation;
var startdate = inputs.planned_start;
var enddate = inputs.planned_end;
var rfcnum = inputs.rfcNumber;
var rfcsys = inputs.rfcID;
var keydesc = rfcnum + ' - Keycard request for change request.';
var myLogSource = '[RFC CONTACTS LIST KEYCARD REQUEST]';
var locarray = [];
//var effDate = new GlideCalendarDateTime(startdate);
//effDate = effDate.getDate();
var effDate = startdate.getDisplayValue();

var currentdt = new GlideDateTime();
currentdt = startdate.getDisplayValue().split(' ')[0];

//array for location
var cardarray = [];
var ritmneeded = 'false';

//query for the location
var conquery = new GlideRecord('u_rfc_contacts');
conquery.addQuery('u_change_request', rfcsys);
conquery.addQuery('u_key_card_request_needed', true);
conquery.query();
while (conquery.next()) {

var location_name = conquery.u_building.sys_id;
gs.log("Location to check for: " + location_name.getDisplayValue() + ' - ' + location_name, myLogSource);
var arrayUtil = new ArrayUtil();
var a1 = locarray;
var found_id = arrayUtil.contains(a1, location_name);
gs.log('Location array to query: ' + a1, myLogSource);
gs.log('Found location?: ' + found_id + ' for ' + location_name, myLogSource);
if (found_id == false) {
locarray.push(location_name);
}
}

gs.log('Location array: ' + locarray,myLogSource);
//outputs.locations = locarray;

//Create the new cart
session.onlineImpersonate(inputs.assignedTo);
var cart = new Cart ();
cart.setRequestedFor = cartowner;
cart.sysparm_requested_for = cartowner;
cart.special_instructions = "Note: This is a system generated Keycard request for " + inputs.rfcNumber;
cart.location = userloc;
cart.user = cartowner;
cart.owner = cartowner;
cart.update();

//for loop for each location that is needed.
for (var i = 0; i < locarray.length; i++) {

///query the user for the location and add it to user array
var userarray = [];
var suiteslist = [];
var suites = '';
var createritm = '0';
gs.log('Location length: ' + locarray.length, myLogSource);
var locquery = locarray[i];
gs.log('Checking location: ' + locquery, myLogSource);
var userquery = new GlideRecord('u_rfc_contacts');
userquery.addQuery('u_change_request', rfcsys);
userquery.addQuery('u_key_card_request_needed', true);
userquery.addQuery('u_building', locquery);
userquery.addQuery('u_vendor', false);
userquery.query();
while (userquery.next()) {
gs.log('I got here for checking user', myLogSource);
var name = userquery.u_name;
gs.log('User to add: ' + name, myLogSource);
userarray.push(name.toString());
suites = userquery.u_suites_room;
createritm = '1';
}

gs.log('Done Checking locations.', myLogSource);

//Create the new item for each location and add all the users for that location
if (createritm != '0') {
var item = cart.addItem('c0bda6c2b41fa04044e97357a6539f7c');
//cart.owner = requestor;
cart.setVariable(item, 'requested_for', cartowner);
cart.setVariable(item, 'scif_user_type', 'Employee');
cart.setVariable(item, 'sc5_confirmation_begin', 'true');
cart.setVariable(item, 'sc5_action_require', 'Exception to ID Keycard');
cart.setVariable(item, 'sc5_exception_mulitple', 'Yes');
cart.setVariable(item, 'sc5_exception_reason2', 'Changing Access Hours and Location');
//cart.setVariable(item, 'sc5_scif_location_selection', locquery.toString());
cart.setVariable(item, 'sc5_scif_location_selection', locquery);
cart.setVariable(item, 'sc5_scif_exception_users', userarray.toString());
cart.setVariable(item, 'sc5_scif_exception_users', userarray);
cart.setVariable(item, 'sc5_excep_access_hrs', 'Release Coordinator Access');
cart.setVariable(item, 'sc5_start_exception', startdate);
cart.setVariable(item, 'sc5_end_exception', enddate);
cart.setVariable(item, 'sc5_exception_reason', keydesc);
cart.setVariable(item, 'opened_by', cartowner);
cart.setVariable(item, 'location', userloc);
cart.setVariable(item, 'submitted_by', cartowner);
cart.setVariable(item, 'opened_by', cartowner);
cart.setVariable(item, 'sc5_exception_suite', suites);
cart.setVariable(item, 'scif_start_date', currentdt);
cart.setVariable(item, 'scif_time', '06:00:00');
//cart.setVariable(item, '')
cart.update();

gs.log('Created cart', myLogSource);
//Add Item to the Cart
var cartdetails = cart.addToCart(item);
cartdetails.update();

//Update the Cart settings
var cartGR = cart.getCart();
cartGR.requested_for = cartowner;
cartGR.u_impacted_location = userloc;
cartGR.submitted_by = cartowner;
cartGR.user = cartowner;
cartGR.sys_created_by = requestor;
cartGR.opend_by = cartowner;
cartGR.location = userloc;
cartGR.special_instructions = "Note: This is a system generated Keycard request for " + inputs.rfcNumber;
cartGR.update();
}

gs.log('Added to cart', myLogSource);
}

//Place the order
var cartid = cart.placeOrder();
cartid.u_opened_by = requestor;
cartid.owner = cartowner;
cartid.update();
session.onlineUnimpersonate();


})(inputs, outputs);

1 ACCEPTED SOLUTION

mpmacconnell
Tera Guru

We got it working with the following script:

 

(function execute(inputs, outputs) {
var assigned_to = inputs.assignedTo;
var startdate = inputs.planned_start;
var enddate = inputs.planned_end;
var rfcnum = inputs.rfcNumber;
var rfcsysid = inputs.rfcsysid;
var currentdt = new GlideDateTime();
currentdt = startdate.getDisplayValue().split(' ')[0];

var location_temp = [];


var contact_loc = new GlideAggregate('u_rfc_contacts'); //GlideAggregate
contact_loc.addQuery('u_change_request', rfcsysid);
contact_loc.addQuery('u_key_card_request_needed', true);
contact_loc.addQuery('u_vendor', false);
contact_loc.groupBy('u_building');
contact_loc.query();
while (contact_loc.next()) {
location_temp.push(contact_loc.u_building.toString());
}
var location_arr = new ArrayUtil().unique(location_temp);

var hddcart = new Cart(generateId());
//for each location create a RITM for all users in each location
for (var i = 0; i < location_arr.length; i++) {
///query the user for the location and add it to user array
var userarray = [];
var suite = '';
var contact_user = new GlideRecord('u_rfc_contacts');
contact_user.addQuery('u_change_request', rfcsysid);
contact_user.addQuery('u_key_card_request_needed', true);
contact_user.addQuery('u_building', location_arr[i]);
contact_user.addQuery('u_vendor', false);
contact_user.query();
while (contact_user.next()) {
userarray.push(contact_user.u_name.toString()); //array of all users for that particular location.
suite = suite + " " + contact_user.u_suites_room;
}

var item = hddcart.addItem('c0bda6c2b41fa04044e97357a6539f7c');
hddcart.setVariable(item, 'scif_user_type', 'Employee');
hddcart.setVariable(item, 'sc5_confirmation_begin', 'true');
hddcart.setVariable(item, 'sc5_action_require', 'Exception to ID Keycard');
hddcart.setVariable(item, 'sc5_exception_mulitple', 'Yes');
hddcart.setVariable(item, 'sc5_exception_reason2', 'Changing Access Hours and Location');
hddcart.setVariable(item, 'sc5_scif_location_selection', location_arr[i]);
hddcart.setVariable(item, 'sc5_scif_exception_users', userarray.toString());
hddcart.setVariable(item, 'sc5_excep_access_hrs', 'Release Coordinator Access');
hddcart.setVariable(item, 'sc5_start_exception', startdate);
hddcart.setVariable(item, 'sc5_end_exception', enddate);
hddcart.setVariable(item, 'sc5_exception_reason', rfcnum + ' - Keycard request for change request.');
hddcart.setVariable(item, 'sc5_exception_suite', suite);
hddcart.setVariable(item, 'scif_start_date', currentdt);
hddcart.setVariable(item, 'scif_time', '06:00:00');
}
// set the requested for
var cartGR = hddcart.getCart();
cartGR.special_instructions = "Note: This is a system generated Keycard request for " + rfcnum;
cartGR.requested_for = assigned_to;

//cartGR.submitted_by = cartowner;
//cartGR.user = cartowner;
//cartGR.u_impacted_location = userloc;
//cartGR.sys_created_by = requestor;
//cartGR.opend_by = cartowner;
//cartGR.location = userloc;
cartGR.update();

//Place the order
var cartid = hddcart.placeOrder();

outputs.request = cartid;

var ritm = new GlideRecord("sc_req_item");
ritm.addQuery("request", cartid.sys_id);
ritm.query();
while (ritm.next()) {
ritm.contact_type = 'RFC keycard';
ritm.opened_by = assigned_to;
ritm.update();
}

var req = new GlideRecord("sc_request");
req.addQuery("sys_id", cartid.sys_id);
req.query();
if (req.next()) {
req.opened_by = assigned_to;
req.update();
}


// note this is an example.
function generateId() {
var text = "CART-";
var possible = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (var i = 0; i < 7; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}

 

})(inputs, outputs);

View solution in original post

4 REPLIES 4

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi,

Instead of "session.onlineImpersonate(inputs.assignedTo);", try "gs.getSession().impersonate(<sys_id>);"

Well, that did get rid of the nullpointer error. Now I am stuck with the issue that it is not adding the items to the cart and creating the request. I have checked the persons cart and it is empty and the RITM's are not created.

Hi,

Do you get all the gs.log statements as expected?

Your code may be throwing an unhandled error, suggest you wrap the whole lot in a try-catch block and log any error thrown to the system log (I always do this in script steps anyway).

Might give you some more insight as to what is going on, I would suspect that you have a null variable somewhere.

Hope this helps,

Richard

mpmacconnell
Tera Guru

We got it working with the following script:

 

(function execute(inputs, outputs) {
var assigned_to = inputs.assignedTo;
var startdate = inputs.planned_start;
var enddate = inputs.planned_end;
var rfcnum = inputs.rfcNumber;
var rfcsysid = inputs.rfcsysid;
var currentdt = new GlideDateTime();
currentdt = startdate.getDisplayValue().split(' ')[0];

var location_temp = [];


var contact_loc = new GlideAggregate('u_rfc_contacts'); //GlideAggregate
contact_loc.addQuery('u_change_request', rfcsysid);
contact_loc.addQuery('u_key_card_request_needed', true);
contact_loc.addQuery('u_vendor', false);
contact_loc.groupBy('u_building');
contact_loc.query();
while (contact_loc.next()) {
location_temp.push(contact_loc.u_building.toString());
}
var location_arr = new ArrayUtil().unique(location_temp);

var hddcart = new Cart(generateId());
//for each location create a RITM for all users in each location
for (var i = 0; i < location_arr.length; i++) {
///query the user for the location and add it to user array
var userarray = [];
var suite = '';
var contact_user = new GlideRecord('u_rfc_contacts');
contact_user.addQuery('u_change_request', rfcsysid);
contact_user.addQuery('u_key_card_request_needed', true);
contact_user.addQuery('u_building', location_arr[i]);
contact_user.addQuery('u_vendor', false);
contact_user.query();
while (contact_user.next()) {
userarray.push(contact_user.u_name.toString()); //array of all users for that particular location.
suite = suite + " " + contact_user.u_suites_room;
}

var item = hddcart.addItem('c0bda6c2b41fa04044e97357a6539f7c');
hddcart.setVariable(item, 'scif_user_type', 'Employee');
hddcart.setVariable(item, 'sc5_confirmation_begin', 'true');
hddcart.setVariable(item, 'sc5_action_require', 'Exception to ID Keycard');
hddcart.setVariable(item, 'sc5_exception_mulitple', 'Yes');
hddcart.setVariable(item, 'sc5_exception_reason2', 'Changing Access Hours and Location');
hddcart.setVariable(item, 'sc5_scif_location_selection', location_arr[i]);
hddcart.setVariable(item, 'sc5_scif_exception_users', userarray.toString());
hddcart.setVariable(item, 'sc5_excep_access_hrs', 'Release Coordinator Access');
hddcart.setVariable(item, 'sc5_start_exception', startdate);
hddcart.setVariable(item, 'sc5_end_exception', enddate);
hddcart.setVariable(item, 'sc5_exception_reason', rfcnum + ' - Keycard request for change request.');
hddcart.setVariable(item, 'sc5_exception_suite', suite);
hddcart.setVariable(item, 'scif_start_date', currentdt);
hddcart.setVariable(item, 'scif_time', '06:00:00');
}
// set the requested for
var cartGR = hddcart.getCart();
cartGR.special_instructions = "Note: This is a system generated Keycard request for " + rfcnum;
cartGR.requested_for = assigned_to;

//cartGR.submitted_by = cartowner;
//cartGR.user = cartowner;
//cartGR.u_impacted_location = userloc;
//cartGR.sys_created_by = requestor;
//cartGR.opend_by = cartowner;
//cartGR.location = userloc;
cartGR.update();

//Place the order
var cartid = hddcart.placeOrder();

outputs.request = cartid;

var ritm = new GlideRecord("sc_req_item");
ritm.addQuery("request", cartid.sys_id);
ritm.query();
while (ritm.next()) {
ritm.contact_type = 'RFC keycard';
ritm.opened_by = assigned_to;
ritm.update();
}

var req = new GlideRecord("sc_request");
req.addQuery("sys_id", cartid.sys_id);
req.query();
if (req.next()) {
req.opened_by = assigned_to;
req.update();
}


// note this is an example.
function generateId() {
var text = "CART-";
var possible = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (var i = 0; i < 7; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}

 

})(inputs, outputs);