How can I access a catalog variable from another catalog task in a client script?

KevinCuster
Tera Contributor

Hi,

I have a workflow that creates 2 catalog tasks in parallel. One is for production fulfillment and one is for sub-prod fulfillment. The fulfiller has to enter a CI name on each task form through an fsv variable. One form is the prod CI name, the other is for the subprod CI name.

I need to create a client script on the task form that checks to make sure the Prod and Subprod CI name fields aren't the same from the two catalog tasks.

In my workflow I have only exposed the relevant fields to the Create Task activities (i.e. Prod FSV for the Prod Task, Subprod FSV for the Subprod Task).

In my client script I've added some debug to see if I can get the values from the other task variables like so:

		alert('Prod:    ' + g_form.getValue('variables.fsv_prod_partition_name') + '\n' +
			  'SubProd: ' + g_form.getValue('variables.fsv_subprod_partition_name'));

However, the alert doesn't get the value for the opposing fsv field. i.e., If I'm on the Prod form, the fsv_subprod_partition_name is Null even if I populate it and close the subprod task first.

What are my options here?

Thanks!

3 REPLIES 3

Allen Andreas
Administrator
Administrator

Hi...so are you saying basically on the cat_item (way back at the beginning of the request), you have like 2 variables (1 for each task) created, but hidden while the end-user fill out the form, and then on the task, you expose those 2 variables (again 1 for each task) on to their respective forms...and you're trying to capture the values from within once the technician has filled in the data? And you're not getting anything?

Try doing g_form.getValue('name') to see if that works since you're on the task form running that script. You can access variables directly while in Task table or extended from. 

https://docs.servicenow.com/bundle/helsinki-application-development/page/script/client-scripts/refer...

Another thought is....could you not expose the other variable to that task but make it read-only? So that they could see it and not make it the same name?

Hopefully I make sense, but I'm unsure if this is what you've done or not.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

miked_jones
Giga Expert

Your script is not returning a value because, from the context of the task, the opposite variable does not exist on the form, so g_form has no value to return. The easiest way to do what you want would be to add the variable to both tasks, make sure both tasks have unique short descriptions and then set a client script to run on tasks, check for the short_description and make the one you don't want active read-only or hidden. You could then reference both variables in your client script.

The would be the risk that a value could be set while the task is open in the browser with that approach - the value is only checked during the onload so, if the other task were open at the same time and closed with an updated value, you could still wind up with a duplicate in your second task. 

The harder way to go would be to use a GlideAjax call to reference your associated RITM and pull back the current value of the other variable from the variable_pool and do your check that way. You'd always be checking the current value of the variable in that scenario however, so the chance of a duplicate would be significantly lessened. 

patricklatella
Mega Sage

Happened to find this thread for a requirement that I have whereby I need to have a couple variables that need to be shown and populated on a catalog task.  I then need to retrieve the variable values entered after the task is closed.  I achieved this by setting a workflow scratchpad variable in the advance script of the catalog task in the workflow...and then in my Run Script I run a GlideRecord lookup of that catalog task and grab the variable value using task.variables.myVariableName;

 

 

So in the catalog task this script sets the scratchpad variable:

workflow.scratchpad.taskid = task.setNewGuid();

 

 

Then in my Run Script activity that runs after the catalog task is completed (and the variable value has been set)...I use this script to log the value of the variable.  Use the variable name that you want the value from your catalog item that is running the workflow...in my example below the variable name is "tdm_any_new_ssns".

 

var taskSysId = workflow.scratchpad.taskid;
var task = new GlideRecord('sc_task');
task.addQuery('sys_id', taskSysId);
task.query();

if(task.next()){
gs.log('found the task = '+task.number+' and tdm_any_new_ssns value is '+task.variables.tdm_any_new_ssns);
current.variables.
}

 

Log returns the value from the variable correctly.

Hope this helps.