Trying to do an onAfter business rule that checks against a select value and inserts it into another table.

jjones4773
Tera Guru

I am creating a Record Producer form that calls on the hardware_alm table for a selected lookup select box listing of assets.

find_real_file.png

From there, I'm populating on the Incident table a value called u_asset_tag with the selected value tied to the Lookup value field.

find_real_file.png

Once that is populated, I want to query against that value in hardware_alm assets and populate the "Assigned_to" field within Assets the same as  the "caller_id" from Incident.

find_real_file.png

assign to

find_real_file.png

Below is the business rule, but I'm missing something obviously.

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
    var asset = new GlideRecord('alm_hardware');
asset.addNullQuery('assigned_to');
asset.addQuery('u_loanable', true);
asset.addActiveQuery('asset_tag');
asset.query();
while(asset.next()) {
    var assetinc = new GlideRecord('incident');
    assetinc.addQuery('u_asset_tag',asset.asset_tag); // match the asset tag between incident and asset
    assetinc.query();
    if(assetinc.next()){
        assetinc.setValue('assigned_to',asset.caller_id);
        assetinc.update();
    }
}

})(current, previous);

Any assistance is appreciated

1 ACCEPTED SOLUTION

So if I understand correctly you have Record Producer "Open an IT Support Ticket" that creates incidents. And when an incident is submitted from such a record producer you want to copy the Caller id information from the incident into the Asset (alm_hardware) record associated with the incident through field "Asset tag". Right?

If my understanding is correct, the Business Rule should be configured for table "Incident". Business Rules are executed around Database Operations. Since your Record Producer only "operates" on table Incident, creating a Business Rule for table Hardware (alm_hardware) will not work. The Business Rule should be created for table Incident, onAfter and for DB Operation Insert. It should also have a condition that "Asset tag" is not empty.

In that case the Business Rule does not need to "load" from table Incident, 'cause global variable current defined in Business Rules will be the Incident records the script needs. The script just should load and update the correct Hardware (asset):

 

(function executeRule (current, previous) {
	var alm_hardware = new GlideRecord('alm_hardware');

	// Not sure if this is needed here - might be counter productive
	// It is assumed that table alm_hardware has a custom True/False field
	// called u_loanable
	// I wonder why field "asset_function" is not used, that is a choice and
	// among others it has a choice called "loaner".
	alm_hardware.addQuery('u_loanable', true);

	// Filter to the asset that has the same tab as the one in the incident
	// The assumption here is that u_alm_hardware_tag is field Asset Tag of Incident
	// (as current is the current incident) and that it is a field of type string
	// If field Asset Tag on tab "Loaner Request" on Incident is actually calle something
	// else then u_alm_hardware_tag, you need to adjust/correct on the next line
	alm_hardware.addQuery('asset_tag', current.u_alm_hardware_tag);

	alm_hardware.query();

	if (alm_hardware.next()) {
		// If an asset has been found and it is a "loanable" one, update its
		// Assigned to attribute with the current Incident's Caller id
		// again here current is the Incident that has just been generated
		alm_hardware.assigned_to = current.caller_id;
		alm_hardware.update();
	}
})(current, previous);

View solution in original post

10 REPLIES 10

Tony Chatfield1
Kilo Patron

Hi, I would start by reviewing and debugging your code using gs.info('test') type statements to validate your results and expectations. You can do much of this in a dev instance background window which will allow for real time debugging.

The first issue I think you have is your attempt to override the method addActiveQuery by passing in a parameter, I don't see this behavior documented anywhere and my understanding is that addActiveQuery()/addInactiveQuery() are dependent on an 'active' field on the table.

You can test this yourself in a background window with a simple script.
These 3 queries return the same result;
As there is no 'active' field OOB for 'alm_hardware' and so the active\inactive queries are not matched to a field and in all 3 queries the first record found by the glidequery is returned (normally the lowest sys_id).

var test1 = new GlideRecord('alm_hardware');
test1.addActiveQuery();
test1.query();

if(test1.next()) {
gs.info('active ' + test1.sys_id);
}

var test2 = new GlideRecord('alm_hardware');
test2.addInactiveQuery();
test2.query();

if(test2.next()) {
gs.info('inactive ' + test2.sys_id);
}


var test3 = new GlideRecord('alm_hardware');
test3.query();

if(test3.next()) {
gs.info('first record ' + test3.sys_id);
}