g_form.getControl not working in service portal - There is a JavaScript error in your browser console

Krishna Priya
Tera Contributor

Hi All,

I have used the below client script to make field style background color. 

 g_form.getControl('u_timezone').style.backgroundColor = 'red';  but this line is not working in service portal and throwing an error. There is a JavaScript error in your browser console

Even i have tried with g_form.hasField but its not working.

Can any one help on this.

------------------------------------------------------------------------------------------------------------------

function onLoad() {
//Type appropriate comment here, and begin script below
var timeZone = g_form.getReference('location',getLocTimeZone);
function getLocTimeZone(loc){
var loc_timeZone= loc.time_zone;
//if(loc_timeZone != ''){
var ga = new GlideAjax('TimeZoneUtil');
ga.addParam('sysparm_name', 'getTimeZOne');
ga.addParam('sysparm_user_name', loc_timeZone);
ga.getXML(HelloWorldParse);

function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
// alert(answer);
if(answer){
var dateArr = answer.split(' ');
var time = dateArr[1];
var hours = time.split(':');
var hour = hours[0];

if((hour>=20) || (hour<=9)){
g_form.getControl('u_timezone').style.width = '50px'; //Set the width of a field
g_form.getControl('u_timezone').style.backgroundColor = 'red'; //Set the background color of a field
g_form.getControl('u_timezone').style.color = 'black'; //Set the text color of a field
g_form.getControl('u_timezone').style.fontStyle = 'italic'; //Set the text font to italic
g_form.getControl('u_timezone').style.fontWeight = 'bold';
g_form.setValue('u_timezone',answer);
}
if((hour>=9)&&(hour<=10)|| ((hour>=19)&&(hour<=20))){
g_form.getControl('u_timezone').style.width = '50px'; //Set the width of a field
g_form.getControl('u_timezone').style.backgroundColor = 'Yellow'; //Set the background color of a field
g_form.getControl('u_timezone').style.color = 'black'; //Set the text color of a field
g_form.getControl('u_timezone').style.fontStyle = 'italic'; //Set the text font to italic
g_form.getControl('u_timezone').style.fontWeight = 'bold';
g_form.setValue('u_timezone',answer);
}
}
else{
g_form.setDisplay('u_timezone',false);
}
}
}
}

------------------------------------------------------------------------------------------------------------------ 

Thanks!

8 REPLIES 8

Hi Krishna Priya,

 

Did you check my second link?

Reference fields: A special case

The ‘control’ element is actually hidden for all reference fields so using the script on a reference field will set a style, but that style will be set on an element you’ll never see.

For reference fields, you need to get the ‘sys_display’ input element. Here’s a sample script that shows how you could do this…

 

You can try below simplified code, this will help.

Syntax:

var myVar = $('sys_display.' + g_form.getControl('u_field_name').id);
myVar.style.width = '200px';

Putting it all together:

var myVar = $('sys_display.' + g_form.getControl('u_timezone').id); 

myVar.style.width = '50px';
myVar.style.backgroundColor = 'red';
myVar.style.color = 'black';
myVar.style.fontStyle = 'italic';
myVar.style.fontWeight = 'bold';

or

You can try like this:

g_form.getControl('sys_display.table_name.u_field_name').style.backgroundColor = 'red';

 
 
 
Mark the answer as Correct/Helpful based on its impact.
 
Thanks and Regards,
Tejas

Any updates?

Is this answered?

If my answer helped you, kindly mark it as Correct & 👍Helpful so that others with the same question in the future can find it quickly & close the thread and that it gets removed from the Unanswered list.

 

Regards,

Tejas

Hey Tejas,

 

Background color is not working in portal.

 

Thanks,

krishna Priya.

sachin_namjoshi
Kilo Patron
Kilo Patron

g_form.getControl is not supported in service portal.

https://docs.servicenow.com/bundle/kingston-servicenow-platform/page/build/service-portal/concept/un...

Instead, you need to use g_form.hasField() method

 

Regards,

Sachin