The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Background script works, as a business rule it doesn't

caliban
Tera Expert

I have a few custom fields on the Release Table (rm_release).  2 of this fields are for string values of List fields, the fields will be hidden and are only there for reporting purposes.

Our workflow is as such create new Release(parent), > Create Release (child) > Create Task

find_real_file.png

This tab is visible on Child releases and what I would like to do is copy the string value from Environments Used to Env Used and Environments Not Used to Env Not Used.

The values in Environments Used & Not Used are populated only when a new task has been created and the Application Environment selected (which is a reference field to the CMDB_CI table).

I have run the below code as a background script and it worked as expected 

(function executeRule(current, previous /*null when async*/) {
	var env = new GlideRecord ('rm_release');
	env.query();
	
	while (env.next()){
		env.u_env_used_string = env.getDisplayValue('u_environments_used')
		env.setWorkflow(false);
		env.update();
	}

})(current, previous);

But I am struggling to get it to work as a Business Rule, I've tried before and after, but I think the issue is that the fields I'm trying to copy from are populated when the task is created so there is no Insert or Update of the record.  I have tried to update the record, adding work notes for example and still the fields are not populated.

Can anyone assist as to how to get the above code to run as release records are created or updated on an adhoc basis.  If not I'm guessing I could always run it as a scheduled Script execution.

 

1 ACCEPTED SOLUTION

Shashikant Yada
Tera Guru

You can try with OnAfter BR - Insert

(function executeRule(current, previous /*null when async*/) {
var env = new GlideRecord ('rm_release');
env.query();

while (env.next()){
env.u_env_used_string = env.u_environments_used.getDisplayValue();
env.setWorkflow(false);
env.update();
}

})(current, previous);

Thanks
Shashikant

Hit Helpful or Correct on the impact of response.

View solution in original post

2 REPLIES 2

Shashikant Yada
Tera Guru

You can try with OnAfter BR - Insert

(function executeRule(current, previous /*null when async*/) {
var env = new GlideRecord ('rm_release');
env.query();

while (env.next()){
env.u_env_used_string = env.u_environments_used.getDisplayValue();
env.setWorkflow(false);
env.update();
}

})(current, previous);

Thanks
Shashikant

Hit Helpful or Correct on the impact of response.

Hi Shashikant,

Thanks for your assistance, I tried it as an OnAfter BR with the small code change you suggested and it is now populating the field.