• Jannis Bott
  • NEWBIE
  • 0 Points
  • Member since 2014
  • Cloud Crowd Pty Ltd


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
Hi all,

I have implemented an SMS service from Twilio that can handle incoming messages. I have pretty much followed this awesome video from Pat Patterson.

I am struggling to write a test class that passes the Twilio request validation. Can anybody please help me out and show how to do it? 

Here is my class:
@RestResource(urlMapping='/TwilioRecruitment')
global class TwilioRecruitment {
    static TwilioAccount account = TwilioAPI.getDefaultAccount();
    
    @future(callout=true)
    public static void reply(string fromNumber, string toNumber, string message){
        Map<String, String> params = new Map<String, String> {
            'From' => fromNumber,
            'To' => toNumber,
            'Body' => message
        };
        
        TwilioSms sms = account.getSmsMessages().create(params);
        system.debug('Send SMS SId' +  ' ' + sms.getSid());
    }
    
    @HttpPost
    global static void incomingSMS() {
        
        String expectedSignature = 
            RestContext.request.headers.get('X-Twilio-Signature');
            system.debug('expectedSignature ' + expectedSignature);
        String url = 'https://' + RestContext.request.headers.get('Host') + 
            '/services/apexrest' + RestContext.request.requestURI;
            system.debug('url ' + url);
        Map <String, String> params = RestContext.request.params;
        system.debug('params ' + params);
        system.debug('TwilioAPI.getDefaultClient().validateRequest(expectedSignature, url, params) ' + TwilioAPI.getDefaultClient().validateRequest(expectedSignature, url, params));
        // Validate signature
        if (!TwilioAPI.getDefaultClient().validateRequest(expectedSignature, url, params)) {
            RestContext.response.statusCode = 403;
            RestContext.response.responseBody = 
                Blob.valueOf('Failure! Rcvd '+expectedSignature+'\nURL '+url);
            return;
        } 
        
        //Twilio to check if something is in the response body otherwise report
        // a 502 error in https://twilio.com/account/log/notifications
        
        RestContext.response.responseBody = Blob.valueOf('ok');
        
        //Extract useful fields from the incoming SMS
        String senderNumber = params.get('From');
        String receiver = params.get('To');
        String body = params.get('Body');
        system.debug('From, To, Body' + ' ' + senderNumber + ' ' + receiver + ' ' + body);
        //Try find matching TFP employee
        TFP_Employee__c tfpEmpl = null;
        try{
            String prefixAU = '+61';
            String formattedSenderNumber = '';
            if(senderNumber.startsWith(prefixAU)){
                formattedSenderNumber = 0 + senderNumber.removeStart(prefixAU);
            }
           ... More logic below here...

This is the test class that fails at the validation:
@isTest
global class TwilioRecruitmentTest {

    static testMethod void testGet() {

        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        req.requestURI = '/services/apexrest/TwilioRecruitment';
        req.httpMethod = 'POST';

        req.addParameter('From','+61458883222');
        req.addParameter('Body','This is a test');
        req.addParameter('To','+61437879336');
        req.addParameter('ToCountry','AU');
        req.addParameter('ToState','');
        req.addParameter('SmsMessageSid','SMS MESSAGE ID HERE ');
        req.addParameter('NumMedia','0');
        req.addParameter('ToCity','');
        req.addParameter('FromZip','');
        req.addParameter('SmsSid','SMS ID HERE');
        req.addParameter('SmsStatus','received');
        req.addParameter('FromCity','');
        req.addParameter('FromCountry','AU');
        req.addParameter('ToZip','');
        req.addParameter('MessageSid','MESSAGE SID HERE');
        req.addParameter('AccountSid','ACCOUNT SID HERE');
        req.addParameter('ApiVersion','2010-04-01');

        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;
        
        TwilioRecruitment.incomingSMS();
        
    }
}

Any help would be awesome!
Thanks!
Hi all, I Have Two Objects called Student and Payment Student is the Master Object and Payment is the Child Object In those i have a common field called Fee When i enter a Fee field in the parent object its child objects also fill with the same value so can any one tell me the code for this to achieve After Triggers
I created a flow that uses a VF page (www.mydomain.force.com), that is accessed outside of SFDC by a "Guest User" license.  The flow simply creates a Lead within SFDC (standard object), and that is all.  As of the Summer 14 release into our org, I now receive the following error message every time someone tries to access it:

UPSERT --- UPSERT FAILED ---  ERRORS :  (INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY) insufficient access rights on cross-reference id ---  for SFDC record with ID : null,

Please help me figure out how to fix this error.  I am only referencing one object in my flow (creating a Lead), so I'm not sure what the "cross reference" is referring to.  Any help is much appreciated!
Hi All,

Just wondering if there's a way to simplify the formula I am working on.

To give you a background, I am trying to create a formula that will execute the standard address formatting that will be pulled to a Conga template.
There are 2 types of address consists of fields namely:
1. Postal Address
              PO Box<br>
              City<double space>State<double space>Postcode<br>
              Country
2. Residential Address
              Building/property name<double space>Flat Unit Details<double space>Street No.<double space>Street Name<br>
              City<double space>State<double space>Postcode<br>
              Country

The formula checks if any of postal address fields have values, so
              if YES, it will return the concatenated postal address
              if NO, it will check if Building/property name or Flat Unit details have values
                         so if YES, then it will display all the address fields for the residential address
                               if NO, then it will only display Street No. and Street name on the first line of the residential address and the rest of the designated fields


Here's the actual formula:
IF(

OR(
Other_City__c <> "", Other_Country__c <> "", Other_Street__c <> "", Other_Zip_Postal_Code__c <> "", Other_State_Provience__c <> "" ),
UPPER(Other_Street__c) & "  " & UPPER(Postal_street_number__c) & "  " & UPPER(Postal_street_name__c) & "<br>" & UPPER(Other_City__c ) & "  " & UPPER(CASE( Other_State_Provience__c , "Queensland", "QLD", "QLD", "QLD", "Western Australia", "WA", "WA", "WA", "Australian Capital Territory", "ACT", "ACT", "ACT", "New South Wales", "NSW", "NSW", "NSW", "Northern Territory", "NT", "NT", "NT", "South Australia","SA", "SA", "SA", "Tasmania", "TAS", "TAS", "TAS", "Victoria", "VIC", "VIC", "VIC", "Other (Overseas but not an Australian territory or dependency)", "OVERSEAS", "OVERSEAS", "OVERSEAS", "OTHER" )) & "  " & Other_Zip_Postal_Code__c & "<br>" & UPPER(Other_Country__c ),

IF(OR(Address_building_property_name__c <> "", Address_flat_unit_details__c <> ""),

UPPER(Address_building_property_name__c) & "  " & UPPER(Address_flat_unit_details__c) & "  " & UPPER(Address_street_number__c) & "  " & UPPER(Address_street_name__c) & "<br>" & UPPER(Mailing_City__c) & "  " & UPPER(CASE( Mailing_State_Provience__c , "Queensland", "QLD", "QLD", "QLD", "Western Australia", "WA", "WA", "WA", "Australian Capital Territory", "ACT", "ACT", "ACT", "New South Wales", "NSW", "NSW", "NSW", "Northern Territory", "NT", "NT", "NT", "South Australia","SA", "SA", "SA", "Tasmania", "TAS", "TAS", "TAS", "Victoria", "VIC", "VIC", "VIC", "Other (Overseas but not an Australian territory or dependency)", "OVERSEAS", "OVERSEAS", "OVERSEAS", "OTHER" )) & "  " & Mailing_Zip_Postal_Code__c & "<br>" & UPPER(Mailing_Country__c ),

UPPER(Address_street_number__c) & "  " & UPPER(Address_street_name__c) & "<br>" & UPPER(Mailing_City__c) & "  " & UPPER(CASE( Mailing_State_Provience__c , "Queensland", "QLD", "QLD", "QLD", "Western Australia", "WA", "WA", "WA", "Australian Capital Territory", "ACT", "ACT", "ACT", "New South Wales", "NSW", "NSW", "NSW", "Northern Territory", "NT", "NT", "NT", "South Australia","SA", "SA", "SA", "Tasmania", "TAS", "TAS", "TAS", "Victoria", "VIC", "VIC", "VIC", "Other (Overseas but not an Australian territory or dependency)", "OVERSEAS", "OVERSEAS", "OVERSEAS", "OTHER" )) & "  " & Mailing_Zip_Postal_Code__c & "<br>" & UPPER(Mailing_Country__c )
)
)

You may notice that there are case methods on the formula which eat up a lot on the editor so to explain it, the 'State' data on the app was not uniformed so I had to check both formats (Complete State name and Short name) and return only one (Short name will be returned).

Problem: I cannot save the formula I created because it exceeds 5000 characters on the editor.
To-do: Simplify the formula

Any idea or suggestion to solve this? Thank you very much.