Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

complted tasks are also getting changed to closed incomplete if request is cancelled.

Debasis Pati
Tera Guru

Hello All,

I saw one scenario where if i cancel a request which has completed tasks and work in progress catalog tasks for all the tasks it makes as closed incomplete.
where i want it should not close incomplete the closed completed tasks it should only update the incomplete tasks(active true) to closed incomplete.

where is this oob behaviour present which makes this changes?

@Ankur Bawiskar  any suggestion on this?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Debasis Pati 

see if this OOTB BR does this "request closure"

Also you can try to enable debug business rule and see which BRs are triggering

AnkurBawiskar_0-1761570450045.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

23 REPLIES 23

@Debasis Pati 

did you try to set hard-coded value in comment and work_notes?

how about other fields are getting set like stage or state?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar 
Yes i tried hard coating the value of comment and worknote tried thats also not working but yes state and stage is getting set

@Debasis Pati 

from UI you are able to add work notes and comment?

try this

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    // var comment = current.comments.getJournalEntry(1);
    var comment = "test comment";
    gs.info("Latestcomment: " + comment);

    // Update active Catalog Tasks
    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('request', current.sys_id);
    taskGR.addQuery('active', true);
    taskGR.query();
    while (taskGR.next()) {
        gs.info("Updating task: " + taskGR.number);

        taskGR['work_notes'].setJournalEntry(comment);

        taskGR.setValue('state', '4');
        taskGR.update();
    }

    // Update active RITMs
    var ritmGR = new GlideRecord('sc_req_item');
    ritmGR.addQuery('request', current.sys_id);
    ritmGR.addEncodedQuery('active=true');
    ritmGR.query();
    while (ritmGR.next()) {
        gs.info("Updating RITM: " + ritmGR.number);

        ritmGR['comments'].setJournalEntry(comment);

        ritmGR.setValue('stage', 'Request Cancelled');
        ritmGR.setValue('state', '4');
        ritmGR.update();
    }



})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

ChallaR
Giga Guru

Hi @Debasis Pati .

Identify the Source of the Behavior

1. Check Business Rules on sc_req_item (Request Item Table)

  • Go to System Definition > Business Rules.
  • Filter by Table = sc_req_item.
  • Look for rules with names like:
    • Cancel Request
    • Update Task States
    • Close Catalog Tasks
  • Open each rule and check the Script section for logic that updates sc_task records.

2. Check Business Rules on sc_task (Catalog Task Table)

  • Same steps as above, but filter by Table = sc_task.
  • Look for rules that trigger on update or state change.
  • See if any rule is setting state = Closed Incomplete without checking if the task is already Closed Complete.

3. Check Workflows or Flow Designer Flows

  • If your catalog item uses Workflow:
    • Go to Workflow Editor.
    • Open the workflow associated with the catalog item.
    • Look for a "Cancel Request" or "Close Tasks" activity.
  • If using Flow Designer:
    • Go to Flow Designer > Catalog Item Flows.
    • Open the flow tied to the item.
    • Look for steps that update task states.

4. Check Script Includes or UI Actions

  • Go to System Definition > Script Includes.
  • Search for keywords like cancel, closeTasks, updateTaskStates.
  • Also check UI Actions on sc_req_item that might trigger cancellation logic.

FIX ----

Once you find the script or flow that updates task states, modify the logic like this:

 
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', current.sys_id);
taskGR.query();
while (taskGR.next()) {
   if (taskGR.active == true && taskGR.state != 3) { // 3 = Closed Complete
      taskGR.state = 4; // 4 = Closed Incomplete
      taskGR.update();
   }
}

NOTE -

This ensures only active and incomplete tasks are updated.

 

Please mark as complete and close the thread if this is helpful.

 

Thanks,

Rithika.ch