- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 08:27 PM - edited 05-13-2023 04:43 AM
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';
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2023 12:30 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 09:51 PM
is the above code working fine and you just want to optimize it?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 10:43 PM
Yeah, Its working but instead of declaring 20 sys ids in 20 different variables in script do we have anything that can be possible through backend and using them in script , actually i need to optimize it and instead of 'if' and 'else ifs' can we use switch ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 11:27 PM
you can store those 20 sysIds in array and then check if the value of abbuse is present in that array or not
No need to declare 20 variables; only 1 array is sufficient
I hope you can update the code from your side.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2023 12:30 AM
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.