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.

Notification for user creation

jobin1
Tera Expert

in ServiceNow whenever a user profile is created we have to send a notification to that user and it includes the user id, password (which is set at the time of creation), and the ServiceNow URL ->https:/.service-now.com
how we can achieve this?

10 REPLIES 10

Vrushali  Kolte
Mega Sage

Hello @jobin1,

 

Please find below the links which will help you to achieve your use case -

 

https://www.servicenow.com/community/now-platform-blog/create-notification-when-a-record-is-inserted...

 

https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/administer/notification...

 

If my answer solves your issue, please mark it as Accepted and Helpful based on the impact.

 

 

Yashsvi
Kilo Sage

Hi @jobin1,

To achieve sending a notification with user details upon profile creation in ServiceNow, you can follow these steps:

  1. Create an Email Notification:

    • Go to System Notification > Email > Notifications.
    • Click on New to create a new notification.
    • Configure the notification with a meaningful name and description.
  2. Define Notification Content:

    • In the notification content, you can use placeholders to dynamically insert user-specific details like User ID, Password, and the ServiceNow URL.

Dear {{recipient.name}},

Your ServiceNow account has been created successfully.

User ID: {{recipient.user_id}}
Password: {{recipient.password}}
ServiceNow URL: https://your-instance.service-now.com

Regards,
ServiceNow Team

 

3.Associate Notification with User Creation Event:

  • You need to associate this notification with the user creation event.
  • Navigate to System Definition > Business Rules.
  • Find or create a Business Rule that triggers when a new user record (sys_user) is inserted

 

var user = new GlideRecord('sys_user');
user.addQuery('sys_id', current.sys_id); // current.sys_id represents the newly created user's record
user.query();
if (user.next()) {
    // Send notification
    var email = new GlideEmailOutbound();
    email.setSubject('Your ServiceNow Account Details');
    email.setBodyFromNotification('notification_sys_id'); // Use the sys_id of your email notification
    email.addRecipient(user.email);
    email.send();
}

 

Thank you, please make helpful if you accept the solution.

currently, i am using the below BR and calling notifications via event but i am getting event logs correctly but notifications are not getting 
//br //after insert on sys_user

(function executeRule(current, previous /*null when async*/) {

    var userid=current.user_name;
    gs.log("userid1"+userid);

if (current.email) {
        // Trigger the custom event
        gs.eventQueue('user.creation.notification', current, current.user_name, '');

        // Send notification
    var email = new GlideEmailOutbound();
    email.setSubject('Your ServiceNow Account Details');
    email.setBodyFromNotification('ec3bac3983578a10b7594d226daad371'); //
    email.addRecipient(current.email);
    email.send();
    }

})(current, previous);
message html in notification

Dear {{recipient.name}},

Your ServiceNow account has been created successfully.

User ID: {{recipient.user_id}}
Password: {{recipient.password}}
ServiceNow URL: https://your-instance.service-now.com

Regards,
ServiceNow Team



Hi @jobin1,

please try below script:

 

 

(function executeRule(current, previous /*null when async*/) {
    // Log to track the flow
    gs.log('Executing Business Rule for new user creation. User Name: ' + current.user_name + ', Email: ' + current.email);

    if (current.email) {
        // Trigger the custom event
        gs.eventQueue('user.creation.notification', current, current.user_name, '');

        // Send notification
        var email = new GlideEmailOutbound();
        email.setSubject('Your ServiceNow Account Details');
        email.setBodyFromNotification('ec3bac3983578a10b7594d226daad371'); // Replace with your actual notification sys_id
        email.addRecipient(current.email);
        var sent = email.send();
        if (sent) {
            gs.log('Notification email sent successfully to ' + current.email);
        } else {
            gs.log('Failed to send notification email to ' + current.email);
        }
    } else {
        gs.log('No valid email address found for user ' + current.user_name);
    }
})(current, previous);

 

 

Ensure you replace 'ec3bac3983578a10b7594d226daad371' with the correct sys_id of your notification. Also, check your email server settings and logs to ensure there are no issues with outbound emails.

Thank you, please make helpful if you accept the solution.