You need to sign in to do that
Don't have an account?
Ramesh Somalagari
Exception :
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out*
**Visualforce Page**
<apex:page controller="Controller" action="{!init}" showHeader="false" sidebar="false">
<center>
<apex:pageBlock title="Request Listener"></apex:pageBlock>
</center>
</apex:page>
**Controller :**
PUBLIC with sharing class Controller
{
PUBLIC String fromNumber = ApexPages.currentPage().getParameters().get('From');
PUBLIC String toNumber = ApexPages.currentPage().getParameters().get('To');
PUBLIC String body = ApexPages.currentPage().getParameters().get('Body');
PUBLIC PageReference init()
{
System.debug('From Phone Number :' +fromNumber);
System.debug('To phone NUmber :' + toNumber);
System.debug('Message Body :' + body);
TwilioRestClient Client = TwilioAPI.getDefaultClient();
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'
};
TwilioMessage message = client.getAccount().getMessages().create(params1); /* Valid conformation SMS sent to the Customer.*/
return null ;
}
}
While login as admin then click preview on visualforce page executed but case is not created.I have checked the the debug log it throw the exception.
System.CalloutException: You have uncommitted work pending
Exception :
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out*
**Visualforce Page**
<apex:page controller="Controller" action="{!init}" showHeader="false" sidebar="false">
<center>
<apex:pageBlock title="Request Listener"></apex:pageBlock>
</center>
</apex:page>
**Controller :**
PUBLIC with sharing class Controller
{
PUBLIC String fromNumber = ApexPages.currentPage().getParameters().get('From');
PUBLIC String toNumber = ApexPages.currentPage().getParameters().get('To');
PUBLIC String body = ApexPages.currentPage().getParameters().get('Body');
PUBLIC PageReference init()
{
System.debug('From Phone Number :' +fromNumber);
System.debug('To phone NUmber :' + toNumber);
System.debug('Message Body :' + body);
TwilioRestClient Client = TwilioAPI.getDefaultClient();
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'
};
TwilioMessage message = client.getAccount().getMessages().create(params1); /* Valid conformation SMS sent to the Customer.*/
return null ;
}
}
While login as admin then click preview on visualforce page executed but case is not created.I have checked the the debug log it throw the exception.
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'
};
sendSMS();
return null ;
}
@future
private void sendSMS()
{
Map<String,String> params1 = new Map<String,String>
{
'To' => fromNumber,
'From' => '+1908280****',
'Body' => 'Conformation to customer'
};
TwilioRestClient Client = TwilioAPI.getDefaultClient();
TwilioMessage message = Client.getAccount().getMessages().create(params);
}
All Answers
This is a salesforce limitation you can't do a DML Statement and do a callout in the same context.
What i suggest for you is to stop the callout from the VF , and to create an after insert trigger on case with an @ future method and do the callout of sending sms from this method,it should work.
Good luck
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'
};
sendSMS();
return null ;
}
@future
private void sendSMS()
{
Map<String,String> params1 = new Map<String,String>
{
'To' => fromNumber,
'From' => '+1908280****',
'Body' => 'Conformation to customer'
};
TwilioRestClient Client = TwilioAPI.getDefaultClient();
TwilioMessage message = Client.getAccount().getMessages().create(params);
}
Can you help me how you seperated dmls and callouts in your code. i am also facing the same issue . Please see my link.
https://developer.salesforce.com/forums/?state=id#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=ALLQUESTIONS&id=906F0000000BKhfIAG
You can check blog https://salesforcecodex.com/salesforce/avoid-batch-apex-and-use-queueable-class/ (https://salesforcecodex.com/salesforce/avoid-batch-apex-and-use-queueable-class/)for more detail about, how to resolve this issue. We can utilize Queueable class instead of futuure and batch apex.
Thank You,
Dhanik