How can i Modify the Label of Form Fields With Client Scripts

nysbigdave
Kilo Expert

Hey Guys I am attempting to rewrite the label if a user has itil role..
however I believe the syntax maybe off.. can you help?


the field name is called u_comments

 

 

function onLoad() {
  if (!g_user.hasRole("itil"))
return;

var field = 'u_comments';
var fieldFormName = g_form.getTableName() + "." + field;
var fieldLabelName = "label." + fieldFormName;
var ctrl = $(fieldLabelName);
  if (!ctrl)
  return;
  var label = ctrl.select('label')[0];
  if (!label)
  return;
  var s = label.innerHTML;
  label.innerHTML = s.substring(0, s.length — 1) + ' (' + getMessage('Customer visible') + '):';
  //label.setStyle({fontWeight:"bold"});
}

1 ACCEPTED SOLUTION

nysbigdave
Kilo Expert

Mark Stanger (Crossfuze) Resolved this for me



function onLoad() {
      if(!g_user.hasRole('itil'))
              return;



      //Change the description label to 'My New Label' with bold red text
      changeFieldLabel('u_comments', 'My New Label', 'red', 'bold');
}



function changeFieldLabel(field, label, color, weight){
      try{
            var labelElement = $('label.' +   g_form.getControl(field).id);
            labelElement.select('label').each(function(elmt) {
                  elmt.innerHTML = label + ':';
            });
            if(color)
                  labelElement.style.color = color;
            if(weight)
            labelElement.style.fontWeight = weight;
      }catch(e){};
}


View solution in original post

11 REPLIES 11

casey_stinnett
Giga Expert

Here is a wonderful post on ServiceNow Guru that helps with this very question. » Modifying the Label of Form Fields With Client Scripts



Does that point you in the right direction?


Yeah that's how I reached this point, im not sure why its not populating ....


nysbigdave
Kilo Expert

Mark Stanger (Crossfuze) Resolved this for me



function onLoad() {
      if(!g_user.hasRole('itil'))
              return;



      //Change the description label to 'My New Label' with bold red text
      changeFieldLabel('u_comments', 'My New Label', 'red', 'bold');
}



function changeFieldLabel(field, label, color, weight){
      try{
            var labelElement = $('label.' +   g_form.getControl(field).id);
            labelElement.select('label').each(function(elmt) {
                  elmt.innerHTML = label + ':';
            });
            if(color)
                  labelElement.style.color = color;
            if(weight)
            labelElement.style.fontWeight = weight;
      }catch(e){};
}


Hi nysbigdave/ mark.stanger



I am trying to change field label color to red in incident form by onload client script .


But as soon as I change the choice option of that field tha field label color changes red to black .


Can you guide me why this is happening and what to do solve this.


Please refer below screenshots.



Before changing choice value of any field .


find_real_file.png



After changing choice value of system field , severity . Field labels are turning to black color.



find_real_file.png



Please help me out .ctomasi  



Thanks


Aastha