- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 12:14 PM
It seems when the Name/Value pair fields are marked as mandatory, ServiceNow only cares about the Name field, the user does not need to put anything in the value field. How can I make both mandatory?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 09:40 PM
Hi @TD8
I was not able to find any direct way to control this behavior but we can fix it with a workaround. You can make the Key-Value Pairs field Mandatory at dictionary level which will make the Name field mandatory. For the value field, we can make use of an On-Change client script on the Key-Value pair field itself which will parse the Key-Value pair field to check if the value exists for the Name or not and will display error message accordingly. Refer below screenshots where I have tried this on Products field :
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var fieldValue = JSON.parse(g_form.getValue('u_products'));
for (var name in fieldValue) {
if (!fieldValue[name]) {
g_form.addErrorMessage('Value is Mandatory');
}
}
}
If you want the submission to be restricted, you can convert this to an On-Submit Client script.
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 08:31 PM
Hi, unfortunately your post doesn't make it clear where you are having this issue or how you are making the name:value pair mandatory? I would expect that for any name, null or empty string could be a valid value and expect you would need to test and validate the value.
Perhap you could update this thread to clarify your issue?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 09:40 PM
Hi @TD8
I was not able to find any direct way to control this behavior but we can fix it with a workaround. You can make the Key-Value Pairs field Mandatory at dictionary level which will make the Name field mandatory. For the value field, we can make use of an On-Change client script on the Key-Value pair field itself which will parse the Key-Value pair field to check if the value exists for the Name or not and will display error message accordingly. Refer below screenshots where I have tried this on Products field :
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var fieldValue = JSON.parse(g_form.getValue('u_products'));
for (var name in fieldValue) {
if (!fieldValue[name]) {
g_form.addErrorMessage('Value is Mandatory');
}
}
}
If you want the submission to be restricted, you can convert this to an On-Submit Client script.
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.