How to update an array field for many records at one time?

MG Casey
Mega Sage

I am needing to add a specific role to all of our 4,500 knowledge articles, however, I am prevented from editing the "Role" field when viewing these articles in List view.

That was the fastest way I could think to update them all - anyone else have some different ideas?

1 ACCEPTED SOLUTION

Dominik Simunek
Tera Guru

The roles field in Knowledge [kb_knowledge] table is glide_list type which is restricted for list editing OOTB (there is a property for it called "glide.ui.list_edit_ignore_types"). Definitely better option how to update all these records is to use the background script (module "Scripts - Background"). So I think you should run a script similar to this one:



var roleName = 'test'; // name of the role to be added


var knowledgeGR = new GlideRecord('kb_knowledge');


// add more specific condition if you want to update only selected articles


// without any condition it will update all articles


knowledgeGR.query();


while (knowledgeGR.next()) {


      var current = knowledgeGR.getValue('roles');


      if (gs.nil(current)) {


              knowledgeGR.roles = roleName;


      } else {


              // there should be check if the role is already there and if so then it should not be added


              knowledgeGR.roles = current + ',' + roleName;


      }


      knowledgeGR.setWorkflow(false); // not run business rules


      knowledgeGR.setUseEngines(false);


      knowledgeGR.autoSysFields(false); // not update Updated on and Updated by fields


      knowledgeGR.update();


}



This example script can help you create the script that you need in your case.


View solution in original post

3 REPLIES 3

rita_m
Tera Guru

I'd just do it in a background script.   Since it's a reference, you'll need to set the field to the sys_id of the role.   If it's a field that allows MULTIPLE roles and you want to keep the current values, then your script will have to be more complex.


Dominik Simunek
Tera Guru

The roles field in Knowledge [kb_knowledge] table is glide_list type which is restricted for list editing OOTB (there is a property for it called "glide.ui.list_edit_ignore_types"). Definitely better option how to update all these records is to use the background script (module "Scripts - Background"). So I think you should run a script similar to this one:



var roleName = 'test'; // name of the role to be added


var knowledgeGR = new GlideRecord('kb_knowledge');


// add more specific condition if you want to update only selected articles


// without any condition it will update all articles


knowledgeGR.query();


while (knowledgeGR.next()) {


      var current = knowledgeGR.getValue('roles');


      if (gs.nil(current)) {


              knowledgeGR.roles = roleName;


      } else {


              // there should be check if the role is already there and if so then it should not be added


              knowledgeGR.roles = current + ',' + roleName;


      }


      knowledgeGR.setWorkflow(false); // not run business rules


      knowledgeGR.setUseEngines(false);


      knowledgeGR.autoSysFields(false); // not update Updated on and Updated by fields


      knowledgeGR.update();


}



This example script can help you create the script that you need in your case.


This script worked great! Thank you so much.