Background script to update new field on Task with task related requested item field dont work

Siddharth Parna
Tera Contributor

var taskgr = new GlideRecord('sc_task');

//taskgr.addQuery('assignment_group', 'Group id ');

taskgr.setLimit(5);

taskgr.query;

 

while (taskgr.next()) {

    if(taskgr.request_item){

    gs.info('Task Number:' + taskgr.number);

    var reqItem = new GlideRecord('sc_req_item');

    if (reqItem.get(taskgr.request_item)) {

        var shortDesc = reqItem.variables.short_description;

        if (shortDesc) {

            taskgr.u_short_description = shortDesc.toString();

            taskgr.update();

            gs.info('Updated Task' + taskgr.number);

        } else {

            gs.info('No Short Description:' + taskgr.number);

           

        }

    }

    }

}

 

1 ACCEPTED SOLUTION

Nilesh Pol
Tera Guru

@Siddharth Parna 

verify the updated script:

var taskgr = new GlideRecord('sc_task');
taskgr.setLimit(5);
taskgr.query(); // <-- Fix here

while (taskgr.next()) {
if (taskgr.request_item) {
gs.info('Task Number: ' + taskgr.number);

var reqItem = new GlideRecord('sc_req_item');
if (reqItem.get(taskgr.request_item.toString())) { // <-- Add .toString()

var shortDesc = reqItem.variables.short_description;
if (shortDesc) {
taskgr.u_short_description = shortDesc.toString();
taskgr.update();
gs.info('Updated Task: ' + taskgr.number);
} else {
gs.info('No Short Description for Task: ' + taskgr.number);
}

} else {
gs.info('Requested item not found for Task: ' + taskgr.number);
}
}
}

View solution in original post

8 REPLIES 8

Nilesh Pol
Tera Guru

@Siddharth Parna 

verify the updated script:

var taskgr = new GlideRecord('sc_task');
taskgr.setLimit(5);
taskgr.query(); // <-- Fix here

while (taskgr.next()) {
if (taskgr.request_item) {
gs.info('Task Number: ' + taskgr.number);

var reqItem = new GlideRecord('sc_req_item');
if (reqItem.get(taskgr.request_item.toString())) { // <-- Add .toString()

var shortDesc = reqItem.variables.short_description;
if (shortDesc) {
taskgr.u_short_description = shortDesc.toString();
taskgr.update();
gs.info('Updated Task: ' + taskgr.number);
} else {
gs.info('No Short Description for Task: ' + taskgr.number);
}

} else {
gs.info('Requested item not found for Task: ' + taskgr.number);
}
}
}

This works.Can you also let me know how to filter based on assignment group as mentioned in the comments.I tried giving the id ,but I see when added filter records dont get updated

 

@Siddharth Parna 

try this for your earlier question and subsequent question as well and give correct group sysId

var taskgr = new GlideRecord('sc_task');

// Replace 'GROUP_SYSID' with the actual sys_id of the assignment group you want to filter by
taskgr.addQuery('assignment_group', 'GROUP_SYSID');

taskgr.setLimit(5);
taskgr.query();

while (taskgr.next()) {
    if (taskgr.request_item) {
        gs.info('Task Number: ' + taskgr.number);

        var reqItem = new GlideRecord('sc_req_item');
        if (reqItem.get(taskgr.request_item)) {
            var shortDesc = reqItem.variables.short_description;
            if (shortDesc) {
                taskgr.u_short_description = shortDesc.toString();
                taskgr.update();
                gs.info('Updated Task: ' + taskgr.number);
            } else {
                gs.info('No Short Description: ' + taskgr.number);
            }
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Siddharth Parna 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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