function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tSoudritSoudri 

Detect login time

Hello!
 
I have written an inline s-control that runs some script when the home page loads. However, I would not like it to run each time the user reloads the home page, but for instance only the first time after he logged in.
 I was wondering if there was a way for me to know the exact time the user logged in?
Or to detect the first time the page was loaded?
 
And another thing- is there any way to keep some script running even if the user logged off?
 
Thanks in advance,
Tsoudri
sfdcfoxsfdcfox
Scripts run in the browser of the user, so logging off necessarily ends the script. You'd want to run a process on a server that uses the API to do that.

As for detecting if the page has previously loaded, you could add a cookie to the page that expires at the end of the session (eg. document.cookie = 'appHasRun=1;expires=Jan-01-1990'). Before each execution, you could check to see if the cookie exists (eg. document.cookie.match(/appHasRun/)) and abort execution if the result isn't null.

~ sfdcfox ~
tSoudritSoudri

Thanks for the tip!

The cookie works great... I also tried comparing the lastLoginDate from the user object to the local date in UTC, but   a. the code was much longer and more complicated

b. there was sometimes a delay of about a minute between login and the function running

c. the code would still run every time the user logged off then logged on

When I made a cookie that expires only at the end of the day, the code runs only at the first login each day

As for the server side script- I'm trying to write an app that can be downloaded by other salesforce users with as little setup as possible, and server side scripts are out of the question. It was working fine with Apex triggers, but then I realized that only developer editions have that option- and I need it to run for regular customers as well. I'll have to settle for the login option I guess.

Thanks again!

TSoudri