background script update not updating record

Santhosh40
Giga Expert

Hi Guys,

As per my requirement, Need to update all the group names which are having "HDPro" to "THDPro".

Tried with the following code: in Background script and Fix script.

var a =[];

var gr = new GlideRecord("sys_user_group");
gr.addEncodedQuery("nameSTARTSWITHHDPro");
gr.query();
while (gr.next()) {
gs.print(gr.name);
var b = a.push(gr.group.name.toString());
var c = "T" + gr.name;
gs.print(c);
gr.update();
}

I am able to get all the records, But it is not updating the record(Group name)

Can you please suggest with your idea.

Leads are more appreciable.

Thanks in advance..

Santhosh.K

 

1 ACCEPTED SOLUTION

Santhosh40
Giga Expert

Thankyou all for valueble thoughts.

My Work is done with following code.

************************

var gr1 = new GlideRecord("sys_user_group");
gr1.addEncodedQuery("nameSTARTSWITHHDPro");
gr1.query();
while (gr1.next()) {
gs.print(gr1.name);
gr1.name = "T" + gr1.name;
gs.print(gr1.name);
gr1.update();
}

 

View solution in original post

4 REPLIES 4

Mark Skinner
Mega Guru

Hi Santhosh,

 

I think this is an easy fix. I would try this 

 

var a =[];

var gr = new GlideRecord("sys_user_group");
gr.addEncodedQuery("nameSTARTSWITHHDPro");
gr.query();
while(gr.next()) {
gs.print(gr.name);
var b = a.push(gr.group.name.toString());
var name = gr.name.toString();
var newName =  "T" + name;
gr.name = newName;
gs.print(newName);
gr.update();
}

 

if this was helpful please mark as so. If this solved your question please mark as correct

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Santosh,

you are not updating the name field anywhere so it is not updating that column; update script as below

var c = "T" + gr.name;
gs.print(c);

gr.name = c;
gr.update();

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Alikutty A
Tera Sage

I think you are using too many lines of code, You can simplify this to

var gr = new GlideRecord("sys_user_group");
gr.addEncodedQuery("nameSTARTSWITHHDPro");
gr.query();
while (gr.next()) {
gr.name = "T" + gr.name;
gr.update();
}

Santhosh40
Giga Expert

Thankyou all for valueble thoughts.

My Work is done with following code.

************************

var gr1 = new GlideRecord("sys_user_group");
gr1.addEncodedQuery("nameSTARTSWITHHDPro");
gr1.query();
while (gr1.next()) {
gs.print(gr1.name);
gr1.name = "T" + gr1.name;
gs.print(gr1.name);
gr1.update();
}