Add Approvals to request when all the associated tasks are completed.

Add Approvals to request when all the associated tasks are completed.

Requirement:
Request approvals have to be added only after all the associated tasks of a request are completed.
If the approvals are added via the request workflow, then it would be automatically triggered when a request is created.  To avoid this, we need to add the approvals in request additional fields.

These can either be added directly or a FAFR script can be used to load all the users in the organisation with their user ids.

Execution Steps:
  1. Create new additional fields for the request to hold the approver information and update the API name in the script.
  2. Goto:  Admin > Incident Additional Fields

  3. Each stage must be configured as a separate additional field.
  4. Connect to Db and execute the below query.  Restart your instance for this to take effect.
    1. update globalconfig set paramvalue=true where parameter='AllowUserAPIInRequesterLogin'
  5. Go to Admins > Status > Create a custom request status "Waiting for approvals".
  6. Go to Admin > Service Templates > Select a particular template > Field and Form Rules > Rule execution - On Create / Edit > Event - "On Form Load" >> Action > Execute Script > Use the FAFR script.
    1. var fafrKeyList = ["WorkOrder_Fields_UDF_CHAR1", "WorkOrder_Fields_UDF_CHAR10", "WorkOrder_Fields_UDF_CHAR13", "WorkOrder_Fields_UDF_CHAR2", "WorkOrder_Fields_UDF_CHAR4"];
      var selectMultiple = true;
      
      fafrKeyList.forEach(listUsers);
      
      function listUsers(fafrKey) {
          var cs_value = $CS.getValue(fafrKey);
          $CS.referField(fafrKey, "users", {
              url: "/api/v3/users",
              multiple: selectMultiple,
              needID: true
          });
          if (cs_value !== "") {
              var arr = cs_value.split(",");
              var arr_length = arr.length;
              var list = [];
              for (var i = 0; i < arr_length; i++) {
                  list.push({
                      id: arr[i],
                      text: arr[i]
                  });
              }
              $CS.element(fafrKey).select2("data", list);
          } }
  7. Goto Admin > Developer Space > Global function > New > Paste the content from the attachment  GF_4 - Configuration.txt  and save it with a name.
  8. Update the URL and integration key in the script. 
    1. You can get the integration key under Admin > Integrations > Integration Key > New
    2. Please use an integration key with admin privilege, as there wont be any role related permission issues. 

    3. Update the integration key in the global function.

  9. Goto Admin > Developer Space > Request Custom Function > New > Paste the "setApprovals.txt" content and save it with a name.
  10. Goto Admin > Developer Space > Task Custom Functions. Create a new function.
    1. Refer to the attached script. (addApprovals.txt.)
    2. Update the global function count and custom request status name in the script as per your instance.
  11. Configure Task custom trigger to get executed on every task being closed with "Module" as "Request".
  12. Configure Request custom trigger to get executed every time the request is moved to the custom status.