How can I set Group Type field on Group table using script?

Sontel
Tera Contributor

I have few user groups . I have the encoded query with their sys ID after filtering the required records. I would like to know how can I insert multiple group types i.e (ITIL, Demand,Agile) to these groups considering few groups already have some values which should be kept. Type is OOB List type field and I will be using sys Ids for these group types. I know I have to use array. Can someone help me with the background script which should be used in this case. I managed to write below code which isn't working, moreover this doesn't take care of keeping the existing values so can someone please review this code

var gr = new GlideRecord('sys_user_group');

var Q = "sys_idIN8a4dde73c6112278017a6a4baf547aa7,d625dccec0a8016700a222a0f7900d06";


gr.addEncodedQuery('sys_id',Q);
gr.query();

if(gr.next())

{

gr.type = 'cd94c5951b40101019aa4375cc4bcb7a, 1cb8ab9bff500200158bffffffffff62'; //sys IDs of ITIL and agile group type

gr.update();

}

 

9 REPLIES 9

Jaspal Singh
Mega Patron
Mega Patron

Hi Sonesh,

 

Can you try something as below in background script.

var typeis=['cd94c5951b40101019aa4375cc4bcb7a', '1cb8ab9bff500200158bffffffffff62'];

for(var i=0; i<=typeis.length;i++)
{
gs.print(typeis.length);
var gr = new GlideRecord('sys_user_group');
var Q = "sys_idIN8a4dde73c6112278017a6a4baf547aa7,d625dccec0a8016700a222a0f7900d06";
gr.addEncodedQuery('sys_id',Q);
gr.query();
while(gr.next())
{
gr.type = gr[i]+','+gr.type; //sys IDs of ITIL and agile group type
gr.update();
}
}

 

Thanks,

Jaspal Singh

Looks like this is getting stuck in an infinite loop, as the execution kept on continuing till page times out and instance freezes for couple of mins.

Hello Sonesh,

 

Please try below script.

var typeis=['cd94c5951b40101019aa4375cc4bcb7a','1cb8ab9bff500200158bffffffffff62'];
var gr = new GlideRecord('sys_user_group');
gr.addQuery('sys_id','IN,'8a4dde73c6112278017a6a4baf547aa7,d625dccec0a8016700a222a0f7900d06');
gr.setValue('type',typeis); //sys IDs of ITIL and agile group type
gr.updateMultiple();

 

Thanks

Sontel
Tera Contributor

Hi @kalyan this script worked but updated all the records in group table. It failed to query only those 3 sys ID records.

ps- small tweak I did putting IN in quote.