Show choices by what field value is in business rule

KENxx
Tera Contributor

Hello, teams

 

I have a question about businessrule.
We have a requirement to show choices "u_substatus" by what "contact_type" is.
For example, when "contact_type" is "system-error" we want to show "u_substatus" choices('1', '2').
When "contact_type" is "report" we want to show "u_substatus" choices('3', '4').
I made a businessrule and wrote a script below, but didn't work.

Any suggestion, that would be so helpful.

 

Thanks.

 

★When to run
▼When after
▼Filter Conditions
Contact type is System-error
or Contact type is Report

★Advanced
▼Script

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

 

// Show substatus choices by contact types

var fieldOptions = [];
if (current.contact_type == 'system-error') {
 fieldOptions = ['1', '2'];
} else if (current.contact_type == 'report') {
 fieldOptions = ['3', '4'];
}

 current.u_substatus.setChoices(fieldOptions);
})(current, previous);

1 ACCEPTED SOLUTION

Hi @KENxx adjusted the code accordinlgy

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Remove substatus choices by contact types

g_form.clearOptions('u_substatus');
if (newValue == 'system-error') {
  g_form.addOption('u_substatus', '20', 'Control confirmation');
  g_form.addOption('u_substatus', '21', 'Under development request');
} else if (newValue == 'report')
g_form.addOption('u_substatus', '22', 'Under development analysis');
}

Regards
Harish

View solution in original post

5 REPLIES 5

Bert_c1
Kilo Patron

Hi KENxx,

 

Business rules run when a record is displayed, deleted, or updated. See:

 

https://docs.servicenow.com/search?q=business%20rules

 

So that approach doesn't seem right for your situation, unless it runs on Display. But where is 'setChoices()' documented? And what is expected when 'contact_type' is changed on a form?

 

Maybe someone can suggest how you limit a field's values based on the value of another field.