TUESDAY, FEBRUARY 07, 2012

Dynamic browser window title script

UI

scripts in Service-now are one of those things that can be very useful but aren’t very well known.  They allow you to do a lot of client-side javascript manipulation of places in the form that you might not otherwise be able to get to.  One good example of this is setting the browser window title dynamically based off of the content of records being displayed.



By default, Service-now displays the same window title information all of the time no matter what you’re looking at within the application.  This isn’t normally an issue, but it can be frustrating if, like me, you work with multiple browser tabs open that all have something to do with Service-now.

You might just prefer to look at a different title every now and then :) .  In my client’s case, they wanted to change the window title dynamically when they opened up a task ticket. They wanted to show the Number and Short description of the task being displayed. Otherwise, they wanted to show the default title. We accomplished this with the addition of a simple global UI script as outlined in the steps below…

1Navigate to ‘System UI -> UI scripts’ and create a new UI script

2Name the script ‘DynamicWindowTitle’ and check the ‘Global’ checkbox

3Use the following script in your UI script

addLoadEvent(setTitle);
function setTitle(){
   try{
      var num = g_form.getValue('number');
      var sd = g_form.getValue('short_description');
      var nme = g_form.getValue('name');
   }catch(e){
   //alert('Error getting title info.');
}
   if(num && sd){
      top.document.title = num + ' - ' + sd;
   }
   else if(nme){
      top.document.title = nme;
   }
   else{
      top.document.title='Service-now.com - IT Service Management Suite';
   }
}

Comments

Posted On
Apr 08, 2010
Posted By
Valor

Hey Mark–nice work!

I made some changes to be even more developer friendly–what I really care about is the table name and view (I have a BR, a CS and Incident windows open for testing..).

addLoadEvent(setTitle);
 
function setTitle(){
 
   var prefix = 'VP-';
 
   var url = location.pathname;
 
   url = url.substring(1,url.length-3);
 
   var base = prefix + url;
 

 
   try{
 
      var sd = g_form.getValue('short_description');
 
      var num = g_form.getValue('number');
 
      var nme = g_form.getValue('name');
 
   } catch(e) {
 
   }
 

 
   if(sd || num || nme){
 
      var add = '';
 
      sd = (sd) ? sd : (nme) ? nme : '';
 
      add = (sd != '' && num) ? num + ' - ' + sd : (sd != '') ? sd : num;
 
      top.document.title = base + ' | ' + add;
 
   } else{
 
      top.document.title = base;
 
   }
 
}
Posted On
Apr 08, 2010
Posted By
Mark Stanger

I like it! I wonder if maybe a combination of the two would work well for all users. Something more simple for itil and ess users and something like yours for admins maybe.

Posted On
Aug 03, 2010
Posted By
Joe

Great stuff!

I agree with your last post Mark, a combo of the 2 would be good.

Is it possible to have the title show the actual ‘Title’ rather than the name of the page, i.e. where Valor’s script shows ‘VP-kb_home’ if you select Knowledge, can the tab be titled ‘Knowledge’ instead of ‘kb_home’?

Thanks,

Joe

Posted On
Aug 03, 2010
Posted By
Mark Stanger

It might be possible, but it certainly isn’t simple given the current system setup. These scripts rely on whatever information they can gather from the form itself. The big challenge is that the information that you need to get the display value of a table or a form isn’t in the same place every time or isn’t accessible directly by ID. I’ve actually looked at this a couple of times since Valor posted his solution and have yet to see a consistent way to show the friendly name instead of the system name.

Posted On
Aug 03, 2010
Posted By
Joe

Thanks for the quick reply Mark :)

Great site by the way, only came across it today and with us being having just gone live with Service-now a couple of months ago the info you’ve got on here looks like it will be very useful.

Posted On
Aug 20, 2010
Posted By
valor

Joe, that’s one of the reasons why I’m parsing out the URL instead of trying to get the page element with the “friendly” name–it’s not in a standard or easy-to-grab place.

It’s possible (always in the same place for lists & forms), but easier the way I did it, and it just so happens that that’s what I care about anyway.

Posted On
Mar 18, 2011
Posted By
Brian Broadhurst

Mark, do UI Scripts have access to g_scratchpad? I have an idea to set some values in a display business rule, which would not be accessible using g_form or g_user. How about session client data?

Brian

Posted On
Mar 21, 2011
Posted By
Mark Stanger

I’d have to test to be positive, but I would imagine they would as long as they were running on a traditional form or in a catalog client script (just like g_form or g_user). Probably not available in other places though.

Posted On
Mar 21, 2011
Posted By
valor

Like Mark said, you should have access to g_scratchpad (haven’t tested). Note that you have to be careful about breaking IE when you try to use g_form and the like. You do have access to the g_ objects, though.

Also keep in mind that UI Scripts run on EVERY page (they’re loaded with the navigation), so use very sparingly!

Posted On
Jun 18, 2011
Posted By
Bill Collins

Mark, Do you think the Script Action “Set Theme” could be modified to change title based on user company?

Posted On
Jun 18, 2011
Posted By
Mark Stanger

No because the script actions run server-side and this modification needs to be done client-side. You could modify the UI script above to perform an asynchronous call to the server to get the company data but that probably wouldn’t work very well. I’m not sure of a good way to do what you’re asking for off the top of my head.

Posted On
Jun 19, 2011
Posted By
Bill Collins

I can display company sysID as follows, but still lack the conversion to name.

UI Script

addLoadEvent(setTitle);
 
function setTitle(){
 
   try{
 
      var comp = g_user.getClientData('test');
 
   }catch(e){
 
   //alert('Error getting title info.');
 
}
 
   if(comp){
 
      top.document.title = comp + ' - IT Service Management Suite';
 
   }
 
   else{
 
      top.document.title='Service-now.com - IT Service Management Suite';
 
   }
 
}

Script Action

myCompany();
 
 
 
function myCompany() {
 
   var cmp = gs.getUser().getCompanyID();
 
   session.putClientData("test", cmp);
 
}

Tried: var cmp = gs.getUser().getCompanyID().getDisplay();

Posted On
Jun 20, 2011
Posted By
Mark Stanger

‘getCompanyID’ just gets you the sys_id. ‘getDisplay’ doesn’t exist…I think you meant ‘getDisplayValue’. Also, ‘getDisplayValue’ won’t work off of a sys_id string, you have to reference it from a GlideRecord. You’ll also want to be aware that this information will require you to log in again each time as you are testing since it all gets cached as the session is created. I’m also not sure that the client data is going to be available throughout the product, though it should be available on standard forms. I think this is what you are looking for…
[code]
var cmp = gs.getUser().getCompanyRecord().getDisplayValue();
[/code]

Leave a Reply


Notify me of followup comments via e-mail. You can also subscribe without commenting.

Latest Comments

  • Mark Stanger: This linkage all happens for you if you use the task survey plugin. You can look on the wiki for more...
  • Vineeth: I want a way in which if a survey is filled in by the user the response are stored in the survey response...
  • Mark Stanger: This functionality doesn’t connect to an FTP server. See this line in the post above…...
  • Mark Stanger: The report page is back-end XML so there’s no way to directly manipulate the behavior of that...