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

On which table are you running this BR and what type of BR is this.?

Please hit like and mark my response as correct if that helps
Regards,
Musab

-O-
Kilo Patron
Kilo Patron

When and on which table is this supposed to run? If we re-write the script to be a bit more readable, by renaming the two GlideRecords, as below:

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

	alm_hardware.addNullQuery('assigned_to');
	alm_hardware.addQuery('u_loanable', true);
	alm_hardware.addActiveQuery('alm_hardware_tag');
	alm_hardware.query();

	while (alm_hardware.next()) {
		var incident = new GlideRecord('incident');

		incident.addQuery('u_alm_hardware_tag', alm_hardware.alm_hardware_tag); // match the alm_hardware tag between incident and alm_hardware
		incident.query();

		if (incident.next()) {
			alm_hardware.assigned_to = incident.caller_id;
			alm_hardware.update();
		}
	}
})(current, previous);

we can immediately see a couple of problems:

- this script will loop through all(!) alm_hardware records where assigned_to is empty (!?) and u_loanable is true (the addActiveQuery filter will have no effect as there is no active field on alm_hardware) - is this what is desired (to handle all records of alm_hardware)?

- the query to select an incident is again wrong as there is no field called alm_hardware_tag on alm_hardware - in other words for all assets the same incident will be selected, as the faulty query will be ignored.

The solution will be different depending on when and on which table the Business Rule is executed - can you share that information?

Table is on alm_hardware.

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);

-O-
Kilo Patron
Kilo Patron

Forgot to mention, in case this update should only run when an incident has been created through the Record Producer, further conditions should be added.