The CreatorCon Call for Content is officially open! Get started here.

Handling User Type Detection and u_datecheck Field Update for New Users in ServiceNow

anireddysai
Tera Contributor

In ServiceNow, there are scenarios where we need to distinguish between new users and old users based on their login history. Specifically, we want to update a custom field (u_datecheck) with the last_login_time for new users when they log in for the first time, but we should not update this field for old users.

The goal is to implement a condition where:

Note: User were created yesterday, but they have logged in today ,  the u_datecheck field should be updated because they are considered new users.

  1. New users will have their u_datecheck updated with their first login time (last_login_time).
  2. Old users who have logged in previously should not have their u_datecheck updated, even if the field is empty.

    Problem Statement:

    We have a custom field u_datecheck which should:

    • Be populated with the last_login_time when a new user logs in.
    • Not be updated for old users who have already logged in at least once, even if the u_datecheck field is empty.

    Challenges:

    • For old users, the u_datecheck field might be empty due to various reasons (e.g., field was reset or cleared). We need to ensure that they are not mistakenly treated as new users.
    • For new users, their u_datecheck should only be updated during their first login.
1 ACCEPTED SOLUTION

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @anireddysai 

 

Greetings!!

 

How do you identify whether a user is old or new? Let's say I got my username 1 week back and was on Levae and logged in after 8 days in that case am I new or old? What is your business need here?

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@anireddysai 

just based on that field you cannot determine if user is new or old

what's your basic business requirement here?

Try this

  1. Configure the Business Rule:

    • Name: Update date check for New Users
    • Table: sys_user
    • When to run: Before
    • Insert: True
    • Update: True
    • Filter Conditions: last_login_time changes
    • Script as this
(function executeRule(current, previous /*null when async*/) {
    // Check if the user is logging in for the first time
    if (!previous.last_login_time) {
        // User is logging in for the first time
        current.u_datecheck = current.last_login_time;
    } else {
        // User has logged in before
        // Check if u_datecheck is empty
        if (!current.u_datecheck) {
            // Do not update u_datecheck for old users
            current.u_datecheck = previous.u_datecheck;
        }
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar 

Note1: User were created yesterday, but they have logged in today ,  the u_datecheck field should be updated once because they are considered new users.
Note 2 : 
New users will have their u_datecheck updated with their first login time (last_login_time). Only once






Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @anireddysai 

 

Greetings!!

 

How do you identify whether a user is old or new? Let's say I got my username 1 week back and was on Levae and logged in after 8 days in that case am I new or old? What is your business need here?

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Hi @Dr Atul G- LNG 

Yea we can find it out by different approaches in servicenow