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

Harish KM
Kilo Patron
Kilo Patron

Hi @KENxx to show or hide choices Client script is the way to do because choice needs to be displayed upon changing the value where as Business rule will not work until you hit update.

Refer doc below

https://developer.servicenow.com/dev.do#!/search/vancouver/All/addOption/

Regards
Harish

KENxx
Tera Contributor
Thank you for replying, Mr.Harish.
 
I tried it on client script below, but didn't work also.
I'm wondering how to remove 'u_substatus' choices by what "contact_type" field is.
Any wrong way you can see, wolud you tell me what wrong is?
 
Best Regards,
 
▼Type
 on change
 
▼Field name  
 Contact type
 
▼Script
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //Remove substatus choices by contact types
    if (newValue == 'system-error') {
  g_form.clearOptions('u_substatus', '20', 'Control confirmation');
  g_form.clearOptions('u_substatus', '21', 'Under development request');
    } else if (newValue == 'report')
        g_form.clearOptions('u_substatus', '22', 'Under development analysis');
}

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

KENxx
Tera Contributor

Thank you for the answer, Mr.Harish.

 

It did work properly.

It was so helpful.

Thank you again.

 

Best Regards,