Difference between value and display value

Khaled RAJHI
Tera Contributor

Hello everyone,

what is the difference between value and display value of a field ?

Could anyone explain to me to understand this particularity

I want to use g_form_setValue to set the value of assignment group to Hardware when the short description field contain 'disk', onSubmit

I need assistance to correct the query or missing parameter in the query, thank you

 
function onSubmit() {
 var short_description = g_form.getValue('short_description');
 var shdes = short_description.toUpperCase();
   if (shdes.includes("DISK"))
   {
  g_form.setValue('assignment_group','Hardware' );
   } }
1 ACCEPTED SOLUTION

Hello @Khaled RAJHI ,

 

Certainly! In the `g_form.setValue` function in ServiceNow, the third parameter (`displayValue`) is optional. The basic syntax is as follows:

 

g_form.setValue(fieldName: string, value: string, displayValue: string);

However, in many cases, you might not need to explicitly provide the `displayValue`. The `displayValue` parameter is typically used when you want to set both the value (sys_id) and the corresponding human-readable label at the same time.

 

In your specific scenario of setting the assignment group based on a condition, you are only interested in setting the sys_id of the assignment group based on the value associated with the condition. Therefore, you provide only the `fieldName` and `value` parameters.

 

If you had the display value and wanted to set both the sys_id and the display value simultaneously, you would include the third parameter:

g_form.setValue('assignment_group', 'sys_id_of_hardware_group', 'Hardware');

In your case, since you are setting the value programmatically based on a condition, you can omit the `displayValue` parameter if you don't need to set it explicitly.

 

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

View solution in original post

9 REPLIES 9

Hello @Khaled RAJHI ,

 

Certainly! In the `g_form.setValue` function in ServiceNow, the third parameter (`displayValue`) is optional. The basic syntax is as follows:

 

g_form.setValue(fieldName: string, value: string, displayValue: string);

However, in many cases, you might not need to explicitly provide the `displayValue`. The `displayValue` parameter is typically used when you want to set both the value (sys_id) and the corresponding human-readable label at the same time.

 

In your specific scenario of setting the assignment group based on a condition, you are only interested in setting the sys_id of the assignment group based on the value associated with the condition. Therefore, you provide only the `fieldName` and `value` parameters.

 

If you had the display value and wanted to set both the sys_id and the display value simultaneously, you would include the third parameter:

g_form.setValue('assignment_group', 'sys_id_of_hardware_group', 'Hardware');

In your case, since you are setting the value programmatically based on a condition, you can omit the `displayValue` parameter if you don't need to set it explicitly.

 

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

Hi Anit,

another question please, even when I changed the short description and Clic Save the assignment group does not change and it remains 'Hardware' ?

Hello @Khaled RAJHI ,

 

Like you want to clear the existing value of assignment group i.e. hardware when short description is changed?

If yes then you can keep only on UI policy as onChange on the short description variable so whenever the short description matches it will set the assignment group as hardware and for all the other values it will clear the assignment group value of any existing value is present.

onChange Client Script:

// Client script for the 'short_description' field
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '')
        return;

    var shortDescriptionField = g_form.getControl('short_description');
    var assignmentGroupField = g_form.getControl('assignment_group');

    var short_description = shortDescriptionField.value;
    var shdes = short_description.toUpperCase();

    if (shdes.includes("DISK")) {
        // Set the assignment group if 'DISK' is found in the short description
        assignmentGroupField.value = 'sys_id_of_hardware_group';
    } else {
        // Clear the assignment group if 'DISK' is not found
        assignmentGroupField.value = '';
    }
}

onChange(); // Call the function on form load as well

 

Thank you but I want that the assignment group change when I clic Save or Submit, I mean that I want to continue using OnSubmit Function

according to your script, the assignment group is null when short description does not contain "disk", but I don't that the assignment group to be null, I want that it would be introcuded manually if the short description does not contain "disk"