- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2024 05:02 AM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2024 11:28 AM - edited 01-01-2024 12:38 PM
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';
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2024 11:28 AM - edited 01-01-2024 12:38 PM
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';
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2024 11:45 AM
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