Glide list type field need to remove one value in bulk.

servicenow_devo
Tera Expert

I have a glide list type field in one form need to remove on value from it without doing manually. 

Can any one suggest background script to update same.

Thankyou
2 ACCEPTED SOLUTIONS

OK, you just need to set the top 3 variables then:

  1. table: you want to set this to 'kb_knowledge_block'
  2. listField: you want to set this to 'can_read_user_criteria'
  3. whatToRemove: this is something I cannot tell you. Open the user criteria record that you want removed and then right-click, Copy sys_id, then insert into this variable. Make sure the quotes are maintained.

Script updated for your requirements (with additional explanation):

 

 

var table = ''; // Name of the table, e.g. kb_knowledge_block
var listField = ''; // Name of the list field to remove an element from, e.g. can_read_user_criteria
var whatToRemove = ''; // Sys_id of the element to remove from the list field, e.g. sys_id of user criteria
var encQ = listField + 'LIKE' + whatToRemove; // encoded query to limit queried record to those that contain the element we want to remove
var listArray = []; // initial array variable declaration
var elementIndex = -1; // initial index variable declaration


/* Run a GlideRecord query to find all records that contain the element to be deleted from the specifid list field */
var listGr = new GlideRecord(table);
listGr.addEncodedQuery(encQ);
listGr.query();
while(listGr.next()) {
  listArray = listGr[listField].toString().split(','); // set the array variable based on the List field of the found record
  elementIndex = listArray.indexOf(whatToRemove); // search for the element to remove from this particular record
  /* Only try to remove the element and update the record if it was found, i.e. not -1 */
  if(elementIndex > -1) {
    listArray.splice(elementIndex,1);
    listGr.setValue(listField,listArray);
    listGr.update();
  }
  /* Reset the initial array related variables */
  listArray = [];
  elementIndex = -1;
}

 

View solution in original post

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,
Refer below script

var valueToBeRemoved='anyxyzvalue';//SysID of one item from list that needs to be removed

var t=new GlideRecord('your_table_name_on_which_list_is_present');
t.query();
while(t.next())
{
var listTobeUpdate=t.getValue('backend_name_of_list_type_field');
var arr=listTobeUpdate.split(",");
var index = arr.indexOf(valueToBeRemoved);
if (index > -1) { // only splice array when item is found
  arr.splice(index, 1); // 2nd parameter means remove one item only
}
    var updatedList=arr.join(",");
    t.setValue('backend_name_of_list_type_field',updatedList);
    t.update();
}

Thanks and Regards,

Saurabh Gupta

View solution in original post

8 REPLIES 8

Taha Habib
Tera Guru

Hello re_sa, I hope you are doing well.

 

You can use this below mentioned background script to remove values from a glideList type field in a particular record.

Here, in this example, 

u_temporarytest is the name of the table.

u_glidelist is the name of a particular glidelist field.

 

var test= new GlideRecord('u_temporarytest');
test.addQuery('sys_id', '17d0a6bc2fc025102482802df699b680');
test.query();
if(test.next())
{

     gs.info(test.u_glidelist);
     test.u_glidelist="";
     test.update();
}

 

I hope this information helps you, If you find this helpful, please mark this answer as helpful and correct. Thank you.

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

I understood you would like to remove the same value from a particular list field on multiple record at once.

Here is an example script to illustrate one way to do this. In this example:

  • I have already filtered for the records in scope of this update in a list view, and used the encoded query from the list filter in my script
  • I wanted to remove a particular user from the Work Nots List field (which is a list type field)
/* Delete Adam Ringle from Work Notes List of incidents */

var encQ = 'work_notes_listLIKEf19943d813a81300f2277f176144b031'; // encoded query on list field
var whatToRemove = 'f19943d813a81300f2277f176144b031'; // Adam Ringle
var listArray = [];
var elementIndex = -1;

var incGr = new GlideRecord('incident');
incGr.addEncodedQuery(encQ);
incGr.query();
while(incGr.next()) {
  listArray = incGr.work_notes_list.toString().split(','); // set the array variable from the List field
  elementIndex = listArray.indexOf(whatToRemove); // search for the element to remove
  if(elementIndex > -1) {
    listArray.splice(elementIndex,1);
    incGr.setValue('work_notes_list',listArray);
    incGr.update();
  }
  listArray = [];
  elementIndex = -1;
}

 

Let me know if anything is unclear or you need help polishing your version of the script.

Hi could you please help me with understanding the script 

As I'm now to scripting. My requirment is to remove one specific user criteria from can read of knowldge block 

Thankyou

OK, you just need to set the top 3 variables then:

  1. table: you want to set this to 'kb_knowledge_block'
  2. listField: you want to set this to 'can_read_user_criteria'
  3. whatToRemove: this is something I cannot tell you. Open the user criteria record that you want removed and then right-click, Copy sys_id, then insert into this variable. Make sure the quotes are maintained.

Script updated for your requirements (with additional explanation):

 

 

var table = ''; // Name of the table, e.g. kb_knowledge_block
var listField = ''; // Name of the list field to remove an element from, e.g. can_read_user_criteria
var whatToRemove = ''; // Sys_id of the element to remove from the list field, e.g. sys_id of user criteria
var encQ = listField + 'LIKE' + whatToRemove; // encoded query to limit queried record to those that contain the element we want to remove
var listArray = []; // initial array variable declaration
var elementIndex = -1; // initial index variable declaration


/* Run a GlideRecord query to find all records that contain the element to be deleted from the specifid list field */
var listGr = new GlideRecord(table);
listGr.addEncodedQuery(encQ);
listGr.query();
while(listGr.next()) {
  listArray = listGr[listField].toString().split(','); // set the array variable based on the List field of the found record
  elementIndex = listArray.indexOf(whatToRemove); // search for the element to remove from this particular record
  /* Only try to remove the element and update the record if it was found, i.e. not -1 */
  if(elementIndex > -1) {
    listArray.splice(elementIndex,1);
    listGr.setValue(listField,listArray);
    listGr.update();
  }
  /* Reset the initial array related variables */
  listArray = [];
  elementIndex = -1;
}