Unable to get Sequential catalog task state in workflow

Anvikumari
Kilo Contributor

Hi,

Could you please help me to get state of the latest created sequential task  in workflow.

Requirement is when the first task get close incomplete then second task will get created and when I close complete 2nd then 3rd task will get created and if I close incomplete 2nd task then set RITM to Work in Progress.

To achieve this I have created if activity and used below code:

answer = check();

function check() {
var gr_tsk = new GlideRecord("sc_task");
gr_tsk.addQuery('request_item', current.sys_id);
gr_tsk.query();
gr_tsk.next();
if (gr_tsk.state == 4) //check for task status closed incomplete
{
return 'yes';
}
return 'no';
}

 

but it is taking state value of 1st created task.

 

Could you please help me to implement above scenario?

 

Thank you!

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

First, you should always put .next() in an if or while condition.  One way to do this is to simply order the results by Created descending, then use 'if', which will only return one record, so there's no need for a setLimit:

 

answer = check();
function check() {
    var gr_tsk = new GlideRecord("sc_task");
    gr_tsk.addQuery('request_item', current.sys_id);
    gr_tsk.orderByDesc('sys_created_on');
    gr_tsk.query();
    if (gr_tsk.next()) {
        if (gr_tsk.state == 4) { //check for task status closed incomplete
            return 'yes';
        }
    }
    return 'no';
}

 

 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

First, you should always put .next() in an if or while condition.  One way to do this is to simply order the results by Created descending, then use 'if', which will only return one record, so there's no need for a setLimit:

 

answer = check();
function check() {
    var gr_tsk = new GlideRecord("sc_task");
    gr_tsk.addQuery('request_item', current.sys_id);
    gr_tsk.orderByDesc('sys_created_on');
    gr_tsk.query();
    if (gr_tsk.next()) {
        if (gr_tsk.state == 4) { //check for task status closed incomplete
            return 'yes';
        }
    }
    return 'no';
}

 

 

Aniket Chavan
Tera Sage
Tera Sage

Hello @Anvikumari ,

 

Please give a try to the code below and let me know how it works for you.

answer = check();

function check() {
    var gr_tsk = new GlideRecord("sc_task");
    gr_tsk.addQuery('request_item', current.sys_id);
    gr_tsk.orderByDesc('sys_created_on'); // Order by created date in descending order
    gr_tsk.setLimit(1); // Limit the result to one record
    gr_tsk.query();

    if (gr_tsk.next()) {
        if (gr_tsk.state == 4) {
            return 'yes';
        }
    }

    return 'no';
}

Let me know your views on this and Mark āœ…Correct if this solves your query and also mark šŸ‘Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket