how to set priority based on list field?

KM SN
Tera Expert

Based on the list field values i am setting priority and in the list if values are different priority will be with higher order based on values selected.

 

But here instead of declaring 20 variables how i need to check the values and if possible can any one write script using switch instead of if and else ifs.(Over all concept is we need to optimize the code)

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var a = g_form.getValue('abbuse');

var Deathofachild_p1 = 'be1d7d004f38e950a9a2a38021ad487d';
var PhysicalAbuse_p1 = 'd33c79cc4ff4e950a9a2a38021ad4846';
var SexualAbuseExploitation_p1 = '86acb1004f38e950a9a2a38021ad4884';
var HumanTrafficking_p1 = '58ccb1004f38e950a9a2a38021ad4893';

var MoralTurpitude_p2 = '679c31004f38e950a9a2a38021ad48f6';
var NeglectResponse_p2 = '06ec39004f38e950a9a2a38021ad48eb';
var DependencyResponse_p2 = '4fccb1004f38e950a9a2a38021ad4897';
var ImproperSupervision_p2 = 'f4fcb9004f38e950a9a2a38021ad483f';
var ImproperCare_p2 = '0ffcb9004f38e950a9a2a38021ad4841';
var ImproperDisciplinenoInjuries_p2 = 'ad0df5004f38e950a9a2a38021ad4804';

var ImproperDisciplineInjuries_p3 = 'd41d3d004f38e950a9a2a38021ad489f';
var Extendingcircumstancesduetodeathofparents_p3 = '726df1404f38e950a9a2a38021ad48d0';
var Extendingcircumstancesduetonaturaldisaster_p3 = '64344ac84f38e950a9a2a38021ad48c3';
var EmotionalMentalPsychologicalAbuse_p3 = '05bcb1004f38e950a9a2a38021ad4891';
var AbandonmentandSafeSurrender_p3 = 'b14dfd004f38e950a9a2a38021ad4897';

var ImproperMedicalRemedialCare_p4 = '645db1404f38e950a9a2a38021ad48be';
var InjuriousEnvironment_p4 = '852d39004f38e950a9a2a38021ad48f6';
var InjuriousEnvironmentDomesticViolence_p4 = 'c83dbd004f38e950a9a2a38021ad48b6';
var InjuriousEnvironmentSubstanceAbuse_p4 = 'b63d31404f38e950a9a2a38021ad48fc';
var AdoptioninViolationofLaw_p4 = '886d71404f38e950a9a2a38021ad4801';

var prty = g_form.getControl('priority');


if (a.indexOf(Deathofachild_p1) > -1 || a.indexOf(PhysicalAbuse_p1) > -1 || a.indexOf(SexualAbuseExploitation_p1) > -1 || a.indexOf(HumanTrafficking_p1) > -1) {
g_form.setValue("priority", '1');

prty.style.color = 'red';

} else if (a.indexOf(MoralTurpitude_p2) > -1 || a.indexOf(NeglectResponse_p2) > -1 || a.indexOf(DependencyResponse_p2) > -1 || a.indexOf(ImproperSupervision_p2) > -1 || a.indexOf(ImproperCare_p2) > -1 || a.indexOf(ImproperDisciplinenoInjuries_p2) > -1) {

g_form.setValue("priority", '2');
prty.style.color = 'yellow';

} else if (a.indexOf(ImproperDisciplineInjuries_p3) > -1 || a.indexOf(Extendingcircumstancesduetodeathofparents_p3) > -1 || a.indexOf(Extendingcircumstancesduetonaturaldisaster_p3) > -1 || a.indexOf(EmotionalMentalPsychologicalAbuse_p3) > -1 || a.indexOf(AbandonmentandSafeSurrender_p3) > -1) {

g_form.setValue("priority", '3');
prty.style.color = 'blue';

} else if (a.indexOf(ImproperMedicalRemedialCare_p4) > -1 || a.indexOf(InjuriousEnvironment_p4) > -1 || a.indexOf(InjuriousEnvironmentDomesticViolence_p4) > -1 || a.indexOf(InjuriousEnvironmentSubstanceAbuse_p4) > -1 || a.indexOf(AdoptioninViolationofLaw_p4) > -1) {

g_form.setValue("priority", '4');
prty.style.color = 'black';
}
}

 

 

1 ACCEPTED SOLUTION

Let me frame the requirement clearly

 

i have reference list field where we can find 20 values and 20 were belongs to 4 priorities.

(like if selected value have priority value 4 means low, if priority value is 1 means critical etc)

pls see SS for better understanding.

 

user can select multiple values or single value, if so we need set priority in case form based on  lowest priority value as list consist multiple.

 

Example 1:

 

assume if list consists 5 abuses

Human Trafficking, Priority Value:1

Improper Care, Priority Value: 2

Abandonment and Safe Surrender , Priority Value: 3

Adoption in Violation of Law, Priority Value: 4

Emotional/Mental/Psychological Abuse, Priority Value: 3

 

then priority should be critical in our case form as one of list  consist highest priority value as 1

 

Example 2:

 

if list consist two abuses

Improper Care, Priority Value: 2

Abandonment and Safe Surrender , Priority Value: 3

 

then priority should be High in our case form as one of list consist highest priority value as 2.

 

View solution in original post

11 REPLIES 11

@KM SN 

share the latest script as per your discussed approach.

did you debug it further?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;}

var a = g_form.getValue('alleged_abuse_or_neglect');

var gaa = new GlideAjax('getpriorityvalues');
gaa.addParam('sysparm_name','getpriorities');
gaa.addParam('sysparm_options',a);
gaa.getXML(showDetails);

function showDetails(response){

var answer = response.responseXML.documentElement.getAttribute("answer");
var parser = JSON.parse(answer);
var priority = parser[priority];

g_form.addInfoMessage(answer);

}

 

 

Script Include:

 

var getpriorityvalues = Class.create();
getpriorityvalues.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getpriorities: function() {

var list = this.getParameter('sysparm_options');
var list_array = field_value.split(',');

for (var i = 0; i < list_array.length; i++) {

var opt = new GlideRecord('x_guid2_child_welf_workspace_list_options');
opt.addQuery('sys_id', list_array[i]);
opt.query();
var x = {};
if (opt.next()) {

x.priority = opt.priority_value.toString();
}

return JSON.stringify(x);

}
},

type: 'getpriorityvalues'
});

 

 

But its not returning anything. Expectation is it should bring priority values of selected list and later we need to write conditions to set higher priority value Priority of the case .