How to set value of a choice field, that has Reference qual set up?

michaelpelenski
Kilo Contributor

Hello, Community experts

I'm targeting to automate Vendor's risk rating definition, that will be based on conditions, that come out of GRC module.

When reviewing risk rating field dictionary, I've noticed that it's values come from sn_grc_choice table, and there is a reference qual set up: 

javascript: new sn_grc.GRCChoiceUtils().getGRCChoices('risk_rating', current.sys_class_name)

When I attempt to set the value of this choice field via Business rule, neither of approaches I've treid so far work: 

setValue(); setDisplayValue(), assigning value through a variable, etc. ended up in a new value appearing in the dropdown choice list rather then setting one of exising values. 

What am I doing wong here? How to map the values correctly for such type of a field? 

P.S. Regular choice fields, that get configured through choice list work fine for me, but this one makes me go crazy.

 

1 ACCEPTED SOLUTION

Can you try the below script. I made few improvements. Where are you running this business rule?

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

	var risk_ratings = new GlideRecord('sn_grc_choice');
	risk_ratings.addQuery('choice_category','risk_rating');
        risk_ratings.addQuery('name','high');
	risk_ratings.query();
	// gs.addInfoMessage('found ' + risk_ratings.getRowCount(), 'risks');
	if (risk_ratings.next()){
		var risk = risk_ratings.getValue('sys_id');
		gs.addInfoMessage('sys id is ' + risk);
		var vendor = new GlideRecord('core_company');
		vendor.addQuery('name', 'test');
		vendor.query();
		// gs.addInfoMessage('Vendors count ' + vendor.getRowCount(), 'risks');
		while(vendor.next()){
			vendor.setValue('risk_rating', risk);
vendor.update();
			gs.addInfoMessage('Risk rate is set to '+risk);
		}
	}
})(current, previous);

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

While setting the risk rating field, are you using the display value to set the field. Instead can you try getting the sys_id of the record from sn_grc_choice table and set that sysid using setValue.


Please mark this response as correct or helpful if it assisted you with your question.

Thanks for the suggestion.

I'm able to retreive sys_id of a choice record from sn_grc_choice table. But when I attempt to use this sys_id to set value of risk_rating choice field in core_company table, it turns to Undefined instead of regular sys_id. 

My code snippet: 

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

	var risk_ratings = new GlideRecord('sn_grc_choice');
	risk_ratings.addQuery('choice_category','risk_rating');
	risk_ratings.query();
	gs.log('found ' + risk_ratings.getRowCount(), 'risks');
	while(risk_ratings.next()){
		//gs.log('')
		if(risk_ratings.name == 'high'){
			var risk = risk_ratings.getUniqueValue();
			gs.log('sys id is ' + risk, 'risks');
			var vendor = new GlideRecord('core_company');
				vendor.addQuery('name', 'test');
				vendor.query();
				gs.log('Vendors count ' + vendor.getRowCount(), 'risks');
				while(vendor.next()){
					var risk_level = vendor.setValue('risk_rating', risk);
					gs.log('Risk rate is set to ' + risk_level, 'risks');
				}
			
			break;
		}else {
			continue;
		}
		
	}	
	
})(current, previous);

In my system logs I see following: 

 

https://gyazo.com/f90a71fb5c53761d289830b64c5105d0

 

Any ideas what happens to sys_id? 

Thanks in advance.

Can you try the below script. I made few improvements. Where are you running this business rule?

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

	var risk_ratings = new GlideRecord('sn_grc_choice');
	risk_ratings.addQuery('choice_category','risk_rating');
        risk_ratings.addQuery('name','high');
	risk_ratings.query();
	// gs.addInfoMessage('found ' + risk_ratings.getRowCount(), 'risks');
	if (risk_ratings.next()){
		var risk = risk_ratings.getValue('sys_id');
		gs.addInfoMessage('sys id is ' + risk);
		var vendor = new GlideRecord('core_company');
		vendor.addQuery('name', 'test');
		vendor.query();
		// gs.addInfoMessage('Vendors count ' + vendor.getRowCount(), 'risks');
		while(vendor.next()){
			vendor.setValue('risk_rating', risk);
vendor.update();
			gs.addInfoMessage('Risk rate is set to '+risk);
		}
	}
})(current, previous);

Please mark this response as correct or helpful if it assisted you with your question.

Thanks much - I've tried your code and it worked perfectly fine. Seems that getUniqueValue() method was my weak spot.