The CreatorCon Call for Content is officially open! Get started here.

Workflow

angelicaodnc
Tera Contributor

Hi! 
I have to set an workflow for a ne catalog item. 

On the portal, the item has a field named spend_category, and it is referencing a custom table named u_spend_category. The table is a custom table, with only 2 columns - u_spend_category and u_responsible_gvcm. u_responsible_gvcm is reference to sys_user table.

u_spend_category                 u_responsible_gvcm

        A                                               Anne

        B                                               John

        C                                               Anne

       ..                                                  ...

I have to set the following in a script in the workflow: when the user chooses from the field spend_category the A or C element, an approval should be requested from Anne. When the user chooses from the field spend_category the B  element, an approval should be requested from John. 

I tried with switch tab from the workflow and it would work if I had few records on the table. But there are almost 50 records, so it has to be done with a script. 

Thank you!
Angelica

1 REPLY 1

Community Alums
Not applicable

Hi @angelicaodnc ,

 

To achieve this behavior using workflow script activity, you can follow these steps:

 

1. Add a workflow activity for script execution:

  • In the workflow editor, drag and drop the "Script" activity onto the canvas.
  • Give a suitable name to the activity, e.g., "Determine Approver".
  • In the "Script" section, write the script that will determine the appropriate approver based on the selected "spend_category."
  • Write the script logic:

    1. You can use a GlideRecord query to fetch the "u_responsible_gvcm" (approver) value from the "u_spend_category" table based on the selected "spend_category" from the catalog item form.

    2. The script will compare the selected "spend_category" with the records in the "u_spend_category" table and fetch the corresponding "u_responsible_gvcm" value (approver).

    3. You can then set the fetched "u_responsible_gvcm" value as the assigned approver for the approval activity.

 

// Get the selected value of "spend_category" from the current request
var spendCategory = current.variables.spend_category;

// Query the "u_spend_category" table to fetch the approver based on the selected "spend_category"
var spendCategoryGR = new GlideRecord('u_spend_category');
spendCategoryGR.addQuery('u_spend_category', spendCategory);
spendCategoryGR.query();

// Check if the record with the selected "spend_category" exists
if (spendCategoryGR.next()) {
    // Fetch the approver (u_responsible_gvcm) from the "u_spend_category" table
    var approver = spendCategoryGR.u_responsible_gvcm;

    // Set the fetched approver as the assigned approver for the approval activity
    current.approval = approver;
} else {
    // If the spend_category does not match any records, handle the scenario accordingly (e.g., set a default approver or raise an error).
    // For example:
    gs.addErrorMessage('Invalid spend_category selected. Please choose a valid spend_category.');
    current.setAbortAction(true);
}

 

Make sure correct table and field names are used in above script 🙂 

  • Save the script activity and the workflow.
  • Configure the approval activity:

    1. Add an "Approval" activity to the workflow canvas after the "Determine Approver" activity.

    2. In the "Approval" activity configuration, set the "Assigned to" field to the "approval" variable to assign the determined approver.

  • Save and activate the workflow.

Now, when users request the catalog item and select a value for the "spend_category" field, the workflow will execute the "Determine Approver" script activity and assign the appropriate approver based on the selected "spend_category." The approval activity will then be routed to the assigned approver for approval.

 

If this helped you in any way, please hit the like button/mark it helpful. Also, don't forget to accept it as a solution. So it will help others to get the correct solution.

 

thanks,

Prasad