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

@Siddharth Parna you just need to make sure you:

the correct sys_id of the group (not its name) used, Reference the field properly in your addQuery() condition. and Place the filter before the query() call.

mohdarbaz
Kilo Guru

@Siddharth Parna 

  • The query method should be called with parentheses to execute the query.
  • Ensure that the request_item field is correctly referenced and exists in the sc_task table.
  • Ensure that the short_description variable exists in the sc_req_item table.

try this corrected script:

var taskgr = new GlideRecord('sc_task');

// Uncomment and set the appropriate group ID if needed
// 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);
}
}
}
}

 

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,

Mohd Arbaz.

@Siddharth Parna If my response helped, please mark it as helpful and accept it as one more solution.

Viraj Hudlikar
Tera Sage

@Siddharth Parna 

 
  1. taskgr.query(); - You missed the parentheses, so the query was never executed.
  2. reqItem.get(taskgr.request_item.toString()) - Ensures the correct sys_id is passed

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.