System.CalloutException: You have uncommitted work pending

You're trying to insert a new case record and then make the call out — the case is your uncommitted work in this instance.

You should insert the case, and then call a new method which contains the rest of the code for the callout, and mark that with the @future annotation, something like this:

public PageReference init()
{           
    System.debug('From Phone Number :' +fromNumber);
    System.debug('To phone NUmber :' + toNumber);
    System.debug('Message Body :' + body);                                              
    System.debug('FROM AND To  Number is NOT NULL');

    String formattedNumber='+919876543210';    
    if(body != NULL)
        body = body;
    else
        body = '';
    Case c = NEW Case (Subject = formattedNumber,Description = body,Origin = 'Phone');
    insert c;                 

    Map<String,String> params1 = new Map<String,String>
    {
       'To'   => fromNumber,
       'From' => '+1908280****',
       'Body' => 'Conformation to customer'
    };

    DoCallout(params);

    return null ;
}

@future(callout=true)
private static void DoCallout(Map<String, String> params)
{
    TwilioRestClient Client = TwilioAPI.getDefaultClient();

    TwilioMessage message = Client.getAccount().getMessages().create(params);
    // Valid conformation SMS sent to the Customer.                                   
}

Also, you're doing all of this from the page init method — you really shouldn't do DML operations on page load as that's open to abuse and would fail security review if you were submitting this to the AppExchange. It's much better to have the user confirm they want to perform the action using a button on the page or similar.


One of the main reason of this exception is that you are doing some CRUD operation (create,read,update,delete) in your code in same context of running code, means you are doing some DML operation and then doing call out in single run.

You have to break thread with @future annotation.