Insert inside gliderecord next loop on same table

Arnoud Kooi
ServiceNow Employee
ServiceNow Employee

As an answer to another question I have created a script.

The idea is to create placeholder records for the sys_choice table in other languages.

But it seems i can not do inserts on the same table i'm looping via a GlideRecord.

The base gr GlideRecord has a rowcount of about 4000, but after a first insert using the ins variable, it drops to around 30.

Is this a known limitation of the GlideRecord API, or am I doing something wrong?

var langs = ['es','ja','zh']; //define languages in array  

 

//query all english records  

var gr = new GlideRecord('sys_choice');  

gr.addQuery("language","en");  

gr.orderBy('name');  

gr.setLimit(10000);

gr.query();  

var cnt = 0;

var cntb = 0;

var cntc = 0;

while (gr.next()) {  

cnt = cnt+1

 

      var myLangs = langs.slice(); //clone array  

      var label = "#"+gr.getValue("label");  

      var sys_id = gr.getValue("sys_id")  

 

      //query for existing translations  

      var q = new GlideRecord('sys_choice');  

      q.addEncodedQuery("language!=en");  

      q.addQuery("table",gr.getValue("table"));  

      q.addQuery("element",gr.getValue("element"));  

      q.addQuery("value",gr.getValue("value"));  

      q.orderBy('name');  

      q.query();  

      while (q.next()) { //remove existing languages from array  

            cntb = cntb+1

          var idx = myLangs.indexOf(q.getValue('language'));  

          if (idx >= 0)  

              myLangs.splice(idx, 1);        

      }  

       

      //insert new records for remaining tables  

  for (var i = 0; i< myLangs.length;i++ ){  

      cntc = cntc+1

              var ins = new GlideRecord('sys_choice');      

          ins.initialize();

          ins.name = gr.getValue("name");

          ins.element = gr.getValue("element");

          ins.value = gr.getValue("value");

          ins.inactive = gr.getValue("inactive");

          ins.sequence = gr.getValue("sequence");

          ins.dependent_value = gr.getValue("dependent_value");

          ins.hint = gr.getValue("hint");

          ins.language = myLangs[i];  

          ins.label = label;

          ins.insert();  

      }

    //gs.print("a" + cnt);

  //gs.print("br" + gr.getRowCount());

  //gs.print("b" + cntb);

  //gs.print("c" + cntc);

 

}  

1 REPLY 1

JK1
Giga Expert

Hey Arnoud,

sorry to see this one so late. I havent tested with such huge amount, but this might help you somehow :

 

function tbl(){
  var rec2, count = 0;
  var rec = rec2 = new GlideRecord('incident');
  //rec.addActiveQuery();
  rec.query();
  while(rec.next()){
       rep(rec);
  }
  
}


function rep(rec){
    var rec2 = new GlideRecord('incident');
    rec2.initialize();
    rec2.number = rec.number;
    rec2.caller_id = rec.caller_id;
    rec2.category = rec.category;
    rec2.insert();
    //gs.addInfoMEsasge("Conter is " + count);
}

tbl()

 

 

Cheers,

Joro