Script not working

Nisha30
Kilo Sage

Hello Community,

 

I want to add prefix zzz  infront of retired CI for specific cmdb_ci_server class , as there are same names in cmdb_ci_win_server and linuxserver.

i am getting print of these names which are to be added prefix whcih have specific string in one field po_number.

However the update is not happening.

 

Saw another thread but no luck

 

Can you please say what is wrong here i am doing please.

 

var sr = new GlideRecord('cmdb_ci_server');
sr.addQuery('po_number','decom');
sr.addQuery('class','cmdb_ci_server'); //only  cmdb_ci_server CI
sr.query();
while(sr.next())
{
// gs.print(sr.name);
var dName = 'zzz' + sr.name;
sr.name = dName;
sr.update();
}
1 ACCEPTED SOLUTION

Hi @Nisha30,

 

Scripting is case sensitive, so within the while loop, the .next() method needs to be all lower case.

 

Please use the below script. However, can you clarity one point, before even updating, have you followed my initial advise by specifically targeting one record at first to verify results etc.

 

As @Ankur Bawiskar has also aked, does the PO Number 'contain' decom or does it only have the string 'decom'? - Please confirm. I've provided some lines that can be commented or un-commented.

 

var sr = new GlideRecord('cmdb_ci_server');
//sr.addQuery('po_number', 'decom'); // Use this if the PO number has the string value of 'decom'
sr.addQuery('po_number', 'CONTAINS', 'decom'); // USe this if the PO number contains the string
sr.addQuery('sys_class_name', 'cmdb_ci_server'); //only  cmdb_ci_server CI
sr.addQuery('sys_id', '1072335fc611227500c0267a21be5dc5'); // temporary line to target one specifc record. This can be recomed once verified the script runs as required.
sr.query();
while (sr.next()) {
    gs.print('Server name before update: ' + sr.name);
    var dName = 'zzz' + sr.name;
    sr.name = dName;
    gs.print('Server name after update: ' + sr.name);
    sr.setWorkflow(false);
    sr.update();
}

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.




Thanks, Robbie

View solution in original post

13 REPLIES 13

Hi @Nisha30,

 

I appreciate you may not be too familiar with scripting. We're so close. I have this working no problem my end. We'll get you there ; )

Based on your screen shot, the syntax within the while loop is incorrect. As stated, scripting is case sensitive.

 

Correct what you have on line 6: while(sr.Next())

To this: while(sr.next())

 

Please note the small 'n' in the 'next' method.

A capital N will not work.

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.




Thanks, Robbie

Community Alums
Not applicable

Hi @Nisha30 

Can you verify the query is working correctly or not, please add following code before while loop;

if (!sr.hasNext()) {

gs.print('No records found');

}

and gs.print('Updating record: ' + sr.name); in 1st while loop.

this will ensure your query is correctly returning records.

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Nisha30 

Not a good practice to simply add the prefix

your query is wrong, use this

var gr = new GlideRecord("cmdb_ci_server");
gr.addEncodedQuery("po_numberLIKEdecom^sys_class_name=cmdb_ci_server");
gr.query();
while (gr.next()) {
    var dName = 'zzz' + gr.getValue('name');
    gr.name = dName;
    gr.setWorkflow(false); // use this to avoid triggering business rule, notifications etc
    gr.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Nisha30
Kilo Sage

Thankyou everyone for instant help. It worked now.

I saw carefully i did some silly syntax error for gliderecord variable due to which it was not working.