populating dept field in hardware assets

jeremy1989
Kilo Contributor

Hi I am trying to populate the department field on my hardware asset table which is linked to the assigned to field (in another BR that currently works). However this Business Rule(BR) only works on new hardware assets made. For all of the hardware assets that already exist and do have an assigned to but not a department, I am trying to fill in the department field. So I am running the following script   through a scheduled script execution that only runs once and fills in all of the department fields on each hardware asset where the assigned_to is present. I have posted the code below:

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

//A method to populate the Department field located in Hardware Assets by pulling the department

//from the assigned_to field.

PopulateHdAssetDept();

function PopulateHdAssetDept(){

          //creating the variable, gr, which is a GlideRecord object for the Hardware Assets table (alm_hardware)

  var gr = new GlideRecord('alm_asset');

              //query the records where the field 'assigned_to' is NOT null (empty)

  gr.addNotNullQuery('assigned_to');

              //query the records where the field 'department' IS null (empty)

              gr.addNullQuery('department');

              //issues the query to the database based upon the above field criteria

  gr.query();

              //used to test only 5 records

              setLimit(5);

  //gs.log('Populated ' + gr.getRowCount() + 'with department');

  var dept = getValue('alm_asset.department');

  var assgnTo = getValue('alm_asset.assigned_to');

  while (gr.next()) {

                //Not sure if this is the correct fields? It should populate the department with the assigned_to's department      

  dept = assgnTo;

  //gs.log(' ' + gr.name + ' ');

  gr.update();

  }

}

1 ACCEPTED SOLUTION

Hi Jeremy,


I tried Asset Management module(alm_asset) itself. Please follow the below steps:


1. Navigate to System definition -> Scripts Background


2. Copy & paste the below script there


3. Click run script.


4. Verify the data.



var gr = new GlideRecord('alm_asset');


              //query the records where the field 'assigned_to' is NOT null (empty)


  gr.addQuery('assigned_to','!=',null);


              //query the records where the field 'department' IS null (empty)


              gr.addQuery('department',null);


              //issues the query to the database based upon the above field criteria


  gr.query();


              //used to test only 5 records


          //   setLimit(5);


  //gs.log('Populated ' + gr.getRowCount() + 'with department');


// var dept = getValue('alm_asset.department');


// var assgnTo = getValue('alm_asset.assigned_to');


  while (gr.next()) {


//gs.print('inside');


gr.department=gr.assigned_to.department;


  gr.update();


}


View solution in original post

9 REPLIES 9

Hi Dilip,



Thank you for your continued help , but this new code I just tried and was


unsuccessful for me. Are the addQuery functions that you changed from addNotNull etc. changed for a reason, as in is the functionality any different or is it a different way of writing the original (addNotNull)   Which module are you using to   run your script? Is it


the scheduled jobs module? Also do you mind putting your own comments in


before some of the functions you use ( feel free to delete my comments)?


Why are we making an object of the table alm_asset instead of the table


that extends from it, alm_hardware? I also was curious what the purpose of


the commented out gs.print is? Also, is everything that is commented out,


such as the variables near the end, supposed to be? You can feel free to


delete any commented out code that is not going to be used at any time. I


very much appreciate your continued help.



Best,


Jeremy



On Tue, Dec 6, 2016 at 3:13 AM, dilipdj <community-no-reply@servicenow.com>


Hi Jeremy,


I tried Asset Management module(alm_asset) itself. Please follow the below steps:


1. Navigate to System definition -> Scripts Background


2. Copy & paste the below script there


3. Click run script.


4. Verify the data.



var gr = new GlideRecord('alm_asset');


              //query the records where the field 'assigned_to' is NOT null (empty)


  gr.addQuery('assigned_to','!=',null);


              //query the records where the field 'department' IS null (empty)


              gr.addQuery('department',null);


              //issues the query to the database based upon the above field criteria


  gr.query();


              //used to test only 5 records


          //   setLimit(5);


  //gs.log('Populated ' + gr.getRowCount() + 'with department');


// var dept = getValue('alm_asset.department');


// var assgnTo = getValue('alm_asset.assigned_to');


  while (gr.next()) {


//gs.print('inside');


gr.department=gr.assigned_to.department;


  gr.update();


}


Thanks Dilip,



I was running it in the correct area, I had another BR and or ACL conflicting with this that I had to rectify first. I appreciate the tweaks you made to my code.


For my knowledge, could you   tell me why you prefer: gr.addQuery('assigned_to','!=',null); as opposed to gr.addNotNullQuery('assigned_to'); . Thank you for the suggestions.


Hi Jeremy,


Though gr.addQuery('assigned_to','!=',null) and gr.addNotNullQuery('assigned_to') are same, I have heard of few instances wherein gr.addNotNullQuery did not work as expected. We can use both


I know my formatting was not the best, were there certain parts of the original code that were supposed to be commented out besides what I already have?