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

Musab Rasheed
Tera Sage
Tera Sage

Hi,

What exactly is failing in script.? Did you try to add logs and which part is not working

Regards,

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

jjones4773
Tera Guru

where would I add a log?  Just that it doesn't updated the asset record "assigned_to" with the incident "caller_id".

 

You are trying to add assigned to on incident table hence it is not working ,

replace this.

if(assetinc.next()){ assetinc.setValue('assigned_to',asset.caller_id); assetinc.update();

}

With this

if(assetinc.next()){

asset.assigned_to = assetinc.caller_id asset.update();

}

Mark my answer as correct or hit like based on impact

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

jjones4773
Tera Guru

Musab,

Changed the script to:

(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');
//  gs.addInfoMessage("Asset tag:" + current.asset_tag);
//    gs.print("Asset tag:" + current.asset_tag);
//    gs.log ('Asset tag '+ current.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()){
        asset.assigned_to = assetinc.caller_id;
        asset.update();
    }
}

})(current, previous);

 

Still not updating.