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

Robbie
Kilo Patron
Kilo Patron

Hi @Nisha30,

 

Can you expand on where you are calling and running this script? Is it a one off Fix Script for example, or a scheduled job for example?

 

I've tested the syntax via my PDI and it works as expected. Thoughts that come to mind is that you may have some logic implemented on update of the name unique to your org or instance.

You are able to execute the script without invoking other scripts that maybe blocking the update by add a line before the sr.update();

Add the following above the update: sr.setWorkflow(false);

This essentially stops other rules and logic from being invoked - however - please consider this, there maybe a valid reason why this is being blocked. I would suggest you run this on NON prod first and confirm the results and behaviour before running on prod. I'd also specifically target one or two records first before running on all servers meeting the criteria.

 

Updated script:

 

var sr = new GlideRecord('cmdb_ci_server');
sr.addQuery('po_number', 'decom');
sr.addQuery('sys_class_name', 'cmdb_ci_server'); //only  cmdb_ci_server CI
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

tried both below for class cmdb_ci_server, FIX SCRIPT.but this is needed or else it will update the CI belonging to WIN or LIN server

some error

 

Invalid query detected, please check logs for details [Unknown field class in table cmdb_ci_server]

 Thanks

Hi @Nisha30,

 

Can you try the above script again. I've just spotted it should be

sr.addQuery('sys_class_name', 'cmdb_ci_server'); as opposed to your original call to 'class'

 

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

Its strange no error this time, but nothing happened no update as well.

 

Tried with IF condition also looks does not enter the IF loop 

if(sr.hasNext())

code looks below now

 

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.query();
while(sr.Next())
{
        // if(sr.hasNext())
        // {
        // gs.print('records found');
        var dName = 'zzz' + sr.name;
        sr.name = dName;
        sr.setWorkflow(false);
        sr.update();
        //}
}