I'm trying to automatically add an approval stage in my code, but it doesn't seem to be working as expected

I'm trying to automatically add an approval stage in my code, but it doesn't seem to be working as expected

Here's the code I'm using:
  1. approvalJson = Collection();
  2. approvalStages = Collection();
  3. returnObj = Map();
  4. template_name = requestObj.get("template").get("name");

  5. // Replace department with UDF_REF3 field which is a lookup field
  6. approver = requestObj.get("UDF_REF3").get("name");
  7. // Get the approver email from the lookup field
  8. approver_email = approver.get("email");
  9. if (!"null".equalsIgnoreCase(approver)) {
  10.   // Only add stage1 which will be sent to the user lookup field mentioned above
  11.   stage1 = {"StageOne": [approver_email]};
  12.   approvalStages.put("StageOne", [approver_email]);
  13.   approvalJson.put("approval_stages", approvalStages);
  14. }

  15. if (!approvalJson.isEmpty()) {
  16.   approvalStages = approvalJson.get("approval_stages");
  17.   input_data = {
  18.     "INPUT_DATA": approvalStages,
  19.     "OPERATIONNAME": "SET_APPROVAL_STAGE",
  20.     "send_immediately": "True"
  21.   };
  22.   operations = Collection();
  23.   operations.insert(input_data);
  24.   returnObj = {"operation": operations, "result": "success"};
  25. } else {
  26.   returnObj = {"result": "success"};
  27. }

  28. return returnObj;
The goal is to add an approval stage automatically based on a lookup field called UDF_REF3. However, the code doesn't seem to be functioning as expected, and no approval stages are being added. I've verified that the lookup field is correctly fetched and the email address is extracted.

I'd appreciate any insights or suggestions on what could be causing the issue or any potential improvements to the code.

Thank you!

                New to ADSelfService Plus?