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

shreyans
ServiceNow Employee
ServiceNow Employee

is it possible to pass a variable instead of a hardcoded group there


Yes, absolutely. Infact, that would be the preferred way!



Assuming current here refers to incident table, current.assignment_group will give you the sys_id of the group. If you adjust your GlideRecord query a little bit you should be able to do it for different groups instead of having to hardcode a value.



var group = current.assigment_group;


var gr1 = new GlideRecord('sys_user_grmember');    


gr1.addQuery('group', "=", group);    


gr1.query();


chedley
Tera Contributor

Hi Shreyans, thank you very much for your response. It was very helpful to understand that the sendSMS() function does not accept arrays, and your further comments overall were also helpful. Appreciate the help.


Conor, sorry for the delay, I have been out of the office on vacation the last week and a half and just catching up on this.   Happy to hear Shreyans got you sorted.



Out of the box Notify is user based and the API functions work for individuals but as you have figured out you can loop through a list to send it out to a group or multiple people.