Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to close complete bulk list of demands and demand tasks.

thaduri sai
Tera Contributor

Hi Team,

 

We have a bulk list of demand numbers to be close complete through background script.

 

And also,

 

We have a bulk list of demand task to be close Skipped through background script.

 

How to achieve it - Thanks for the advance.

 

Thanks,

Sai

3 ACCEPTED SOLUTIONS

AnveshKumar M
Tera Sage
Tera Sage

Hi @thaduri sai ,

 

You can try the following script to close demands and their corresponding demand tasks from a fix script or background script.

 

This will not trigger any notifications too as I have used setWorkflow(false).

 

var dmnGr = new GlideRecord("dmn_demand");

dmnGr.addEncodedQuery("YOUR ENCODED QUERY); //Paste your encoded here by copying it from the list view filter bread crumbs

dmnGr.query();

//close corresponding demand tasks

while(dmnGr._next()){

   var dtskGr = new GlideRecord("dmn_demand_task");

   dtskGr.addQuery("parent", dmnGr.getUniqueValue());

   dtskGr.addQuery("state", "!=", "3");

   dtskGr.query();

   while(dtskGr._next()){

      dtskGr.setWorkflow(false);

      dtskGr.autoSysFields(false);

      dtskGr.state = "3";

      dtskGr.update();

   }

   //Close the demand

   dmnGr.setWorkflow(false);

   dmnGr.autoSysFields(false);

   dmnGr.state = "9";

   dmnGr.update();

}

 

Please mark my answer helpful and accept as solution if it helped you 👍

Thanks,
Anvesh

View solution in original post

@thaduri sai Try this, I did a mistake in function name.

 

var gr = new GlideRecord("dmn_demand");

gr.addEncodedQuery("sys_class_name=dmn_demand^number=DEMAND NUMBER");

gr.query();

 

while(gr._next()){

var dmntask = new GlideRecord("dmn_demand_task");

dmntask.addQuery("parent", gr.getUniqueValue());

dmntask.addQuery("state", "!=", "3");

dmntask.addQuery("state", "!=", "7");

dmntask.query();

while(dmntask._next()){

dmntask.setWorkflow(false);

dmntask.state = "3";

dmntask.update();

}

gr.setWorkflow(false);

gr.state = "9";

gr.update();

}

Thanks,
Anvesh

View solution in original post

@thaduri sai You can try using this line of code. But as we are using setWorkflow(false); the comment might not appear in proper order. You can try and check.

 

var gr = new GlideRecord("dmn_demand");

gr.addEncodedQuery("sys_class_name=dmn_demand^number=DEMAND NUMBER");

gr.query();

while(gr._next()){

var dmntask = new GlideRecord("dmn_demand_task");

dmntask.addQuery("parent", gr.getUniqueValue());

dmntask.addQuery("state", "!=", "3");

dmntask.addQuery("state", "!=", "7");

dmntask.query();

while(dmntask._next()){

dmntask.setWorkflow(false);

dmntask.state = "3";

dmntask.work_notes = "YOUR STANDARD COMMENT";

dmntask.update();

 

}

gr.setWorkflow(false);

gr.state = "9";

gr.work_notes = "YOUR STANDARD COMMENT";

gr.update();

}

 

If the above code is not working for you, what you can do is, update work notes for all the demands and it's tasks first then close them like this.

 

//Update work notes

var gr = new GlideRecord("dmn_demand");

gr.addEncodedQuery("sys_class_name=dmn_demand^number=DEMAND NUMBER");

gr.query();

while(gr._next()){

var dmntask = new GlideRecord("dmn_demand_task");

dmntask.addQuery("parent", gr.getUniqueValue());

dmntask.addQuery("state", "!=", "3");

dmntask.addQuery("state", "!=", "7");

dmntask.query();

while(dmntask._next()){

dmntask.work_notes = "YOUR STANDARD COMMENT";

dmntask.autoSysFields(false);

dmntask.update();

}

gr.work_notes = "YOUR STANDARD COMMENT";

gr.autoSysFields(false);

gr.update();

}

//Close Tasks and Demand

var gr = new GlideRecord("dmn_demand");

gr.addEncodedQuery("sys_class_name=dmn_demand^number=DEMAND NUMBER");

gr.query();

while(gr._next()){

var dmntask = new GlideRecord("dmn_demand_task");

dmntask.addQuery("parent", gr.getUniqueValue());

dmntask.addQuery("state", "!=", "3");

dmntask.addQuery("state", "!=", "7");

dmntask.query();

while(dmntask._next()){

dmntask.setWorkflow(false);

dmntask.state = "3";

dmntask.update();

}

gr.setWorkflow(false);

gr.state = "9";

gr.update();

}

 

Please mark my answer helpful and accept as a solution if it helped 👍

Thanks,
Anvesh

View solution in original post

9 REPLIES 9

Danish Bhairag2
Tera Sage

Hi @thaduri sai ,

 

To close multiple demand records and their associated tasks through a Background Script in ServiceNow, you can follow these steps:

 

### Closing Demand Records:

1. **Create a Background Script:**

   - Navigate to **System Definition > Background Scripts**.

   - Click on **New** to create a new Background Script.

 

2. **Write the Script for Closing Demand Records:**

   - Write a script to query demand records based on certain conditions and close them.

   - Example script to close demand records:

     

     (function() {

         var demandGr = new GlideRecord('u_demand_table'); // Replace 'u_demand_table' with your demand table name

         demandGr.addQuery('YOUR_CONDITION_FIELD', 'YOUR_CONDITION_VALUE'); // Add your conditions here

         demandGr.query();

 

         while (demandGr.next()) {

             demandGr.setValue('state', 'Closed Complete'); // Set the state to Closed Complete or your desired state

             demandGr.update();

         }

 

         gs.info('Closed ' + demandGr.getRowCount() + ' demand records.');

     })();

     

 

### Closing Associated Demand Tasks:

1. **Write the Script for Closing Demand Tasks:**

   - Assuming your demand tasks are stored in a table (e.g., `u_demand_task_table`), write a script to query demand task records based on certain conditions and close them.

   - Example script to close demand tasks:

     

     (function() {

         var taskGr = new GlideRecord('u_demand_task_table'); // Replace 'u_demand_task_table' with your demand task table name

         taskGr.addQuery('YOUR_CONDITION_FIELD', 'YOUR_CONDITION_VALUE'); // Add your conditions here

         taskGr.query();

 

         while (taskGr.next()) {

             taskGr.setValue('state', 'Closed Skipped'); // Set the state to Closed Skipped or your desired state

             taskGr.update();

         }

 

         gs.info('Closed ' + taskGr.getRowCount() + ' demand tasks.');

     })();

     

 

### Note:

- Replace `'YOUR_CONDITION_FIELD'` and `'YOUR_CONDITION_VALUE'` with the appropriate field names and values for your conditions.

- Make sure to replace `'u_demand_table'` and `'u_demand_task_table'` with the actual table names where your demand records and tasks are stored.

- Test the scripts in a non-production environment first to ensure they work as expected before running them in your live instance.

- Consider implementing error handling and logging to track the progress and potential issues during the update process.

 

Thanks,

Danish

 

AnveshKumar M
Tera Sage
Tera Sage

Hi @thaduri sai ,

 

You can try the following script to close demands and their corresponding demand tasks from a fix script or background script.

 

This will not trigger any notifications too as I have used setWorkflow(false).

 

var dmnGr = new GlideRecord("dmn_demand");

dmnGr.addEncodedQuery("YOUR ENCODED QUERY); //Paste your encoded here by copying it from the list view filter bread crumbs

dmnGr.query();

//close corresponding demand tasks

while(dmnGr._next()){

   var dtskGr = new GlideRecord("dmn_demand_task");

   dtskGr.addQuery("parent", dmnGr.getUniqueValue());

   dtskGr.addQuery("state", "!=", "3");

   dtskGr.query();

   while(dtskGr._next()){

      dtskGr.setWorkflow(false);

      dtskGr.autoSysFields(false);

      dtskGr.state = "3";

      dtskGr.update();

   }

   //Close the demand

   dmnGr.setWorkflow(false);

   dmnGr.autoSysFields(false);

   dmnGr.state = "9";

   dmnGr.update();

}

 

Please mark my answer helpful and accept as solution if it helped you 👍

Thanks,
Anvesh

Hi @AnveshKumar M ,

 

Thanks for your help.

 

Below script we have to add parent demand also close as well ?

 

var gr = new GlideRecord("dmn_demand");
gr.addEncodedQuery("sys_class_name=dmn_demand^number=DEMAND NUMBER");
gr.query();

while(gr._next()){
var dmntask = new GlideRecord("dmn_demand_task");
dmntask.addQuery("parent", gr.getUniqueValue());
dmntask.addQuery("state", "!=", "3");
dmntask.addQuery("state", "!=", "7");
dmntask.query();
}
while(dmntask._next()){
dmntask.setWorkflow(false);
dmntask.state = "3";
dmntask.update();
}

@thaduri sai 

Try the below script.

 

var gr = new GlideRecord("dmn_demand");

gr.addEncodedQuery("sys_class_name=dmn_demand^number=DEMAND NUMBER");

gr.query();

 

while(gr._next()){

var dmntask = new GlideRecord("dmn_demand_task");

dmntask.addQuery("parent", gr.getUniqueValue());

dmntask.addQuery("state", "!=", "3");

dmntask.addQuery("state", "!=", "7");

dmntask.query();

while(dmntask._next()){

dmntask.setWorkflow(false);

dmntask.state = "3";

dmntask.update();

}

gr.setWorkFlow(false);

gr.state = "9";

gr.update();

}

 

Please mark my answer helpful and accept as solution if it helped 👍

Thanks,
Anvesh