- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 02:29 AM
Hi,
Basically, my requirement is to create a new Catalog Task if user commented on the RITM and no Open Catalog Tasks available. Here is my code:
function onBefore(current, previous) {
var cmt = current.comments;
var verifyTask = 0;
var task = new GlideRecord("sc_task");
task.addQuery("parent", "=", current.sys_id);
task.addQuery("state", "<", 3);
task.query();
while (task.next()) {
task.comments = cmt;
verifyTask = 1;
task.update();
}
if (verifyTask == 0){
task.initialize();
task.insert();
task.request_item = current.sys_id;
task.assignment_group.setDisplayValue('Service Desk');
task.short_description = 'Verify approval';
task.description = 'Check if Approver replied to RITM';
task.comments = cmt;
task.state = 1;
verifyTask = 1;
task.update();
}
}
This code creates a new Catalog Task with State = Open. So this should not create another new Catalog Task anymore but it's creating one. What's wrong with my code?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 03:23 AM
Ok.. You can try below should work...Let me know in case of any issues
function onBefore(current, previous) {
var cmt = current.comments;
var task = new GlideRecord('sc_task');
task.addQuery('request_item', "=", current.sys_id);
task.addQuery('state', "<", 3);
task.query();
if(task.next())
{
while (task.next()) {
task.comments = cmt;
// verifyTask = 1;
task.update();
}
}
else
{
var task1 = new GlideRecord("sc_task");
task1.initialize();
task1.request_item = current.sys_id;
task1.assignment_group.setDisplayValue('Service Desk');
task1.short_description = 'Verify approval';
task1.description = 'Check if Approver replied to RITM';
task1.comments = cmt;
task1.state = 1;
task1.insert();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 02:47 AM
function onBefore(current, previous) {
var cmt = current.comments;
var verifyTask = 0;
var task = new GlideRecord("sc_task");
task.addQuery("parent", "=", current.sys_id);
task.addQuery("state", "<", 3);
task.query();
while (task.next()) {
task.comments = cmt;
verifyTask = 1;
task.update();
}
if (verifyTask == 0){
task.initialize();
task.request_item = current.sys_id;
task.assignment_group.setDisplayValue('Service Desk');
task.short_description = 'Verify approval';
task.description = 'Check if Approver replied to RITM';
task.comments = cmt;
task.state = 1;
verifyTask = 1;
task.insert();
}
}
Please use the above code and try
Please like or mark correct based on the impact of response.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 03:02 AM
Hi Sid,
Not working Many thanks for your help. But it still creates a second task.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 02:48 AM
Hi Diane,
Can you try the below code and let me know is that helpful.
function onBefore(current, previous) {
var cmt = current.comments;
var task = new GlideRecord("sc_task");
task.addQuery("parent", "=", current.sys_id);
task.addQuery("state", "<", 3);
task.query();
if(task.next())
{
while (task.next()) {
task.comments = cmt;
verifyTask = 1;
task.update();
}
}
else
{
var task1 = new GlideRecord("sc_task");
task1.initialize();
task1.request_item = current.sys_id;
task1.assignment_group.setDisplayValue('Service Desk');
task1.short_description = 'Verify approval';
task1.description = 'Check if Approver replied to RITM';
task1.comments = cmt;
task1.state = 1;
task1.insert();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 03:06 AM
Hi Vinoth,
Not working too Still creates another task.