Make mandatory value field in Name/Value pair

TD8
Tera Contributor

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?

1 ACCEPTED SOLUTION

Amit Verma
Kilo Patron
Kilo Patron

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 :

 

 

AmitVerma_1-1715661488772.png

AmitVerma_2-1715661522197.png

 

AmitVerma_3-1715661543077.png

 

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.

View solution in original post

2 REPLIES 2

Tony Chatfield1
Kilo Patron

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?

Amit Verma
Kilo Patron
Kilo Patron

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 :

 

 

AmitVerma_1-1715661488772.png

AmitVerma_2-1715661522197.png

 

AmitVerma_3-1715661543077.png

 

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.