g_form.getControl not working in service portal - There is a JavaScript error in your browser console
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2020 11:12 AM
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!
- Labels:
-
Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2020 12:01 PM
Hello Krishna Priya,
The u_timezone field is a reference field if yes, refer below solution
When you are trying to change the background color of a reference field, the following does not work:
g_form.getControl('u_timezone ').style.backgroundColor = 'red'
The above will work for other field types, except not for reference fields.
The getControl() method is not appropriate for reference fields.
Solution:-
The right method for reference fields is getDisplayBox(), like so:
g_form.getDisplayBox('u_timezone ').style.backgroundColor = 'red'
For more details, you can refer below links:
https://hi.service-now.com/kb_view.do?sysparm_article=KB0726412
https://www.servicenowguru.com/system-ui/field-styles-service-catalog-variables/
I hope this will help you.
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response useful to you and help others to find information faster.
Thanks,
Tejas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2020 10:21 AM
Hi Tejas,
getDisplayBox methos is not working. Can you check and help me on this?
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2020 12:57 PM
Hi,
Did you try with the below syntax?
Try with the below syntax code and let me know the result.
g_form.getDisplayBox('u_field_name'); //returns html element for input
g_form.getDisplayBox('u_field_name').value; //returns html element attribute value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2020 10:22 AM
For String field what kind of method we can use?