Async Business rule

Utkarsha
Tera Contributor

Hello All, 

I need to copy tags using async business rule on the target table from source table is the associated reference field is having tag, it should be copied to the associated target record.

 

 

I am using below code-

written async BR on control test table 

 
 
There is a reference field on target table which is referencing to one of the tables , and on this, there is a reference field which is referencing to the source table.
 
Could anyone please help me with the code here?
Thank you in advance,
 
3 REPLIES 3

Bert_c1
Kilo Patron

Hi,

 

I don't have either the 'sn_compliance_internal_control_test' or the 'sn_compliance_policy_statement' table in my instance. And I'm not clear if either is the "target table" or the "source table". Nor have you specified which table the business rule is defined on. I have some comments on the code. see:

 

 

var gr = new GlideRecord("sn_compliance_internal_control_test");
// no query is present, what record is expected? As is done for 'label_entry' table below. I suspect no info message
// appears, and that won't work for an "async" business role. us "gs.info();" and check Script Log statements for
// results.
if (gr.getUniqueValue()) {
	var contObId = current.u_control.content;
	gs.addInfoMessage(contObId);
}

//label_entry with control object and get all the label entry for the control objective
var labels = new GlideRecord('label_entry');
labels.addEncodedQuery('table=sn_compliance_policy_statement^table_key=' + contObId);
labels.query();
// use of "if" here will only process one record, use "while" here to process all from the above query
if (labels.next()) {
	gs.addInfoMessage(labels.label.getDisplayValue());	// use 'gs.info() here

	var grLabelINCTask = new GlideRecord('label_entry');

	grLabelINCTask.initialize();
	grLabelINCTask.table_key = current.sys_id;
	grLabelINCTask.table = current.sys_class.name;
	grLabelINCTask.label = current.label;
	grLabelINCTask.read = current.read;
	grLabelINCTask.title = current.u_type;
	grLabelINCTask.insert();
}

 

Hello @Bert_c1 ,

Thank you for replying 

I am talking about control test table.. please check the link below-

https://docs.servicenow.com/bundle/vancouver-servicenow-platform/page/product/compliance/task/t_Defi...

On this table, there is a reference field control.I have to copy tags to the control test record from the associated control objective, references from the control field via async business rule.

Let me know if you still have some queries

Thanks 

Bert_c1
Kilo Patron

I suggest you change the When to After, and test you logic, addInfoMessage will work there.

 

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideSystemAPI#r_GS-add...

 

which has:

 

addInfoMessage(Object message)

Adds an info message for the current session.

Use getInfoMessages() to retrieve the list of info messages being shown. This method is not supported for asynchronous business rules and cannot be used within transform scripts.

 

And for 'getUniqueValue' usage see:

 

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideRecordAPI#GlideRec...

 

There is no 'function' declaration in your code, should look like:

 

 

 

 

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

	var gr = new GlideRecord("sn_compliance_internal_control_test");
	// no query is present, what record is expected? As is done for 'label_entry' table below. I suspect no info message
	// appears, and that won't work for an "async" business role. use "gs.info();" and check Script Log statements for
	// results.
//	if (gr.getUniqueValue()) {
		var contObId = current.u_control.content;
		gs.addInfoMessage(contObId);
//	}

	//label_entry with control object and get all the label entry for the control objective
	var labels = new GlideRecord('label_entry');
	labels.addEncodedQuery('table=sn_compliance_policy_statement^table_key=' + contObId);
	labels.query();
	// use of "if" here will only process one record, use "while" here to process all from the above query
	if (labels.next()) {
		gs.addInfoMessage(labels.label.getDisplayValue());	// use 'gs.info() here

		var grLabelINCTask = new GlideRecord('label_entry');

		grLabelINCTask.initialize();
		grLabelINCTask.table_key = current.sys_id;
		grLabelINCTask.table = current.sys_class.name;
		grLabelINCTask.label = current.label;
		grLabelINCTask.read = current.read;
		grLabelINCTask.title = current.u_type;
		grLabelINCTask.insert();
	}
}

if 'u_control' is a reference field defined on that table the business rule is defined on.