Looking for help with a workflow wait condition script error

Julie Catano
Kilo Guru

In my workflow I created a wait condition to wait until all catalog tasks in workflow and/or ad hoc tasks are completed before changing the RITM to State=Close Complete and Stage= Completed.

See workflow screen shot and scripts below.

 

Wait Condition script in workflow

var sctaskCount,sctaskCount1;

var S = new GlideAggregate (‘sc_task’);

s.addQuery(‘request_item’,current.sys_id);

s.addAggregate (‘COUNT’,’request_item’);

s.query();

while(s.next())

{

sctaskCount = s.getAggregate(‘COUNT’,’request_item’);

 

Var S1 = new GlideAggregate (‘sc_task’);

S1.addQuery(‘request_item’,current.sys_id);

S1.addEncodedQuery(‘stateIN3,4,7’);

while(s1.next())

{

sctaskCount = s1.getAggregate(‘COUNT’,’request_item’);

}

if(sctaskCount == sctaskCount1 )

{

answer = true;

 

}

 

}

 

Business Rule #1

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

 

              // Add your code here

              var SC_task = new GlideRecord('sc_task');

              SC_task.addQuery('active', true);

              SC_task.addQuery('request_item', current.sys_id);

              SC_task.query();

              if(SC_task.next()){

 

                             gs.addInfoMessage('All tasks must be completed before request will close');

                             current.state = previous.state;

                             current.setAbortAction('true');

 

              }

})(current, previous);

 

 

Business Rule #2

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

    // Query to check if there are any open sc_tasks for the current ritm

    var task = new GlideRecord('sc_task');

    task.addQuery('request_item', current.request_item);

    task.addQuery('active', true);

    task.query();

 

    var hasActiveTasks = false;

 

    // Check if there are any active tasks

    while (task.next()) {

        hasActiveTasks = true;

        break;

    }

 

    if (!hasActiveTasks) {

        var ritm = new GlideRecord('sc_req_item');

        if (ritm.get(current.request_item)) {

            // Query to check if there are any active tasks remaining for the current ritm

            var remainingTasks = new GlideRecord('sc_task');

            remainingTasks.addQuery('request_item', current.request_item);

            remainingTasks.addQuery('active', true);

            remainingTasks.query();

 

            var activeTaskCount = 0;

            while (remainingTasks.next()) {

                activeTaskCount++;

            }

 

          // If no active tasks are remaining, close the ritm

             if (activeTaskCount === 0) {

             ritm.state = 3; // Closed state

         

}

}

}

})(current, previous);

2 ACCEPTED SOLUTIONS

Tony Chatfield1
Kilo Patron

Hi, in you 'wait for' condition you declare 'sctaskCount1' without a data type and do not assign a value to it
var sctaskCount,sctaskCount1;

then compare this 'undefined' variable to sctaskCount

if(sctaskCount == sctaskCount1 )

 

You could debug\test your script in a background window with a few minor adjustments (like declaring 'answer') which might make it easier to understand\identify your issue(s).

 

Declare a variable current and use gliderecord 'get' to set a record from your reference table (as the current object) via it's sys_id, then your current.dot.walking will function as it would in the real world.

 

//Do not declare a variable called current anywhere in your operational code, but fine for testing in background window.

 

var current = new GlideRecord('sc_req_item');

current.get('aeed229047801200e0ef563dbb9a71c2'); // eg RITM0000001 in my PDI

gs.info(current.number);

 

//add your code and test\debug\log as needed.

View solution in original post

Julie Catano
Kilo Guru

Used this in my wait condition and it worked.

 

Wait for ALL catalog tasks to be completed

 

 

// Set the variable 'answer' to true or false to indicate if the condition has been met or not.

var tskRec = new GlideRecord("sc_task");

tskRec.addQuery('request_item', current.sys_id);

tskRec.addQuery('active', "true");

tskRec.query();

if (tskRec.next()) {

    answer = false;

} else {

    answer = true;

}

View solution in original post

2 REPLIES 2

Tony Chatfield1
Kilo Patron

Hi, in you 'wait for' condition you declare 'sctaskCount1' without a data type and do not assign a value to it
var sctaskCount,sctaskCount1;

then compare this 'undefined' variable to sctaskCount

if(sctaskCount == sctaskCount1 )

 

You could debug\test your script in a background window with a few minor adjustments (like declaring 'answer') which might make it easier to understand\identify your issue(s).

 

Declare a variable current and use gliderecord 'get' to set a record from your reference table (as the current object) via it's sys_id, then your current.dot.walking will function as it would in the real world.

 

//Do not declare a variable called current anywhere in your operational code, but fine for testing in background window.

 

var current = new GlideRecord('sc_req_item');

current.get('aeed229047801200e0ef563dbb9a71c2'); // eg RITM0000001 in my PDI

gs.info(current.number);

 

//add your code and test\debug\log as needed.

Julie Catano
Kilo Guru

Used this in my wait condition and it worked.

 

Wait for ALL catalog tasks to be completed

 

 

// Set the variable 'answer' to true or false to indicate if the condition has been met or not.

var tskRec = new GlideRecord("sc_task");

tskRec.addQuery('request_item', current.sys_id);

tskRec.addQuery('active', "true");

tskRec.query();

if (tskRec.next()) {

    answer = false;

} else {

    answer = true;

}