Send SMS to members of a group using Notify API and Twilio

chedley
Tera Contributor

Hi community members, I have a use case to send SMS to members of a group using the Notify API leveraging Twilio. I have looked at the NotifyUtils Script includes, looking at some of michael.ritchie's Crisis Management app and others posts but can't seem to construct a gliderecord query or even use the out of the box Notify workflows to get SMS's sent to all members of a group.

I simply want to leverage a business rule on the Incident table or others, where I can take the assignment_group field, query its members and obviously their phone number, send it to an array and have the .sendSMS function iterate over the members numbers.

Is there a straightforward way of doing this that's not using the OOB Notify workflow?

Appreciate the discussion.

1 ACCEPTED SOLUTION

shreyans
ServiceNow Employee
ServiceNow Employee

Hello Conor,



I made some edits to your code that you can review in the code sample below. The main thing to note is that sendSMS() method does not accept an array. However, we do not need to build an array since we are already inside a loop and can invoke the Notify API to send the text within that loop. Let me know if you have more questions.



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



      // The following two lines of code can be written outside of the while loop.


      // Initialize Notify


      var notify = new SNC.Notify();  



      // Get Notify phone number to send the message from


      var twilio = gs.getProperty('glide.notify.task.phone_number');  



      // Query for group members


      var gr1 = new GlideRecord('sys_user_grmember');  


      gr1.addQuery('group.name', "IN", "IncMgmt");  


      gr1.query();  



      // Iterate over each group member


      while (gr1.next()) {  



              // We don't really need to put all the phone numbers in an array


              // Since we are iterating over each group member anyway, we can


              // send an SMS in the same iteration for each group member but one


              // group member at a time


              var userPhone = gr1.getDisplayValue('user.mobile_phone');  



              // Message contents


              strMessage = "This is a test";  



              // Call Notify API to send the actual text            


              if (userPhone) {


                      notify.sendSMS(twilio, userPhone, strMessage, current);


              }    


      }


})(current, previous);


View solution in original post

7 REPLIES 7

chedley
Tera Contributor

I wanted to add that it appears that I can't even use the sendSMS function to an array of numbers. Could this be correct?



function sendSMStoAssignedUser() {


      var nn = new SNC.Notify();


      strMessage = "This is a test"; //The message being sent


      var participants = ["+12222222222", "+15555555555"]


      var twilio = "+1234567890"; //my twilio number


      nn.sendSMS(twilio,participants,strMessage, current);


chedley
Tera Contributor

When attempting to send to an array of numbers, I get the below error. I've tried this many ways unsuccessfully, turning the array to string values or joining them. Do I need a for action to loop through to permit the sendSMS() function to work?



Notify: communication error while sending SMS to org.mozilla.javascript.NativeArray@c62a5d (+org.mozilla.javascript.NativeArray@c62a5d is not an E.164 compliant phone number): no thrown error



This is essentially what i'm working with to get the members of a group, their numbers, and the execute the SMS


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




  var arrayUtil = new ArrayUtil();


  var gr1Array = [];


  var gr1 = new GlideRecord('sys_user_grmember');


  gr1.addQuery('group.name', "IN", "IncMgmt");


  gr1.query();


            while (gr1.next()) {


            gr1Array.push(gr1.getDisplayValue('user.mobile_phone')); //Get my values and push it to an array


            //gs.print(gr1Array);



  var notify = new SNC.Notify();


  var twilio = gs.getProperty('glide.notify.task.phone_number');


  strMessage = "This is a test";


  notify.sendSMS(twilio, gr1Array, strMessage, current);



}})(current, previous);



edit: michael.ritchie any thoughts?


shreyans
ServiceNow Employee
ServiceNow Employee

Hello Conor,



I made some edits to your code that you can review in the code sample below. The main thing to note is that sendSMS() method does not accept an array. However, we do not need to build an array since we are already inside a loop and can invoke the Notify API to send the text within that loop. Let me know if you have more questions.



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



      // The following two lines of code can be written outside of the while loop.


      // Initialize Notify


      var notify = new SNC.Notify();  



      // Get Notify phone number to send the message from


      var twilio = gs.getProperty('glide.notify.task.phone_number');  



      // Query for group members


      var gr1 = new GlideRecord('sys_user_grmember');  


      gr1.addQuery('group.name', "IN", "IncMgmt");  


      gr1.query();  



      // Iterate over each group member


      while (gr1.next()) {  



              // We don't really need to put all the phone numbers in an array


              // Since we are iterating over each group member anyway, we can


              // send an SMS in the same iteration for each group member but one


              // group member at a time


              var userPhone = gr1.getDisplayValue('user.mobile_phone');  



              // Message contents


              strMessage = "This is a test";  



              // Call Notify API to send the actual text            


              if (userPhone) {


                      notify.sendSMS(twilio, userPhone, strMessage, current);


              }    


      }


})(current, previous);


chedley
Tera Contributor

Hi Shreyans - thank you kindly for your response. This solution by looping through the existing statement instead of pushing to an array works well. I have one other quick question while I have your attention. You will have noticed in my original code that in the query of the sys_user_grmembers, I have hardcoded a test group "IncMgmt".



In reading the documentation on query and GlideRecords in general, is it possible to pass a variable instead of a hardcoded group there, e.g. current.assignment_group or call an existing variable e.g. var group = current.assignment_group?



Example:


                                            var group = current.assigment_group


  1.   var gr1 = new GlideRecord('sys_user_grmember');      
  2.       gr1.addQuery('group.name', "IN", group);      
  3.       gr1.query();