Reference Catalog Item Variables from workflow

ankitamin
Kilo Contributor

Hi I am trying to create a new branch on my service catalog request workflow to differentiate between Service Requests and Purchase requests to get rid of the "automatically approved <$1000" step on a service request.

I created a variable set that I am applying to all of my service request catalog items that contains a hidden variable (Yes/No) called service_req_id and I am setting the default value to Yes (doing the opposite for purchase requests). Then I am creating an If condition workflow activity to check if this variable is Yes or No and return the correct answer to branch my workflow.

Unfortunately I am getting an "undefined" response when trying to get the value of the service_req_id variable. From what I understand it is because that variable is on the sc_req_item table and the workflow is running on the sc_request table. Is there any way to reference my variable on the sc_req_item table or is there a better approach for what I am trying to accomplish?

Here is the code that I am using:

//Checks if the requested item is a service request or not to branch off the service catalog workflow between service requests and purchase requests

var request = new GlideRecord('sc_request');

answer = check();

function check() {
//Check if the requested item is a service request

//service_req_id is a hidden variable on the catalog item that is set to Yes for service request items and No for purchase requests

if (current.variables.service_req_id != "undefined" && current.service_req_id == 'Yes') {

return 'yes';

}

return 'no';
}

1 ACCEPTED SOLUTION

I'm not sure I understand your request, but this might help:



Catalog items define a form available in your catalog


When users complete the catalog form and submit it, two records are generated: Request (the cart) and Requested item (the specific form they completed)


Variables live on the requested item


Upon submission, the first workflow to run in on the Request. If you want to reference variables on the Requested Items under the request, you must use a GlideRecord query to find all requested items where the request is the current record and then check their variables:


var r = new GlideRecord('sc_req_item');


r.addQuery('request',current.sys_id);


r.query();


while(r.next()){


if(r.variables.variable_name....)....


}


After the Request workflow completes and is approved, the requested item workflow runs. From this workflow, it is simple to reference the variables: current.variables.variable_name.



Hope that helps


View solution in original post

8 REPLIES 8

You can try like below.



var getRITM = new GlideRecord('sc_req_item');


getRITM.addQuery('request',current.sys_id);


getRITM.query();


if(getRITM.next()){


var itemvar = new GlideRecord('sc_item_option_mtom');


itemvar.addQuery('request_item',getRITM.sys_id);


itemvar.addQuery('sc_item_option.item_option_new','sys_id of yes/No variable');


itemvar.query();


if(itemvar.next()){


if(itemvar.sc_item_option.value == "Yes"){


return 'yes';


}


return 'no';


    }


}


Here is the code that I ended up using on the service catalog workflow to distinguish a purchase vs service request. serv_req_id is a custom variable on the catalog item.



answer = check();




function check() {




var gr_item = new GlideRecord('sc_req_item');


gr_item.addQuery('request',current.sys_id);


gr_item.query();


while(gr_item.next()){


var serv_req_id = gr_item.variables.service_req_id;


if (serv_req_id != "undefined" && serv_req_id == 'Yes') {


return 'yes';


}


}


return 'no';


}


Kristen,

Thanks for your input on this info.  I have been struggling to find out needed info about the Catalog Item REQ\RITM process recently.  If I understand you correctly, the REQ record gets created before the RITM record?  I am working on some code right now and having trouble with the referencing the variables on the RITM that are passed from the Catalog Item.  On my Catalog Item from, External Caller is a field that can be selected.  If it is selected, then the "Employee Name" field is removed and the "Alternative Caller" field becomes visible.  The user must enter a value into one of these.  

Right now, the external/alternative caller info shows correctly in the Variables section on the RITM however the information on the RITM/REQ are populating the Requested For info based off of who opened the REQ/RITM and not populating the External/Alternative fields like I need.  So I have created a Business Rule on the RITM to help fix the problem but I am struggling with getting it to work.  

I cant find anyway to control this with filter conditions, unless I am just not dot walking to the necessary related fields.  So I am trying to script this process.  The Business Rule is on the RITM and I am running it After/Insert and the code looks like:

if(current.variables.external_caller == true){
current.variables.external_caller = current.sc_request.u_alternative_caller;

I am not sure if I am using current correctly here but I am trying to say, if the Variables External caller is true then set the field on the RITM named "sc_request.u_alternative_caller" to match it.  Any thoughts?? 

Hi Jonathan,

I like to think of the REQ as the "cart" - in our environment it has almost no useful information. At the REQ level, we don't pay attention to "requested for". At the RITM level, we bypass the issues with the OOB requested for field by creating a customer u_requested_for field and populating it with a insert business rule that reads the variables and sets u_requested_for accordingly.

With respect to your specific need - am I understanding correctly that you're trying to set u_alternative_caller on the request and not the requested item? If yes, how do you handle a situation where the cart has multiple RITMs that might have different people in the variable external caller?

My suggestion would be to set u_alternative_caller on the sc_req_item records under the request instead. Then, your business rule will run on sc_req_item and the script would look like:

if(current.variables.external_caller == true){

current.u_alternative_caller = current.variables.u_alternative_caller;

}

I think I'm understanding your variables correctly (external_caller is a true/false and u_alternative_caller is both in your variable set and a custom field available on the request table.