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.

Run catalog client scripts on certain tasks in workflow without using short_description

PranavSuj96
Mega Expert

Hey all,

 

I have a requirement where I need to use catalog client scripts to act on certain workflows. Up until now I always used g_form.getValue('short_description') to check for a matching short description before any actions were taken. 

 

However, during code review today it was requested that no hard coded values are used. Is there a way that I can make a catalog client script work on a specific task without checking its description?

 

Thanks,

Pranav

1 ACCEPTED SOLUTION

You can capture the Workflow activity [wf_activity] reference field value by setting a g_scratchpad variable that can be leveraged client-side:

Depending upon how you want to leverage the g_scratchpad variable, you can use UI Policies (not, Catalog UI Policies) to do this. For this example, I am going to have a variable called sample_variable become mandatory for the Workflow Activity that is named "Sample Task".

1. Create a Business rule that captures the wf_activity name:
Name: Set g_scratchpad for sc_task
Table: Catalog Task [sc_task]
Advanced: true
When: Display
Script:

(function executeRule(current, previous /*null when async*/) {
    g_scratchpad.wf_activity = current.wf_activity.getDisplayValue();
})(current, previous);

2. Create a UI Policy on the Catalog Task table that makes the variable mandatory when closing:

Make Variables Mandatory on Close
Table: Catalog Task [sc_task]
Reverse if false: true
Order: 300
Global: true
Short description: FP - Make Variables Mandatory on Close
Conditions:
State | is | Closed Complete
Run scripts: true
Execute if True:

function onCondition() {
    if (g_scratchpad.wf_activity == 'Sample Task') {
        g_form.setMandatory('variables.sample_variable', true);
    }
}

Execute if False:

function onCondition() {
    g_form.setMandatory('variables.sample_variable', false);
}

3. Change the existing UI Action used to close the task to include the logic to make the variable mandatory:

Close Task
Name: Close Task
Table: Catalog Task [sc_task]
Order: 100
Action name: close_task
Client: true
Onclick: closeTaskCheck();
Script:

function closeTaskCheck() {
    if (g_scratchpad.wf_activity == 'Sample Task') {
        g_form.setMandatory('variables.sample_variable', true);
    }

    gsftSubmit(null, g_form.getFormElement(), 'close_task');
}
if (typeof window == 'undefined') serverCloseTask();

function serverCloseTask() {
    current.state = 3;
    current.update();
}

Let us know if there are any other scenarios that could be covered by this technique.

View solution in original post

10 REPLIES 10

I also want to set a previously read only variable to not read only for a specific task.

You can capture the Workflow activity [wf_activity] reference field value by setting a g_scratchpad variable that can be leveraged client-side:

Depending upon how you want to leverage the g_scratchpad variable, you can use UI Policies (not, Catalog UI Policies) to do this. For this example, I am going to have a variable called sample_variable become mandatory for the Workflow Activity that is named "Sample Task".

1. Create a Business rule that captures the wf_activity name:
Name: Set g_scratchpad for sc_task
Table: Catalog Task [sc_task]
Advanced: true
When: Display
Script:

(function executeRule(current, previous /*null when async*/) {
    g_scratchpad.wf_activity = current.wf_activity.getDisplayValue();
})(current, previous);

2. Create a UI Policy on the Catalog Task table that makes the variable mandatory when closing:

Make Variables Mandatory on Close
Table: Catalog Task [sc_task]
Reverse if false: true
Order: 300
Global: true
Short description: FP - Make Variables Mandatory on Close
Conditions:
State | is | Closed Complete
Run scripts: true
Execute if True:

function onCondition() {
    if (g_scratchpad.wf_activity == 'Sample Task') {
        g_form.setMandatory('variables.sample_variable', true);
    }
}

Execute if False:

function onCondition() {
    g_form.setMandatory('variables.sample_variable', false);
}

3. Change the existing UI Action used to close the task to include the logic to make the variable mandatory:

Close Task
Name: Close Task
Table: Catalog Task [sc_task]
Order: 100
Action name: close_task
Client: true
Onclick: closeTaskCheck();
Script:

function closeTaskCheck() {
    if (g_scratchpad.wf_activity == 'Sample Task') {
        g_form.setMandatory('variables.sample_variable', true);
    }

    gsftSubmit(null, g_form.getFormElement(), 'close_task');
}
if (typeof window == 'undefined') serverCloseTask();

function serverCloseTask() {
    current.state = 3;
    current.update();
}

Let us know if there are any other scenarios that could be covered by this technique.

@ccajohnson It worked. Thank you!

 

Clare Ricketts
Kilo Explorer

There's a column on the sc_task table called user_input, that you can populate in the Advanced script of each task box on your workflow.

Unfortunately, user_input is not available on the task form, so you will have to use a glideRecord to access it in your client script.

Nolan3
Mega Guru

Can you provide me the code you used in a catalog client script to enforce a variable on a task to be mandatory when the short description of a task equals a known value?

 

I have the below script that works to make a variable for PC name mandatory when a task is closed but need to add the logic to only do it on a task that has the short description of "Non-Employee Onboarding - VPN setup" and not sure how to accomplish that.

Thanks for any help you can provide. 

 

function onSubmit() {
	
	var state = g_form.getValue('state');
	
	// Check if State is Closed Complete
	if(state == '3')
	{
    
	var submitStatus = true;
	
	var account = g_form.getValue('non_employee_pc_name');
	
	if(account == '')
	{
      submitStatus = false;
	  g_form.showErrorBox("non_employee_pc_name", "PC name Required to Complete Task");
	}
	}	
		
	return submitStatus;
}