- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2016 09:20 AM
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();
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2016 05:29 AM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2016 10:06 AM
Hi Jeremy,
Try this. It should work
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()) {
var grdep = new GlideRecord('sys_user');
grdep.addQuery('sys_id',gr.assigned_to);
grdep.query();
if(grdep.hasnext())
{
gr.department=grdep.department;
}
//Not sure if this is the correct fields? It should populate the department with the assigned_to's department
//gs.log(' ' + gr.name + ' ');
gr.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2016 10:17 AM
Hi Dilip, thank you for the reply. I am still not getting any of the hardware asset records with an empty department to fill. Any ideas why?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2016 12:12 AM
Hi Jeremy,
I just modified the code and tested it in PDI using background script and it worked perfectly!!!
Use this
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2016 12:13 AM
Commented the print statement
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();
}