OnLoad Client Script to only show choice depending on CaseType

Gregory Gribben
Tera Guru

As the subject states, I am trying to work on a client-side script. We have a catalog item that creates a case (sn_customerservice_case) with a predefined case type. I have also added a new choice to custom field choice list labeled u_substate

 

The requirement: There are several choices in this choice list that overlap between the additional choice lists, However, we are looking to present a new choice as an option for the substate field (custom field)  based on this predefined case type, as such I believe we would need an OnLoad client-side script, so once the case loads with the predefined case type the script will run and present the additional choice

 

I am new to coding and javascript but this is what I have so far, I did something similar for OnChange and it seemed to work but having issues with the OnLoad, it doesn't seem work at all. Any guidance would be much appreciated

 

function onLoad() {
    if (g_form.getValue('u_case_type') == 'services') {
        g_form.addOption('u_substate', 'ready_for_triage', 'Ready for Triage');
        return;
    }
}

 

1 ACCEPTED SOLUTION

Animesh,

 

My original code/logic was working, but after further analysis, there was another Client Script that was running and overwriting my changes. I have since updated the aforementioned client script with my code and its now working as intended. 

View solution in original post

7 REPLIES 7

Community Alums
Not applicable

Hi @Gregory Gribben ,

Please try below script 

 

function onLoad() {
    if (g_form.getValue('u_case_type') == 'services') {
        g_form.addOption('u_substate', 'ready_for_triage', 'Ready for Triage');
    }
}

 

You need to use backend value of the choice. If you want to give multiple addOptions you need to write that multiple times.

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

Sid_Takali
Kilo Patron
Kilo Patron

HI @Gregory Gribben 

When adding an option, you need three values:
g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);

the last one, index, is technically optional.

 

For removing it's: g_form.removeOption(<fieldName>, <choiceValue>);

https://servicenowguru.com/client-scripts-scripting_removing-disabling-choice-list-options/ 

 

function onLoad() {
    if (g_form.getValue('u_case_type') == 'services') {
        g_form.addOption('u_substate', 'ready_for_triage', 'Ready for Triage');
    }
}

 

Please mark my response Correct/Helpful,

Regards,

Siddharam

CezaryBasta
Tera Guru

Have you marked the UI Type properly to make sure your script will be triggered in proper environment? Also take a look at the field dependency on the dictionary level. It may be easier to setup than creating and managing client scripts here and here.

--
See more of my content here.

KevinBellardine
Kilo Sage

Hey @Gregory Gribben 

 

Just curious but why are you trying to do this? The case types are technically different tables, you should be able to configure your own choices just by going to Configure Choices on the field. You'll have everything you're inheriting, but you can add your own if you want.