Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How To tracking user login Service Portal?

thoang93
Tera Expert

I have a requirement to tracking last user login included:
- User name
- IP Address

- Date/Time login

How can I storage this information and show this information in Service Portal page?

1 ACCEPTED SOLUTION

Hi Atul,

Thank you for your reply. I got the solution for me. Below is my Solution

Create a Widget and add it to the Service Portal

thoang93_0-1705932609696.png

Here is my script in the widget:
HTML

<div class="row">
  <div ng-class="" class="col-sm-12 col-xs-12">
    <div class="panel panel-default b">
      <div class="panel-heading">
        <h2 class="panel-title">
          <i class="fa fa-info-circle m-r-sm"></i>User Tracker
        </h2>
      </div>
      <div class="body padder-xs">
        <div class="list-group">
            <div class="list-group-item">
              <div>
                User Name: {{data.userName}}
              </div>
              <div>
                Display Name: {{data.name}}
              </div>
              <div>
                IP Address: {{data.ipAddress}}
              </div>
              <div>
                Last Login: {{data.process_on}}
              </div>
            </div>
          </div>
        </div>
      </div>
  </div>
</div>

CSS ( I'm not sure which one apply because this widget I clone from Hello World widget just reuse)

.list-group-item {
  border: none;
  padding: 5px 15px;

  .btn-link {
  	padding-left: 0;
    padding-right: 0;
  }
}

.user-details {
  word-wrap: break-word; // ie support
  overflow-wrap: break-word;
}

.select2-container.select2-allowclear .select2-choice .select2-chosen {
  margin-right: 60px;
}

.mia i {
  font-size: 6rem;
}

.mia {
  color: #808080;
}

.popover {
  z-index: 1049;
}

.user-profile-container {
  width: 100%;
  max-width: 755px;
  margin: 0 auto;
  padding: 0 8px;
}
.input-switch input[type=checkbox]:checked ~ .switch {
	background-color: $brand-primary;
}

// accessible accessibility toggle
@media screen and (-ms-high-contrast: active) {
  .input-switch input.ng-not-empty+label.switch[for="accessibility-enabled"] {
    background-color: highlight;
    border: none;

    &:before {
      background-color: highlightText;
    }
  }

  .input-switch input.ng-empty+label[for="accessibility-enabled"] {
    background-color: window;
    border: 1px solid windowText;

    &:before {
      background-color: windowText;
    }
  }
}

Server-script:

(function() {
	data.userName = gs.getUserName();
	data.userID = gs.getUserID();
	data.name = gs.getUserDisplayName();
	
	// Query current login User
	var user = new GlideRecord('sys_user');
	user.get('sys_id', data.userID); // replace USER_SYS_ID with the actual sys_id of the user

	// Query login Event
	var event = new GlideRecord('sysevent');
	event.orderByDesc('sys_created_on');
	event.addQuery('user_id',gs.getUserID());
	event.addQuery('name', 'login'); // replace 'your_event_name' with the actual event name
	event.query();
	if (event.next()) {
		data.ipAddress = event.getValue('parm2'); // replace 'field_name' with the actual field name
		data.process_on = event.getValue('process_on');
		var login_history = new GlideRecord('u_login_history');
		login_history.addQuery('u_user_name',data.userName);
		login_history.query();
		if(login_history.next()){
			login_history.u_current_login = data.process_on;
			login_history.u_ip_address = data.ipAddress;
			login_history.u_user_name = data.userName;
			login_history.u_last_login = login_history.u_current_login;
			login_history.u_user_id = data.userID;
			login_history.update();
		}
		else{
			var gr = new GlideRecord('u_login_history');
			gr.initialize();
			gr.u_user_name = data.userName;
			gr.u_ip_address = data.ipAddress;
			gr.u_last_login = data.process_on;
			gr.insert();
		}
	}

I created a Login history table to save event login because sysevent table will remove login events after 7 days

thoang93_1-1705933251436.png

 

 

View solution in original post

3 REPLIES 3

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @thoang93 

 

https://www.servicenow.com/community/platform-analytics-forum/service-portal-visitors/m-p/1206599

 

https://www.servicenow.com/community/developer-forum/tracking-user-login-information/m-p/1496885

 

https://www.servicenow.com/community/platform-analytics-forum/number-of-users-who-have-logged-in-to-...

*************************************************************************************************************
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 Atul,

Thank you for your reply. I got the solution for me. Below is my Solution

Create a Widget and add it to the Service Portal

thoang93_0-1705932609696.png

Here is my script in the widget:
HTML

<div class="row">
  <div ng-class="" class="col-sm-12 col-xs-12">
    <div class="panel panel-default b">
      <div class="panel-heading">
        <h2 class="panel-title">
          <i class="fa fa-info-circle m-r-sm"></i>User Tracker
        </h2>
      </div>
      <div class="body padder-xs">
        <div class="list-group">
            <div class="list-group-item">
              <div>
                User Name: {{data.userName}}
              </div>
              <div>
                Display Name: {{data.name}}
              </div>
              <div>
                IP Address: {{data.ipAddress}}
              </div>
              <div>
                Last Login: {{data.process_on}}
              </div>
            </div>
          </div>
        </div>
      </div>
  </div>
</div>

CSS ( I'm not sure which one apply because this widget I clone from Hello World widget just reuse)

.list-group-item {
  border: none;
  padding: 5px 15px;

  .btn-link {
  	padding-left: 0;
    padding-right: 0;
  }
}

.user-details {
  word-wrap: break-word; // ie support
  overflow-wrap: break-word;
}

.select2-container.select2-allowclear .select2-choice .select2-chosen {
  margin-right: 60px;
}

.mia i {
  font-size: 6rem;
}

.mia {
  color: #808080;
}

.popover {
  z-index: 1049;
}

.user-profile-container {
  width: 100%;
  max-width: 755px;
  margin: 0 auto;
  padding: 0 8px;
}
.input-switch input[type=checkbox]:checked ~ .switch {
	background-color: $brand-primary;
}

// accessible accessibility toggle
@media screen and (-ms-high-contrast: active) {
  .input-switch input.ng-not-empty+label.switch[for="accessibility-enabled"] {
    background-color: highlight;
    border: none;

    &:before {
      background-color: highlightText;
    }
  }

  .input-switch input.ng-empty+label[for="accessibility-enabled"] {
    background-color: window;
    border: 1px solid windowText;

    &:before {
      background-color: windowText;
    }
  }
}

Server-script:

(function() {
	data.userName = gs.getUserName();
	data.userID = gs.getUserID();
	data.name = gs.getUserDisplayName();
	
	// Query current login User
	var user = new GlideRecord('sys_user');
	user.get('sys_id', data.userID); // replace USER_SYS_ID with the actual sys_id of the user

	// Query login Event
	var event = new GlideRecord('sysevent');
	event.orderByDesc('sys_created_on');
	event.addQuery('user_id',gs.getUserID());
	event.addQuery('name', 'login'); // replace 'your_event_name' with the actual event name
	event.query();
	if (event.next()) {
		data.ipAddress = event.getValue('parm2'); // replace 'field_name' with the actual field name
		data.process_on = event.getValue('process_on');
		var login_history = new GlideRecord('u_login_history');
		login_history.addQuery('u_user_name',data.userName);
		login_history.query();
		if(login_history.next()){
			login_history.u_current_login = data.process_on;
			login_history.u_ip_address = data.ipAddress;
			login_history.u_user_name = data.userName;
			login_history.u_last_login = login_history.u_current_login;
			login_history.u_user_id = data.userID;
			login_history.update();
		}
		else{
			var gr = new GlideRecord('u_login_history');
			gr.initialize();
			gr.u_user_name = data.userName;
			gr.u_ip_address = data.ipAddress;
			gr.u_last_login = data.process_on;
			gr.insert();
		}
	}

I created a Login history table to save event login because sysevent table will remove login events after 7 days

thoang93_1-1705933251436.png

 

 

Ademir Amaral1
Kilo Sage

The last login you can get from the last login in sys_user, and the IP information can cross with this KB https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0564981