Request Item not getting approved automatically

ramireddy
Mega Guru

Hi,

When the request is raised, corresponding request item is not getting approved. I can see that, ServiceNow has a in-built business rule "Cascade Request Approval to Request Item", which will automatically approve the underlying request items, when a request is approved.

When the request is approved, though this business rule is getting executed, It's not making the request item to approved. I put up few log statements. In Line no. 25, before updating the record, it has correct workflow stage. But after this line is executed, it's setting back to previous stage itself. It's not throwing any errors as well in Try..catch statements.

/**

* The Request has been approved, rejected or cancelled.   Propogate this state

* change down to the request items.

*/

cascadeRequestApproval();

function cascadeRequestApproval() {

    var gr = new GlideRecord("sc_req_item");

    gr.addQuery("request", current.sys_id);

    gr.query();

   

    while (gr.next()) {

          // We handle this differently for delivery plans vs. workflows

    try

    {

          if (gr.cat_item.workflow && !gr.cat_item.workflow.nil())

      {

      gs.log("1.Worflow exists for BYOD item. Cascading approval to request item");

                  cascadeRequestApprovalWorkflow(gr);

      }

          else

                  cascadeRequestApprovalDeliveryPlan(gr);

        gs.log("4.Cascade Request Item approval status to be updated to " + gr.stage + " step");

                      gr.update();

      gs.log("5.Cascade Request Item approval status is updated to " + gr.stage + " step");   }

    catch(err) {

      gs.log("6.Error in cascade catalog item approval business rule: " + err);

  }

  }

}

/**

* Cascade the request approval to the request item:

*

*     approved -> if item is waiting for approval, get first stage of the delivery plan

*     rejected -> mark item as Request Cancelled

*/

function cascadeRequestApprovalDeliveryPlan(gr) {

    if (gr.cat_item.delivery_plan.nil())

          return;

   

    gr.approval = current.approval;    

    if (gr.stage == "waiting_for_approval" && current.approval == "approved")

          gr.stage = getNextStage(gr);

    else if (current.approval == 'rejected')

          gr.stage = 'Request Cancelled';

}

function cascadeRequestApprovalWorkflow(gr) {

  gs.log("2.Cascade test: Stage: " + gr.stage + ",approval:" + current.approval);

    if (gr.stage == "waiting_for_approval" && current.approval == "approved") {

          gr.approval = "requested";

          gr.stage = "request_approved";

      gs.log("3.Cascade test: Setting stage to request_approved");

    } else if (current.approval == 'rejected') {

          gr.approval = "rejected";

          gr.stage = "Request Cancelled";

    }

}

function getNextStage(reqitem) {

    // return the first delivery task

    var nextStage = "nothing";

    var planID = GlideappDeliveryPlan.resolvePlanID(reqitem);

    var gr = new GlideRecord("sc_cat_item_delivery_task");

    gr.addQuery("delivery_plan", planID);

    gr.orderBy("order");

    gr.query();

    if(gr.next())

          nextStage = gr.name.getDisplayValue();

   

    return nextStage;

}

1 ACCEPTED SOLUTION

Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Ram



I suggest you to try creating a bran new workflow .


A simplified version of the one you have at the moment using just 3 blocks and check if this works.


I've tried on my OOB instance and seems to work.



Have a look.


This is similar to the first part of your workflow right ?



ServiceNow.png



Here's an example



ServiceNow 2.png



After Approval



ServiceNow 4.png



ServiceNow 3.png


View solution in original post

8 REPLIES 8

Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Ram



A couple of quick questions.


1- Did you test on the first scenario (delivery plan) or second (workflow) or both ?


2- In case you are using a workflow. Did you check for stages used internal to your req item workflow ? Maybe there is a stage over there that overrides the one in the script.



RObo


Hi,



Thank you for starting helping me.



1. I am testing it in the case of workflow, since it's causing the issue in the "workflow".


2. Can you please elaborate, how can check for stages used and what is meant by overriding?? If I approve that request item manually, it's getting approved. But approving the request is not auto-approving this request item.


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Ram



If you check the workflow used by the item in the workflow editor, you should be able to se different blocks right ?


Each block has a stage field.


I suppose that if the stage used by the first block is not empty than the system will use that one instead of the one visible on the script.


Here is an example on the 'service catalog Item request' OOB workflow



ServiceNow.png


Hi,



Here is my workflow screenshot. Manager_approval is 1st stage. But it's not the 1st block.   On Approval of request, I expect this request item workflow also to move to "Approved" action. But it stays at "Manager approval" block.



Screen Shot 2016-03-09 at 11.03.16 am.png