Automatically create SMS Notification Device from Mobile Phone

dhoffman
Tera Contributor

Out of the Box, ServiceNow automatically creates an e-mail notification device called "Primary email" from the sys_user.email field.   Is there a way to do the same thing for cell phones?   I have sys_user.mobile_phone automatically pulled in through Active Directory LDAP integration, as simply the user's mobile phone number: 123-456-7890.   I'd like a way for this to automatically create an SMS device for each user instead of having to manually maintain this for the ~200 or more IT workers who need to be paged out for Severity 1 or 2 incidents and any other SMS notification.


Is this as simple as creating an LDAP onAfter transform script to map sys_user.mobile_phone --> cmn_notif_device table?


Pseudocode as follows:

 

if (this person doesn't already have a notification device corresponding to their mobile_phone) {


     var person = source.u_samaccountname.toString() + " (" + source.u_givenname.toString() + " " + source.u_sn.toString() + ")";

     cmn_notif_device.name = "Bob's Mobile Phone"; // or whatever name I wish to call it

     cmn_notif_device.type = "SMS";
     
     // (use regular expression to strip out punctuation so 123-456-7890 becomes 1234567890).
     cmn_notif_device.phone_number = sys_user.mobile_phone;

     
     cmn_notif_device.service_provider = "Verizon" / "T-Mobile" / "Sprint" / "AT&T" // (etc.)


     log.info("Creating new SMS Notification Device for " + person);

}




 

or would this be done as a Business Rule somewhere?

 

Thank you.

1 ACCEPTED SOLUTION

swilson
Tera Expert

Hi, David. You could use a transform script but it might be easier to make a business rule on the user table. The primary notification device is created by a business rule on the user table called 'Create primary email device' so you could just copy that and change it to create the cell device record.



The only issue is that you need to know who the provider is for each user because that is required on the notification device record. If that information is available through your LDAP integration and you aren't   storing it on the user record then you may need to use a transform script since that would be the only time the provider information is available. But the script from the business rule would still provide a starting point for a transform script.



Hope this helps.



Thanks.


- Steve


View solution in original post

6 REPLIES 6

2)   The "Update SMS Device" Business Rule has a bug where if the carrier is changed, it will create duplicate records.



Updating to note that I fixed issue #2 as described above.



The custom field called "Carrier" (u_carrier) on sys_user table needs to be a reference field to Notification Service Provider (cmn_notif_service_provider), and the "Reference qual condition" should be "Active is true".



In other words, out of the box we have:


  • "Service Provider" field (cmn_notif_device.service_provider) is a reference to:   cmn_notif_service_provider with Reference qual condition "Active is true".   Choice List Specification is "Dropdown with -- None -- "

On the user profile form (sys_user), the custom "Carrier" field should also be defined exactly the same way:


  • "Carrier" field (sys_user.u_carrier) is a reference to:   cmn_notif_service_provider with Reference qual condition "Active is true".   Choice List Specification is "Dropdown with -- None -- "


This way when the carrier drop down on the user profile is changed, it will point directly to the service provider table and not the notification device table.



Reference Fields 101, I suppose, but something I had to learn.  




Edit:   In case the above is clear as mud, here's a picture:



CarrierReferenceField.jpg


Updated the business rules.   Here's the latest versions of both.



/****************************************************************************


Create SMS Notification Device



This Business Rule creates a primary SMS notification device based off the


Mobile phone (sys_user.mobile_phone) and Carrier (sys_user.u_carrier) fields.



Type of Script:   Business Rule (after)


Location:   User Table [sys_user]



David Hoffman


Created 10/17/14.   Last modified 10/30/14.


*****************************************************************************/



// Using Filter Conditions (Eureka+) on "When to run" tab instead of Script Condition on "Advanced" tab:


// Mobile phone is not empty AND


// Carrier is not empty AND


// Mobile phone matches regex /[(]?(\d{3})[\s]?[\-.\/\s)]?[\-.\/\s]?[\s]?(\d{3})[\s]?[\-.\/\s]?[\-.\/\s]?[\s]?(\d{4})[.]?/g;


//


// Originally used Script Condition field:   !current.mobile_phone.nil() && !current.u_carrier.nil()



var thisSysID = current.sys_id;


var device = new GlideRecord('cmn_notif_device');


device.user = thisSysID;


device.active = true;


device.name = 'Mobile Phone ' + current.mobile_phone.replace(regexUSA_CAN, "$1-$2-$3");


device.type = 'SMS';



// Regular Expression for USA/Canadian phone numbers in format 999-999-9999, 999.999.9999, (999) 999-9999, etc.


var regexUSA_CAN = /[(]?(\d{3})[\s]?[\-.\/\s)]?[\-.\/\s]?[\s]?(\d{3})[\s]?[\-.\/\s]?[\-.\/\s]?[\s]?(\d{4})[.]?/g;


if (regexUSA_CAN.test(current.mobile_phone)) { device.phone_number = current.mobile_phone.replace(regexUSA_CAN, "$1$2$3"); }



device.service_provider = current.u_carrier;


device.insert();


gs.addInfoMessage(gs.getMessage('SMS Notification Device created for ') + GlideStringUtil.escapeHTML(current.name) +


    ' - ' + GlideStringUtil.escapeHTML(current.mobile_phone.replace(regexUSA_CAN, "$1-$2-$3")));






/****************************************************************************


Update SMS Notification Device



This Business Rule updates a primary SMS notification device based off the


Mobile phone (sys_user.mobile_phone) and Carrier (sys_user.u_carrier) fields.



Type of Script:   Business Rule (after)


Location:   User Table [sys_user]



David Hoffman


Created 10/20/14.   Last modified 10/30/14.


*****************************************************************************/



// Using Filter Conditions (Eureka+) on "When to run" tab instead of Script Condition on "Advanced" tab:


// Mobile phone changes OR


// Carrier changes OR


// Notification changes


//


// Originally used Script Condition field:   current.mobile_phone.changes() || current.u_carrier.changes() || current.notification.changes()



var thisSysID = current.sys_id;


var device = new GlideRecord('cmn_notif_device');


device.addQuery('user', thisSysID);


device.addQuery('type', 'SMS');


device.query();



while (device.next()) {


    if (current.mobile_phone.changes()) {


              // OUTSTANDING "NICE TO HAVE" TO DO:   If the user doesn't have an existing SMS Notification Device, create one.


//               if (!device) {


//                     gs.addInfoMessage(gs.getMessage('No existing SMS Notification Devices.   Creating one.'));


//               }


           


              // Regular Expression for USA/Canadian phone numbers in format 999-999-9999, 999.999.9999, (999) 999-9999, etc.


              var regexUSA_CAN = /[(]?(\d{3})[\s]?[\-.\/\s)]?[\-.\/\s]?[\s]?(\d{3})[\s]?[\-.\/\s]?[\-.\/\s]?[\s]?(\d{4})[.]?/g;


           


              // Use the above regular expression to test if the Mobile Phone is a valid phone number.


              if (regexUSA_CAN.test(current.mobile_phone)) {


                    device.phone_number = current.mobile_phone.replace(regexUSA_CAN, "$1$2$3");


                    device.name = 'Mobile Phone ' + current.mobile_phone.replace(regexUSA_CAN, "$1-$2-$3"); // Update the SMS Notification Device name with new number.


                    device.active = true;


              }


           


              // If the Mobile Phone does not match the above regular expression, consider it an invalid number.


              else if (!regexUSA_CAN.test(current.mobile_phone)) {


                    device.phone_number = '';   // Blank out device number since invalid number.


                    device.name = 'Mobile Phone (Inactive)';


                    device.active = false; // Inactivate device since invalid number.


              }


      }


 


    if (current.notification.changes()) {


          if (current.notification == 2) { // If Notification drop down is set to "Enable"


                device.active = true;


          }


          else {                                                       // If Notification drop down is set to "Disable"


                device.active = false;


          }


    }


 


    if (current.u_carrier.changes()) {


          device.service_provider = current.u_carrier;


    }



 


    device.setWorkflow(false);


    device.update();


    gs.log('Updating ' + current.name + 's SMS Notification devices based on change to user record Mobile phone, Carrier or Notification field');


 


    if (current.mobile_phone != '') {


          gs.addInfoMessage(gs.getMessage('SMS Notification Device updated for ') + GlideStringUtil.escapeHTML(current.name) +


                ' - ' + GlideStringUtil.escapeHTML(current.mobile_phone.replace(regexUSA_CAN, "$1-$2-$3")));


    } else {


          gs.addInfoMessage(gs.getMessage('SMS Notification Device inactivated for ') + GlideStringUtil.escapeHTML(current.name));


    }



}