The CreatorCon Call for Content is officially open! Get started here.

Prepare and execute one time Fix Script for the change task to migrate the group

Kaushik Ghosh
Tera Contributor

How to prepare a one-time fix script to migrate any change tasks for tickets in Draft, Assess from "SC-ETS-ABCD" to Change Owner group?

 

Note:
A change owner group is a dynamic group.

But the SC-ETS-ABCD is fixed and it will come to the task implementer group.

I have attempted to write the code, it is functioning, but the issue is, it is migrating the Task Implementer Group one at a time it is not processing in a batch. For multiple change tasks, the script needs to be executed multiple times.

 

Here is the code,

 

var chgtsk = new GlideRecord('change_task');
chgtsk.addEncodedQuery('active=true^parent.state=301^ORparent.state=302^u_task_implementer_group=07d39f9c1b556c5080452136ec4bcbc1');
chgtsk.query();
if(chgtsk.next()){
chgtsk.u_task_implementer_group = chgtsk.parent.assignment_group;
chgtsk.update();
}

Could you please help me to fix the issue?

1 ACCEPTED SOLUTION

-Andrew-
Kilo Sage

Hi!

 

To migrate multiple change tasks for tickets in Draft, Assess from "SC-ETS-ABCD" to Change Owner group using a script in ServiceNow, you can modify the code to use a batch update. Here is an example script:

// Set the batch size
var batchSize = 100;

// Query for change tasks that need to be updated
var chgtsk = new GlideRecord('change_task');
chgtsk.addEncodedQuery('active=true^parent.state=301^ORparent.state=302^u_task_implementer_group=07d39f9c1b556c5080452136ec4bcbc1');
chgtsk.query();
chgtsk.setLimit(batchSize);

// Update each change task
while (chgtsk.next()) {
  chgtsk.u_task_implementer_group = chgtsk.parent.assignment_group;
  chgtsk.update();
}

// Continue querying and updating in batches until no more records are found
while (chgtsk.hasNext()) {
  // Query for the next batch of change tasks
  chgtsk.next();
  chgtsk.setLimit(batchSize);

  // Update each change task in the batch
  while (chgtsk.next()) {
    chgtsk.u_task_implementer_group = chgtsk.parent.assignment_group;
    chgtsk.update();
  }
}

This script uses a batch size of 100, which can be adjusted based on your environment and performance requirements.

Note: Please test this script in a non-production environment before executing it in a production environment

View solution in original post

4 REPLIES 4

-Andrew-
Kilo Sage

Hi!

 

To migrate multiple change tasks for tickets in Draft, Assess from "SC-ETS-ABCD" to Change Owner group using a script in ServiceNow, you can modify the code to use a batch update. Here is an example script:

// Set the batch size
var batchSize = 100;

// Query for change tasks that need to be updated
var chgtsk = new GlideRecord('change_task');
chgtsk.addEncodedQuery('active=true^parent.state=301^ORparent.state=302^u_task_implementer_group=07d39f9c1b556c5080452136ec4bcbc1');
chgtsk.query();
chgtsk.setLimit(batchSize);

// Update each change task
while (chgtsk.next()) {
  chgtsk.u_task_implementer_group = chgtsk.parent.assignment_group;
  chgtsk.update();
}

// Continue querying and updating in batches until no more records are found
while (chgtsk.hasNext()) {
  // Query for the next batch of change tasks
  chgtsk.next();
  chgtsk.setLimit(batchSize);

  // Update each change task in the batch
  while (chgtsk.next()) {
    chgtsk.u_task_implementer_group = chgtsk.parent.assignment_group;
    chgtsk.update();
  }
}

This script uses a batch size of 100, which can be adjusted based on your environment and performance requirements.

Note: Please test this script in a non-production environment before executing it in a production environment

Thanks a lot, Andrew, it worked.

Glad i could help!!

Harshal Aditya
Mega Sage

Hi @Kaushik Ghosh ,

 

Replace if with while it should work for multiple records.

Please run your code in lower environment first.

 

Please Mark My Response as Correct/Helpful based on Impact

Regards,
Harshal