Document ready UI Script

Daryll Conway
Giga Guru

I'm trying to use a document ready function in a UI Script to capture the current user...

var $j = jQuery.noConflict(true);

              $j(document).ready(function () {

                      user = gs.getUserID();

                      alert(user);

              });

This doesn't work.

Has anyone used a document ready function in a UI script or an equivalent that has worked.

Failing that can anyone tell me how I can get the current user in a UI Script?

1 ACCEPTED SOLUTION

UI script sometimes doesn't have access g_user object .. Do a glideajax and return gs.getUserID from the script include


View solution in original post

9 REPLIES 9

UI script sometimes doesn't have access g_user object .. Do a glideajax and return gs.getUserID from the script include


GlideAjax did the trick thank you Kalai.


Daryll Conway
Giga Guru

For full disclosure this is my UI Script in full...



addLoadEvent(function () {


      try{


              $('nav_header_stripe_decorations').style.width ="800px";


           


              //New Incident Icon


              var incidentIcon = "<a target='_blank' href='incident.do?' class='btn btn-default homepage-button' id='sneIncidentIcon' style='margin-left: 5px; margin-right: 10px; margin-top: 0px; margin-bottom: 0px; padding: 0px 5px 0px 0px; color:white;border-style: solid; border-width: 1px; vertical-align: middle;'><span class='icon-book pull-left' style='margin-right: 4px;'></span>New Incident</a>"; //);


              $('gsft_logout').insert({before:incidentIcon});


           


              //New Change Icon


              var changeIcon = "<a target='gsft_main' href='change_request.do?' class='btn btn-default homepage-button' id='sneChangeIcon' style='margin-left: 5px; margin-right: 5px; margin-top: 0px; margin-bottom: 0px; padding: 0px 5px 0px 0px; color:white;border-color: white; border-style: solid; border-width: 1px; vertical-align: middle;'><span class='icon-collaboration pull-left' style='margin-right: 4px;'></span>New Change</a>"; //);


              $('sneIncidentIcon').insert({before:changeIcon});


           


              //New Ticket Icon


              var ticketIcon = "<a target='gsft_main' href='ticket.do?' class='btn btn-default homepage-button' id='sneTicketIcon' style='margin-left: 5px; margin-right: 5px; margin-top: 0px; margin-bottom: 0px; padding: 0px 5px 0px 0px; color:white;border-style: solid; border-width: 1px; vertical-align: middle;'><span class='icon-cart pull-left' style='margin-right: 4px;'></span>New Ticket</a>"; //);


              $('sneChangeIcon').insert({before:ticketIcon});


           


              //New Problem Icon


              var problemIcon = "<a target='gsft_main' href='problem.do?' class='btn btn-default homepage-button' id='sneProblemIcon' style='margin-left: 5px; margin-right: 5px; margin-top: 0px; margin-bottom: 0px; padding: 0px 5px 0px 0px; color:white;border-style: solid; border-width: 1px; vertical-align: middle;'><span class='icon-cart pull-left' style='margin-right: 4px;'></span>New Problem</a>"; //);


              $('sneTicketIcon').insert({before:problemIcon});


           


              //Move Update Set Picker


              $('update_set_picker_select').className = '';


              if($('update_set_picker').select('li')[0]){


                      $('update_set_picker').select('li')[0].className = '';


                      $('update_set_picker').select('li')[0].style.paddingRight="10px";


                      $('update_set_picker').select('li')[0].style.listStyleType ="none";


                      $('update_set_picker').style.height ="35px";


                      $('update_set_drill').style.color ="#FFF";


                      $('update_set_drill').style.marginTop = "2px";


                      $('update_set_picker').select('li')[0].select('a')[1].style.color ="#FFF";


                      $('update_set_picker').select('li')[0].select('a')[1].style.marginTop = "2px";


                      $('update_set_picker').select('li')[0].select('a')[2].style.color ="#FFF";


                      $('update_set_picker').select('li')[0].select('a')[2].style.marginTop = "2px";


                      $('update_set_picker_select').style.color ="#000";


              }


              if($('update_set_picker').select('legend')[0]){


                      $('update_set_picker').select('legend')[0].remove();


              }


              $('nav_header_stripe_decorations').insert({


                      top: $('update_set_picker')


              });


              $('update_set_picker').id = 'update_set_picker_new';


              $('update_set_picker_select_title').id = 'update_set_picker_select_title_new';


           


      }catch(e){


              //alert(e);


      }


});



function postLoad(){


      try{


              var user = '9ddeaa32a53f118041c7f3bb53884cbb';


              user = g_user.userID;


              alert('user = ' + user);


              document.getElementById("sneIncidentIcon").href="/nav_to.do?uri=incident.do?sys_id=-1%26sysparm_query=caller_id="+user;


      }


      catch(err){


              alert(err);


      }


}



window.onLoad(postLoad());


CustomEvent.observe('user.login', function(user) {


  alert(user.getFullName());


});



I am able to get the user name at login. Not sure though whether it will be useful in your case.


JoeLighthall
Giga Contributor

We recently upgraded to Fuji and were accessing g_user in a global ui script. However this no longer worked with the upgrade. With Chrome debugging I noticed there were two g_user objects. One in the top frame and another in the main frame. With the upgrade the one accessed in the top frame was partially populated. It did not have fields set like userID. So referencing g_user from a global ui script always selects the g_user object in the top frame. Anyways I added this code to get the g_user object in the main frame.



var mainFrame = document.getElementById('gsft_main');


if (mainFrame) {


  my_guser = mainFrame.contentWindow.g_user;


}



I also tried contacting ServiceNow support and was told g_user is to be used only with client script. Which I understand however I feel this object should not exist as a different object in the top frame.