Search code examples
servicenow

How to change field value font styles in ServiceNow?


Is it possible to change field value font styles on OnLoad in servicenow ? I was able to change field label styles using below code.

// Custom field label colors
    var stateLabel = g_form.getLabel('short_description');
    stateLabel.style.color= 'red';
    stateLabel.style.fontWeight='bold';

I tried to change field value font styles in similar way but no luck.

var stateValue = g_form.getValue('short_description');
    stateValue.style.color= 'red';
    stateValue.style.fontWeight='bold';  

above code does not do any good , any thoughts on getting this to work?

Thanks in advance


Solution

  • The API call you're using g_form.getValue(...) is just going to return the string value of whatever field you're asking for.

    To get access to the Element (like with getLabel) you can use g_form.getControl(...)

    Example:

    var el = g_form.getControl('short_description');
    el.style.color = 'red';
    el.style.fontWeight = 'bold';
    

    However, I'd advise that instead of doing direct DOM manipulation with client-side javascript, that you use Field Styles instead:

    Field styles allow administrators to declare individual CSS styles for a field in a list or form. The CSS can:

    • Change the color.
    • Change the font attributes (bold, italics, underline).
    • Change the padding and alignment of text.

    Field Styles allow you to specify a particular field, and apply arbitrary CSS. To take it a step further, it even allows you to specify javascript to conditionally apply the Style based on something like the state of the record.