I've been following this SmsToLead procedure and am stuck getting SMS messages into Salesforce. The Twilio debug console reports a Schema Validation Warning and this error:

cvc-elt.1: Cannot find the declaration of element 'html'.

Here's more info on this error:

Warning - 12200

Schema validation warning

The provided XML does not conform to the Twilio Markup XML schema. Please refer to the specific error and correct the problem. Possible Causes

misspelled verbs incorrect case for verbs misspelled or unknown attributes unknown or unexpected nested elements.

Also, a post using curl returns error code 503/Forbidden so the apex isn't even getting executed. Not sure how else to troubleshoot beyond the re-re-rechecking of settings (done). Could a spec have have changed since this was put out there?

I've created a site (home page is InMaintenance or Exception - same result) and have authorized the controller class in Public Settings.

Here is the controller class:

global class SmsToLead {

    static TwilioAccount account = TwilioAPI.getDefaultAccount();

    public static void reply(String fromNumber, String toNumber, String message) {
        Map<String, String> params = new Map<String, String>{
          'From' => fromNumber, 
          'To' => toNumber, 
          'Body' => message
        System.debug('Twilio account info: ' + account);

        TwilioSms sms = account.getSmsMessages().create(params);
        System.debug('Sent SMS SID: '+sms.getSid());

    global static void incomingSMS() {
        // This will error out with System.LimitException if we would exceed 
        // our daily email limit

        String expectedSignature = RestContext.request.headers.get('X-Twilio-Signature');
        String url = 'https://' + RestContext.request.headers.get('Host') + '/services/apexrest' + RestContext.request.requestURI;
        Map <String, String> params = RestContext.request.params;

        // Validate signature
        if (!TwilioAPI.getDefaultClient().validateRequest(expectedSignature, url, params)) {
            RestContext.response.statusCode = 403;
            RestContext.response.responseBody = Blob.valueOf('Failure! Rcvd '+expectedSignature+'\nURL '+url/*+'\nHeaders'+RestContext.request.headers*/);

        // Twilio likes to see something in the response body, otherwise it reports
        // a 502 error in https://www.twilio.com/user/account/log/notifications
        RestContext.response.responseBody = Blob.valueOf('ok');

        // Extract useful fields from the incoming SMS
        String leadNumber     = params.get('From');
        String campaignNumber = params.get('To');
        String leadEmail      = params.get('Body');

        // Try to find a matching Campaign
        Campaign campaign = null;
        try {
            campaign = [SELECT Id, Name, NumberSent FROM Campaign WHERE Phone__c = :campaignNumber LIMIT 1];
        } catch (QueryException qe) {
            reply(campaignNumber, leadNumber, 'No Campaign configured. Sorry.');

        // Create and insert a new Lead
        Lead lead = new Lead(LastName = 'From SMS',
            Company = 'From SMS',
            Email = leadEmail, 
            Phone = leadNumber);

        try {
            insert lead;
        } catch (DmlException dmle) {
            String message = (dmle.getDmlType(0) == StatusCode.INVALID_EMAIL_ADDRESS)
                ? leadEmail+' doesn\'t look like an email address. Please try again.'
                : 'An error occurred. Sorry.';
            reply(campaignNumber, leadNumber, message);

        // Link the Lead to the Campaign
        insert new CampaignMember(CampaignId = campaign.Id, LeadId = lead.Id);

        // We're done - send an SMS 
        reply(campaignNumber, leadNumber, 'Thanks for registering. We\'ll send you a confirmation email!');

        // Send an email, recording it as an activity
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setPlainTextBody('Thanks for registering!');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

What could be going wrong? How can I fix it? Here's an unanswered Q with the same problem (the Twilio debug part, anyway).

  • Can someone please help? is a yes/no question whose answer helps you not one bit. I tweaked your post to ask a more useful question and format the errors in a more standard way so it's easier to find for our frequent users. – Adrian Larson Jun 16 '17 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.