How to make variable mandatory only after approval has been given and task created

kdelbridge
Tera Expert

I have a request on a new catalog item for one of the variables to only be visible at the RITM  / SCTASKs  level.

  • the variable will actually be completed during TASK 1

I am trying to figure out a way for this variable to NOT be mandatory until AFTER the approval has taken place and the catalog task has been created.

 

Once the 1st catalog task is created, this variable needs to be visible and editable so it can be filled in as part of completion of task one. 

 

 

Any assistance anyone can offer will be greatly appreciated.

5 REPLIES 5

Sorry for my delay on this! I ended up doing a little more investigation than I thought based on some unexpected results.

 

From my testing, my first suggestion of creating the "Item Approved" field isn't possible without scripting. Unfortunately Flow Designer doesn't have an action for updating Catalog Item Variables. If you want to go down this path, you'll have to create a custom action (which requires scripting). This article here describes the process in depth.

 

With that said, the easiest option is a combination of UI Policies and Client Scripts. Follow these steps:

 

  1. Navigate to your Catalog Item.
  2. Under the Catalog UI Policies related list, click New to create a new UI Policy.
  3. Give your policy a unique description, something like "Hide <variable_name> Variable," where <variable_name> is the name of the variable you wish to make mandatory.
  4. Ensure that Applies on a Catalog Item view is CHECKED, and both Applies on Catalog Tasks and Applies on Requested Items are UNCHECKED. All other values can be left at their defaults, and no conditions are needed. This will ensure that the field is hidden on the Catalog Item form, and only visible on the Requested Item and Catalog Task.
  5. Save the record. Reopen the policy and scroll to the bottom. Under the Catalog UI Policy Actions related list, select New.
  6. Under Variable name select your variable, and select False for Visible. Submit the record.
  7. Navigate back to your catalog item.
  8. Scroll to the bottom and select New under the Catalog Client Scripts related list.
  9. Give your script a descriptive name, something like "Make <variable_name> Mandatory."
  10. For Type select onLoad.
  11. Uncheck Applies on a Catalog Item view, and check both Applies on Requested Items and Applies on Catalog Tasks. This will ensure that this script only runs when viewing the Requested Item or Catalog Task.
  12. Next, add the following script and replace <variable_name> with your catalog variable:
function onLoad() {

    // We need to check if this is for the RITM record first.
    //
    var recItemTable = 'sc_req_item',
        currentTable = g_form.getTableName();

    if (currentTable == recItemTable) {

        // Pull the current stage, and create an array of stages prior to approval.
        // Add any stages prior to approval to this array.
        //
        var currentStage = g_form.getValue('stage'),
            preApprovalStages = ['waiting_for_approval'];

        // Check to see if the current stage is in the array. If it is (true),
        // Then we can assume that we are still awaiting approval.
        //
        if (preApprovalStages.includes(currentStage))
            return; // Exits script without setting mandatory.

    }

	// This will make the Catalog Task variable mandatory as well, and since
	// we assume the catalog task is created after approval, we don't need
	// to perform any additional checks.
	//
    g_form.setMandatory('<variable_name>', true);

}

 

That's it! Save the record, and see attached for some screenshots, and below for a more detailed explanation.

 

Some explanation of this method:

Essentiality we need to perform a client-side (make field mandatory) action based on a server-side event (approval). We can't use something like a Display Business Rule, as that would impact the entire RITM/SCTASK table and would be considered bad practice (In my opinion). Flow Designer will also not work, as it can't mark fields as mandatory. A UI Policy or Client Script is our best option.

 

When running client-side, we only have the data that is available on our form. Anything server-side is unavailable unless we execute a GlideAjax call, which is advanced and taxing on performance (not recommended). Instead, we can make use of what fields are already available on the form.

 

OOTB the Approval field is NOT on the form (as I incorrectly assumed previously), so our only option is to use the Stage field on the RITM record (This assumes you have Stages configured for your flow. If not, see here).

 

First we create a UI Policy to hide the field on the Catalog Item form. Then we create our onLoad client script to mark the field mandatory. The script can be updated to adjust the stages as needed. 

 

You can always update the fields available on the form which will allow you to use something else other than Stage in your client script, but I tried to keep this as OOTB as possible.

Hope this helps! During my testing I had this work without issue. If you have any additional questions please let me know.