workflow.scratchpad and g_scratchpad

Tamara11
Mega Sage

I have a catalog item with a variable that I only want to display on the sctask form of a specific task after an approval request was rejected.  (I tried to do this using a hidden variable but the catalog ui policy script couldn't access it because the variable is hidden?)

In the workflow create task activity, I've assigned the task sys_id to a workflow scratchpad variable.  If I go to wf_context.LIST, I can see that the correct value is stored.

 

I created an on display business rule on the sc_task table to assign the workflow scratchpad value to a g_scratchpad value: 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
// Get the associated sc_req_item
        var workflow = new Workflow();
        var contexts = workflow.getRunningFlows(current.request_item);
        if (contexts && contexts.length > 0) {
            var scratchpad = contexts[0].scratchpad; // Get the first running workflow's scratchpad
            if (scratchpad.equipmentOrderWithSurfaceReplacementTaskSysid) {
                g_scratchpad.taskSysId = scratchpad.equipmentOrderWithSurfaceReplacementTaskSysid;
                gs.info('Populated g_scratchpad.taskSysId: ' + g_scratchpad.taskSysId);
            } }

})(current, previous);

 

Then I tried to use a catalog ui policy script to compare the current task sys_id to my scratchpad variable, but my g_scratchpad variable appears to be undefined according to alert(g_scratchpad...)

function onCondition() {
	alert(g_scratchpad.taskSysId);

	 var currentTaskSysId = g_form.getUniqueValue(); // Get the sys_id of the current sc_task
         var storedTaskSysId = g_scratchpad.taskSysId;   // Get the stored sys_id from workflow scratchpad
         if (currentTaskSysId && storedTaskSysId && currentTaskSysId === storedTaskSysId) {
             g_form.setDisplay('suitable_surface_replacement_tablet_ordered',true);
	     g_form.setMandatory('suitable_surface_replacement_tablet_ordered', true);
         }
}

 

Can someone tell me what I'm doing wrong?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Tamara11 

g_scratchpad variable from display business rule can't be accessed from catalog client script or catalog UI policy.

Use normal onLoad client script on sc_task and then update as this

function onLoad() {
    var currentTaskSysId = g_form.getUniqueValue(); // Get the sys_id of the current sc_task
    var storedTaskSysId = g_scratchpad.taskSysId; // Get the stored sys_id from workflow scratchpad
    if (currentTaskSysId && storedTaskSysId && currentTaskSysId === storedTaskSysId) {
        g_form.setDisplay('variables.suitable_surface_replacement_tablet_ordered', true);
        g_form.setMandatory('variables.suitable_surface_replacement_tablet_ordered', true);
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

@Tamara11 

I already informed g_scratchpad is not accessible in catalog client scripts

So I shared the workaround above.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you, @Ankur Bawiskar !

 

I apologize.  I didn't even see your reply.  I had the page still open and didn't refresh it before posting again.

 

Thanks!

@Tamara11 

No worries.

Glad to help.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader