0
public with sharing class CalloutHandler {

@future (callout=true)
public static void createAccount(String id)
{
    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setHeader('Content-Type','application/x-www-form-urlencoded');
    String endPoint='https://portal/webapi/createorg.jsp';
    req.setEndpoint(endPoint);
    req.setMethod('POST');
    String accountinfo = getAccountDetail(id);
    System.debug(accountinfo);
    req.setBody(accountinfo);
    try{
        HttpResponse response = http.send(req);  
        if(response.getStatusCode() !=200)
        {
         System.debug('Status code return was not expected: '+ response.getStatusCode() +' '+response.getStatus());
        }
        else{
          String constitid =response.getBody();
          System.debug(constitid);  
          //some stuff
        } 
  }catch(System.CalloutException e){
      System.debug(e); 
 }

}

  • 1
    Take a look at these search results from this site. You must do the callout before you do any DML in the transaction. – Keith C Jul 11 '17 at 7:44
  • I have a web service call out that is working fine. The web service callout is triggered in an After Trigger, which call @future method in the above class , which then make the callout. In test class , In my testmethod for this, I start by setting up some test data(list of accouunt: accts) and inside Test.startTest and Test.stopTest , i mock the response first and then did dml operation : "insert accts". – Archita patnaik Jul 11 '17 at 7:45
  • I knew that inserts performed in test methods are not committed, thats why i am getting this error. but according to requirment, the callout is made from DML.( insert acc-> trigger -> call @future method -> call callout. – Archita patnaik Jul 11 '17 at 7:51
  • Add this extra detail to your question: are you saying you only get the error in your test case? If so include that in the question. – Keith C Jul 11 '17 at 8:26
  • While tesing in sandbox , i am not getting any error(Test method pass), but while deploying into production, its fail and gave me this error – Archita patnaik Jul 11 '17 at 9:05

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.