The CreatorCon Call for Content is officially open! Get started here.

Defined related list of assets

chadlockwood
Kilo Sage

I am attempting to create a defined related list of assets to apply to sc_task. In our departure process, a RITM is generated to track the progress of the departure and an SCTASK is created to collect the assets assigned to the leaving employee. The requestor of the RITM is the manager of the leaving employee. The defined related list should appear on the SCTASK and include only the assets that are assigned to the leaving employee. When the RITM is generated, the RITM sys_id is applied to a "Departure RITM" reference field on the user record.

To start, I must first query the user table to find a user record where the Departure RITM equals the parent.request_item of the hardware return SCTASK. Then, query the alm_asset table for assets assigned to the found user record.

The 'Query with' code on the relationship is:

var userRec = new GlideRecord('sys_user');

userRec.addQuery('u_departure_ritm',parent.request_item.sys_id);

userRec.query();

if(userRec.next()){

  current.addQuery('assigned_to',userRec.sys_id);

  current.addNotNullQuery('assigned_to');

}

When I open the hardware return task, where I know the RITM is applied to a user record, I get the expected one asset record in the related list. SUCCESS

However, when I open any other SCTASK, where I know the RITM is NOT applied to a user record, the related list contains all assigned assets. FAIL

I have used logging to show that the code is working correctly in the first scenario and that the userRec.next() does not run in the second scenario. I am looking for how to return a blank related list when no assigned assets record should be found.

5 REPLIES 5

Cory CJ Wesley
Kilo Sage
Kilo Sage

Try adding a if statement that adds a filter if the returned rows are 0 BEFORE the while loop.



var userRec = new GlideRecord('sys_user');


userRec.addQuery('u_departure_ritm',parent.request_item.sys_id);


userRec.query();


if (userRec.getRowCount() == 0){


current.addQuery('whatever goes here to make nothing appear');


}else{


if(userRec.next()){


  current.addQuery('assigned_to',userRec.sys_id);


  current.addNotNullQuery('assigned_to');


}