Inserting Records into a Table via a insert();

calissev
Giga Contributor

I downloaded Crossfuze Configuration Item Groups update set and am trying to figure out how to programmically get items into each group.   I have created a script that pulls CI items and puts them into arrays.   This part of the script is working fine.   The problem that I am having is getting the items in the arrays into the correct group(s) that I have made. I am having no problem getting the correct groups using a glide record for cmdb_ci_group.   I have no problem when I use a glide record to get the list of group members from the cmdb_ci_grmember.   The problem I am having is inserting and/or updating the record with the information that I have stored in the arrays. I keep getting the following error:

Script: The Servers outside: modnet313   //this is the CI that I am trying to add to the u_cmdb_ci_grmember list

Get for non-existent record: u_cmdb_ci_grmember:sys_id, initializing

Get for non-existent record: cmdb_ci:Windows Servers - PRD, initializing   //this is the group cmdb_ci_group that I am trying to add the CI to.

Get for non-existent record: cmdb_ci:Windows Servers - PRD, initializing

This is my script:

grList();

winListPRD();

theGRList = grList();

winPRDList = winListPRD();

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

                      gs.print("The List outside:" + ' ' + theGRList[i]); //just to see if I have data which I know i do.

                        if(theGRList[i] == 'Windows Servers - PRD'){         //the group that I am trying to get the CI added to.

                                              for(var j = 0; j < 4; j++){                                                                         //a test number I don't want to load all by using .length yet

                                                                      gs.print("The Servers outside:" + ' ' + winPRDList[j]); //just to see if I have data which I know i do.

/ ****This is where my issue is******/

                                                                      var grP = new GlideRecord('u_cmdb_ci_grmember');   //there is something wrong here down...

                                                                      grP.get('sys_id');                                                                                                         //i have tried without this and get the same error

                                                                      grP.initialize();

                                                                      grP.u_ci = winPRDList[j];                                                                        

                                                                      grP.u_ci_group = 'Windows Servers - PRD';

                                                                      grP.insertWithReferences();                                                                     //I have tried just insert(), I have tried update(), I have tried updateWithReferences();   no

                                              }   //end for                                                                                                                                               //go I still get nothing updated.   I think my problem is tying the u_cmdb_ci_grmember to the

                                }   //end if                                                                                                                                                                 //u_cmdb_ci_group correctly.

                }   /end for                                                                                                                                                                            

    //the functions below all work as designed and return the items I need in arrays                

                    function grList(){

                                var grpList = [];

                                var ciGroup = new GlideRecord('cmdb_ci_group');

                              ciGroup.addNotNullQuery('name');

                              ciGroup.query();

                              while(ciGroup.next()){

                            grpList.push(ciGroup.name + '');                                                      

                          }

                          return grpList;

                      }   //end function grList

                      function winListPRD(){

                                    var winListP = [];

                                    var winCIGroupP = new GlideRecord('cmdb_ci_win_server');

                                    winCIGroupP.addQuery('install_status', 1);

                                      winCIGroupP.addNotNullQuery('name');

                                      winCIGroupP.addQuery('used_for', 'Production');

                                      winCIGroupP.query();

                                                  while(winCIGroupP.next()){

                                            // gs.print(winCIGroupP.name + ' ' + winCIGroupP.used_for);

                                                  winListP.push(winCIGroupP.name + '');

                                                }

                        return winListP;

                      }   //end function winListPRD

1 ACCEPTED SOLUTION

calissev
Giga Contributor

I figured it out.   My problem was that the two fields that I was trying to update were reference fields. One that referenced the CI and one that referenced the Group.   I didn't need to update the fields I just needed to update the Display Value.   I didn't need the extra information for the cmdb_ci_group either.   I just needed the GlideRecord for the u_cmdb_ci_grmember.   It was easier than I was making it.



winListPRD();


var winPRDList = winListPRD();


//Updating the Windows Servers - PRD list



  for(var j = 0; j < winPRDList.length; j++){


  var grP = new GlideRecord('u_cmdb_ci_grmember');


  grP.initialize();


  grP.u_ci.setDisplayValue(winPRDList[j]);


  grP.u_ci_group.setDisplayValue('Windows Servers - PRD');


  grP.insert();


  }



/*This function gets the existing list of windows servers that


  are listed as production.   It then does a compare to what servers


  are existing in the groupings of servers it sends back only the


  servers that are not already in the list*/



  function winListPRD(){


  var winListP = [];


  var winListPE = [];


  var winListPR = [];


  var auP = new ArrayUtil();




  var winCIGroupP = new GlideRecord('cmdb_ci_win_server');


  winCIGroupP.addQuery('install_status', 1);


  winCIGroupP.addNotNullQuery('name');


  winCIGroupP.addQuery('used_for', 'Production');


  winCIGroupP.query();



  while(winCIGroupP.next()){


  winListP.push(winCIGroupP.name + '');


  }


  var ciExistP = new GlideRecord('u_cmdb_ci_grmember');


  ciExistP.query();



  while(ciExistP.next()){


//this needs to be the display value so that when you use the array utility it compares the same thing


//when I originally pushed the u_ci it pushes the sys_id


  winListPE.push(ciExistP.u_ci.getDisplayValue() + '');


  }


  //what is in the first one that is not in the second one


  winListPR = auP.diff(winListP, winListPE);


  return winListPR;



  }


View solution in original post

1 REPLY 1

calissev
Giga Contributor

I figured it out.   My problem was that the two fields that I was trying to update were reference fields. One that referenced the CI and one that referenced the Group.   I didn't need to update the fields I just needed to update the Display Value.   I didn't need the extra information for the cmdb_ci_group either.   I just needed the GlideRecord for the u_cmdb_ci_grmember.   It was easier than I was making it.



winListPRD();


var winPRDList = winListPRD();


//Updating the Windows Servers - PRD list



  for(var j = 0; j < winPRDList.length; j++){


  var grP = new GlideRecord('u_cmdb_ci_grmember');


  grP.initialize();


  grP.u_ci.setDisplayValue(winPRDList[j]);


  grP.u_ci_group.setDisplayValue('Windows Servers - PRD');


  grP.insert();


  }



/*This function gets the existing list of windows servers that


  are listed as production.   It then does a compare to what servers


  are existing in the groupings of servers it sends back only the


  servers that are not already in the list*/



  function winListPRD(){


  var winListP = [];


  var winListPE = [];


  var winListPR = [];


  var auP = new ArrayUtil();




  var winCIGroupP = new GlideRecord('cmdb_ci_win_server');


  winCIGroupP.addQuery('install_status', 1);


  winCIGroupP.addNotNullQuery('name');


  winCIGroupP.addQuery('used_for', 'Production');


  winCIGroupP.query();



  while(winCIGroupP.next()){


  winListP.push(winCIGroupP.name + '');


  }


  var ciExistP = new GlideRecord('u_cmdb_ci_grmember');


  ciExistP.query();



  while(ciExistP.next()){


//this needs to be the display value so that when you use the array utility it compares the same thing


//when I originally pushed the u_ci it pushes the sys_id


  winListPE.push(ciExistP.u_ci.getDisplayValue() + '');


  }


  //what is in the first one that is not in the second one


  winListPR = auP.diff(winListP, winListPE);


  return winListPR;



  }