Run script when user logs in

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

I'm thinking of doing a few things when a user logs in.

So I'm looking into ways of how to trigger this. I found this Help the Helpdesk - run when logging into ServiceNow

which seems pretty straight forwards and isn't many line of code to get it to work.

So I created a global ui script to test, but I cant get it to work. can anyone explain why or have any other idea how to make something run when user logs on?

This is my code, pretty much copied from the above thread.

CustomEvent.observe('user.login', test);

function test() {

alert("CAN YOU FEEL IT!");

}

And nada happens.

1 ACCEPTED SOLUTION

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

I found a way to do it.



When this inside a global ui script I get the "alert".



addLoadEvent( function() {


    if(window.frameElement){


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


                alert("Working?");


          }


    }


})


View solution in original post

10 REPLIES 10

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

I found a way to do it.



When this inside a global ui script I get the "alert".



addLoadEvent( function() {


    if(window.frameElement){


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


                alert("Working?");


          }


    }


})


This is handy


Just be careful and use more conditions and this: if(window.frameElement.id == 'gsft_main'){


otherwise it will hit like everywhere and even in some cms pages 😃



I'll throw in here my idea when Im done with it 😃


Hi Goran - I'm trying to do something similar, that is, popup a message when a non-admin user logs into a non-production instance.   It has to be modal, but it just needs to be very simple, exactly like an alert.   I came across your post, but I need to know what conditions to use so this will only popup on login.   Do you know what those might be?



Thanks,


-Andy


Hi Andy,


This is a tricky one. There is thou a None documented feature called "broadcast". Take a look at the table "broadcast message" and it pretty self explaining. But it's undocumented, so it's pretty much unknown territory and it might go away as well. Otherwise I guess you need to do some check like if the event for login just came in for the user and then show the modal. And perhaps use user preference to make that a user has seen it



You can also take a look at the overview (System UI->Overview Help) which you find in the navigator if that will help you.