Views:

Scenario Overview

In this scenario we will show you how to use WebFusion to use the Twilio service to send SMS messages from Dynamics 365. In addition we will show you how to use xCache to store & retrieve the static configuration data needed by Twilio to connect & send SMS messages.

North52 Decision Suite

The BPA solution works like this,

  • We create a custom activity entity called SMS. 
  • A Yes \ No field on the entity controls when the message is sent.
  • Three xCache records are created to store the Twilio configuration data AccountSID, Token and FromNumber
  • Finally we use the CallRestAPI() function to send the SMS message using data on the SMS entity for the TO Number & the message text

Configure the formula

The following set of steps will set this formula up for you,

  • Create a new formula of type 'Save - Perform Action'
  • Set the Source Entity to 'SMS'
  • Copy & paste the formula below into the formula description field
  • Click Save

Configure xCache

The following set of steps will set the workflow up for you,

  • Using the main BPA menu bring up the list view of the xCache entity
  • Create a new xCache record as shown in the screenshot below. Please note you will need to substitute in your own Twilio account details as data shown in dummy data.
  • You are ready now to test


 

Formula

if( [north52_sms.sendmessage] = true ,  

  CallRestAPI(
            SetRequestBaseURL('https://api.twilio.com/2010-04-01'),
            SetRequestResource('/Accounts/{0}/Messages.json', xCacheGet('Twilio_AccountSID') ),
            SetRequestDetails('Post'), 
            SetRequestHeaders(),
            SetRequestParams('From', xCacheGet('Twilio_FromNumber'), 'To', '+' + 
                            [north52_sms.north52_mobilenumber], 'Body', [north52_sms.north52_message]),  
            SetRequestAuthenticationBasic(xCacheGet('Twilio_AccountSID'), xCacheGet('Twilio_AccountToken')),
            SetRequestFiles(),
            SetRequestExpected('Created'),
            SetRequestActionPass(
                                UpdateRecord('north52_sms',
                                             [north52_sms.activityid] ,
                                             SetAttribute('north52_responsecontent',
                                                          'SMS ID:' + GetVarJsonValue('sid') +
                                                        ' DateCreated:' + GetVarJsonValue('date_created')),
                                             SetAttribute('scheduledend', [north52_sms.actualend]),
                                             SetAttribute('actualstart', [north52_sms.actualend]),
                                             SetAttributeStatus(1,217890002)
                                            /* Close the record with Sent Status */
                               )
                  ),
                  SetRequestActionFail(
                                      UpdateRecord('north52_sms',
                                                 [north52_sms.activityid] ,
                                                SetAttribute('north52_responsecontent', GetVar('responsecontent')),
                                               SetAttributeStatus(0,217890000) /*Set error message*/
                                                                                 )
                                                       )
   ) , 'NoOp')

JSON Response Object from Twillio

{
"sid": "SM1f0e8ae6ade43cb3c0ce4525424e404f",
"date_created": "Fri, 13 Aug 2010 01:16:24 +0000",
"date_updated": "Fri, 13 Aug 2010 01:16:24 +0000",
"date_sent": null,
"account_sid": "AC228b97a5fe4138be081eaff3c44180f3",
"to": "+15305431221",
"from": "+15104564545",
"body": "A Test Message",
"status": "queued",
"flags":["outbound"],
"api_version": "2010-04-01",
"price": null,
"uri": "\/2010-04-01\/Accounts\/AC228ba7a5fe4238be081ea6f3c44186f3\/SMS\/Messages\/SM1f0e8ae6ade43cb3c0ce4525424e404f.json"
}