UI Script

Ken83
Mega Guru

Hello Community,

I'm trying to utilize a UI Script to generate a popup for end users. As I understand it, a UI Script utilizes Client Side scripting. However, I cannot seem to get it to return GlideRecord results. I've done something simple like...

var id = g_user.userID;

alert("User is : " + id);

This will not generate an alert. So I thought maybe UI Scripts utilize Server Side scripting so I changed that script to...

var id = gs.getUserID();

alert("User is : " + id);

So at this point i'm really confused as to why I can't get any results. So I resorted to GlideAjax and a Script Include to test and it turns out the GlideAjax actually does give a popup, but it's always "undefined".

var ga = new GlideAjax('KnownIssueAjaxScript');

ga.addParam('name', 'userPreference');

ga.getXML(ScriptParse);

function ScriptParse(response){

        var answer = response.responseXML.documentElement.getAttribute("answer");

        alert(answer);

}

The above script is the GlideAjax I used in the UI Script. Below is the Script Include I called to...

var KnownIssueAjaxScript = Class.create();

KnownIssueAjaxScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {

      initialize: function() {

      },

  userPreference: function(){

  var name = "Walk this way!";

  return name;

  },

      type: 'KnownIssueAjaxScript'

});

Here is what I am trying to accomplish.

GOAL : Generate a popup message for all users. Once the user acknowledges the popup message, they shouldn't get the popup anymore.

My approach was to create a global script that I can enable which will in turn, create a GlideOverlay or GlideDialogWindow to display a popup UI Page containing a message to users. When the users click a button that will be on the popup UI Page, it will update a user preference so that the popup doesn't keep coming up for that user. (Similar to what happens with the OOB UI14 pop up message.)

Any ideas or suggestions?

1 ACCEPTED SOLUTION

Hi Kenneth,



On the Client Side, you can use getPreference('name of user preference') and setPreference('name or user preference', 'value of preference) to handle user preferences.   So for example:



addLateLoadEvent(function() {


        // getPreference returns an empty string if there is no property yet.   Once the property is set, the if statement will be false


      if (getPreference('showUserAlert') == '') {


              var id = g_user.userID;


              alert("User is : " + id);


              setPreference('showUserAlert', 'true');


      }


});


View solution in original post

11 REPLIES 11

Hi Travis,



Have you checked this in Fuji.. This is returning undefined.


Any alternate solution.



Thanks


Hi Ashish,



I just checked the solution I offered in Fuji and it worked for me.   Can you share some details / screenshots of your script and the result?   I'd be happy to help troubleshoot.



Kind regards,



Travis


user.PNGusr.png


On loading a page its giving undefined. Its working after the homepage loads and will navigate to any module.


Hi Ashish,



Thanks for pointing this out.   I did not catch it before.   The first thing to note is that as a global UI Script it will run on EVERY page, including the header frame and navigation frame.   There is a pretty good chance g_user doesn't exist in those frames.   Also, it appears that g_user does not exist on homepages either.   This seems consistent with the wiki requirement that g_user can only be used in client scripts.   So you could either swap g_user for a GlideAjax call that would obtain the user info or you could just check for g_user and only run the script if g_user exists.   Here is an example of the latter, also note that I swapped alert for confirm to make testing easier.   Click cancel and the user property is left alone.



addLateLoadEvent(function() {


  // getPreference returns an empty string if there is no property yet.   Once the property is set, the if statement will be false


  if (getPreference('showUserAlerta') == '') {


        if (g_user) {


            var id = g_user.userID;


            var ans = confirm("User is : " + id);



            if (ans) {


            setPreference('showUserAlerta', 'true');


            }


        }


  }


});


Hey Travis,   Thanks a lot for that info, very useful.   Hey I'm doing something very similar to this post:   Here's what I want to do:



1) Have a system property that is set to true or false.   When its set to true, show a dialog/ui page that says "The system is under maintenance" one time to users.


2) When false, don't show the UI page/dialog



Design:


1) Create a property


2) Create a UI page


3) Create an script include to return the value of the system property to the ui script


4) Create a UI script that does this:


Looks to see if the property is true or false, false do nothing, true proceed


Set a preference for the user if they have seen the dialog.


If they have seen the dialog, do nothing.   If they have not , show the dialog.



So this code works, but it seems really glitchy/buggy.   Like I update the property and it keeps firing anyway.   Or i deleted the preference, then it doesn't generate a new one.   Does this code look good?   Any suggestions for improvement?



addLoadEvent(function() {  


      var ga = new GlideAjax('maintUtils');


      ga.addParam('sysparm_name','showMaintenance');


      ga.getXMLWait();


      var x = (ga.getAnswer());


     


      if (x!='true'){ // if the property is off, do nothing


              return;


      }


      if (g_user.getUserName() == "guest") {


              return;


      }


      //Ensure that we call the dialog for the correct frame


      if(window.frameElement){


              if(window.frameElement.id == 'gsft_main'){


                      checkTerms();


              }


      }


      else if(!window.frameElement){


              if(!$('gsft_main')){


                      checkTerms();


              }}


});



function checkTerms() {


      addLateLoadEvent(function() {  


              if (getPreference('have.seen.maintenance.notif') == '') {      


                      setPreference('have.seen.maintenance.notif', 'true');


                      var Dialog = new GlideDialogWindow('MaintenanceDialog');


                      Dialog.setSize(580,550);


                      Dialog.setTitle('Maintenance Page');


                      Dialog.render();


              }


              });


}