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

@Nisha30 

you said you are trying to use contains then this line should be updated as this

sr.addQuery('po_number', 'LIKE', 'decom');

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

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

The string exactly contains value = decom.

Does not do anything

 

Nisha30_0-1739364761097.png

var sr = new GlideRecord('cmdb_ci_server');
sr.addQuery('po_number','decom');
sr.addQuery('sys_class_name','cmdb_ci_server');//only for cmdb_ci_server
sr.addQuery('sys_id', '00b2d5e9db89e300668ce1aa4b961940');
sr.query();
while(sr.Next())
{
        // if(sr.hasNext())
        // {
        // gs.print('records found');
        var dName = 'zzz' + sr.name;
        sr.name = dName;
        sr.setWorkflow(false);
        sr.update();
        //}
}

 

okay the syntax is small q, i corrected that and it goes inside loop now but update of name is not working 😞

@Nisha30 

try this

var dName = 'zzz' + sr.name.toString();
sr.name = dName.toString();

OR

sr.setValue('name', dName.toString());

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