register for new user from service portal

Rajkumar Bommal
Tera Contributor

How can I register a new user from Service Portal and store their password in the sys_user table so that they can log in to the portal?

I have created a custom registration widget/form in the Service Portal that collects:

1.Full Name

2.Email

3.Username

4.Password

I want the submitted data to create a new record in the sys_user table, and set the password so the user can immediately log in to the portal

As Of now I have tried using custom Widget to create a new user record to login service portal:

RajkumarBommal_0-1751610279181.pngRajkumarBommal_1-1751610354760.png

Data is getting stored in user table but login is not working

2 ACCEPTED SOLUTIONS

SANDEEP DUTTA
Tera Patron
Tera Patron

Hi @Rajkumar Bommal ,

Refer to this thread: Solved: how to include user registration feature in my ser... - ServiceNow Community

 

You can watch this video :Plan your retirement better with Axis Max Life Insurance

 

Thanks,
Sandeep Dutta

Please mark the answer correct & Helpful, if i could help you.

View solution in original post

Community Alums
Not applicable

Hi @Rajkumar Bommal ,

try with this foloowing code

html 

<div class="form-group">
  <label>First Name</label>
  <input type="text" class="form-control" ng-model="c.data.first_name" />
</div>
<div class="form-group">
  <label>Last Name</label>
  <input type="text" class="form-control" ng-model="c.data.last_name" />
</div>
<div class="form-group">
  <label>Email</label>
  <input type="email" class="form-control" ng-model="c.data.email" />
</div>
<div class="form-group">
  <label>Username</label>
  <input type="text" class="form-control" ng-model="c.data.username" />
</div>
<div class="form-group">
  <label>Password</label>
  <input type="password" class="form-control" ng-model="c.data.password" />
</div>
<button class="btn btn-primary" ng-click="c.register()">Register</button>

<div class="alert alert-success" ng-if="c.data.success">
  {{c.data.message}}
</div>
<div class="alert alert-danger" ng-if="c.data.error">
  {{c.data.message}}
</div>

client script

function($scope) {
  var c = this;
  
  c.register = function() {
    c.server.get({
      action: "register",
      first_name: c.data.first_name,
      last_name: c.data.last_name,
      email: c.data.email,
      username: c.data.username,
      password: c.data.password
    }).then(function(response) {
      c.data.success = response.data.success;
      c.data.error = response.data.error;
      c.data.message = response.data.message;
    });
  };
}

 

Server script

(function() {
  if (input && input.action === "register") {
    // Check if user already exists
    var existingUser = new GlideRecord('sys_user');
    existingUser.addQuery('user_name', input.username);
    existingUser.query();
    
    if (existingUser.next()) {
      data.success = false;
      data.error = true;
      data.message = 'Username already exists';
      return;
    }
    
    // Check if email already exists
    var existingEmail = new GlideRecord('sys_user');
    existingEmail.addQuery('email', input.email);
    existingEmail.query();
    
    if (existingEmail.next()) {
      data.success = false;
      data.error = true;
      data.message = 'Email already registered';
      return;
    }
    
    // Create new user
    var newUser = new GlideRecord('sys_user');
    newUser.initialize();
    newUser.first_name = input.first_name;
    newUser.last_name = input.last_name;
    newUser.email = input.email;
    newUser.user_name = input.username;
    newUser.user_password.setDisplayValue(input.password); // Proper password handling
    newUser.active = true;
    
    var userSysId = newUser.insert();
    
    if (userSysId) {
      data.success = true;
      data.error = false;
      data.message = 'Registration successful! You can now login.';
    } else {
      data.success = false;
      data.error = true;
      data.message = 'Registration failed. Please try again.';
    }
  }
})();

 

View solution in original post

7 REPLIES 7

SANDEEP DUTTA
Tera Patron
Tera Patron

Hi @Rajkumar Bommal ,

Refer to this thread: Solved: how to include user registration feature in my ser... - ServiceNow Community

 

You can watch this video :Plan your retirement better with Axis Max Life Insurance

 

Thanks,
Sandeep Dutta

Please mark the answer correct & Helpful, if i could help you.

Mark Manders
Mega Patron

Why not use a simple record producer? Create the User record from their (no need for a custom widget). And utilize the 'password reset' functionality to have the user generate their first password. That sounds a lot safer than having a portal widget where people can create their own user, including password, and are granted access to the instance.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

@Mark Manders 
How Can we access a record producer with out loging into servicenow protal , Is it possible

 

You should never allow users to create their own user record! CSM has a registration process for that, but why would you want users to create their own account?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark