Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Setting a variable in a business rule

perlguy
Kilo Explorer

I am trying to set a variable inside of a business rule, but it does not seem to be working and I am not sure why.

In my logs, I see the "I AM IN HERE" message, but both the *1* and *2* log messages say that current.variable.RequestType is "undefined".

What am I missing here?

Thanks!


Here is the code...



setRequestType();

gs.log("*1* The Request Type is: " + current.variables.RequestType );

function setRequestType() {
gs.log(" SETTING REQUEST TYPE ");

// Loop through all of the variables on the submission form
for(var variable in current.variable_pool) {

if( variable.indexOf("change_employee_") > -1) {
current.variables.RequestType = "ChangeToEmployee";

gs.log("∞∞∞∞∞∞∞∞ I AM IN HERE!!! ∞∞∞∞∞∞∞∞" );
gs.log("*2* The Request Type is: " + current.variables.RequestType );
break;
}
else if( variable.indexOf("conference_room_") > -1) {
current.variables.RequestType = "ConferenceRoomReservation";
break;
}

// If NOTHING matched, then we throw an error message.
else {
current.variables.RequestType = "ERROR";
break;
}
}
}

4 REPLIES 4

justin_drysdale
Mega Guru

Does the variable 'RequestType' exist on the form/table you are running your business rule against? If yes then try:

current.variables.getDisplayValue('RequestType');

One other observation:
Your for loop is of the type that enumerates object values, you dont need the 'var' in front of variable. I think you are getting undefined because the test condition is incorrect.

Try:



var tmp = current.variable_pool;
for ( variable in tmp) {
if ( tmp[variable].indexOf("change_employee_") > -1) {
gs.log("88888888 I AM IN HERE!!! 88888888" );
gs.log("*2* The Request Type is: " + current.variables.RequestType );
break;
}
//same thing with the next conditional:
if ( tmp[variable].indexOf("conference_room_") > -1) {
//etc
}


perlguy
Kilo Explorer

Okay, I am still confused about several things.

I didn't realize that you had to have a field in a database table to create a variable, I get that now.

One thing that I don't get is HOW can I add a field to a table?

However, with that said, I probably don't need to set a variable to kick off the proper workflow.

What I am most confused about is this:

* I choose a Catalog Item where I fill in a form to add a new employee.
* This Catalog Item is linked to a Workflow named "New Hire 001"

But, when I look into my logs, not only is the "New Hire 001" workflow kicked off, but so is one called "Purchasing Order" and one called "Service Catalog Request".

Those other 2 workflows are not linked to the Catalog Item that I chose!

What I'd like to do is right at the beginning of my workflows, is to check a variable or something so that I can see which form was actually submitted. If the workflow doesn't need to run then, I can just route it to the "end".

This is all really confusing and frustrating.

I appreciate your help!

Thanks.


gflewis
Kilo Expert

Normally when you submit a catalog request, you should get one workflow for the Request (REQ) and one workflow for each Item (RITM). If your cart contains 1 item there should be 2 workflows. If your cart contains 2 items there should be 3 workflows. "Service Catalog Request" is probably your REQ workflow. I am not sure about "Purchasing Order". Either there was an extra item in your cart that you did not realize, or this is a workflow which has not been correctly defined.

It sounds like you are trying to solve a problem that is not normally a problem in a correctly configured system:
[quote]If the workflow doesn't need to run then, I can just route it to the "end".[/quote]
If the system is running workflows that do not need to run, then those workflows have not been correctly configured.

Nevertheless, to answer your original questions:

  • There are two ways to define a workflow variable:
    • Use the "Variables" related list at the bottom of the Catalog Item form
    • Use "Edit Catalog Variables" from within the workflow editor

  • You do not need to define a variable to determine for which Catalog Item a Requested Item workflow is running. Just refer to

    current.cat_item
    .


Spot on, I would like to add that there are a few ways to kick off a workflow as well.

1. A catalog item can specify which workflow to run in the "workflow" field.
2. The workflow itself has conditions that can be configured to run when its conditions are met. Check out a workflow, click the gear and select, "Properties". See attached. I bet your purchase order met a condition this way if you only ordered 1 item.

Generally you do not want to have both instantiated for the same item, otherwise you can get dual workflows running.