Http call return null for request and response in loop for Salesforce











up vote
0
down vote

favorite












Since salesforce doesn't have sleep method, I create a helper function to use web service to wait specific timeout



public class SalesforceHelper {
public static void waitCall(String timeout){
System_Settings__c lnpSetting = System_Settings__c.getValues('System Properties');
String endpoint=lnpSetting.Base_Endpoint_URL__c + 'salesforceHelper/wait?timeout=' + timeout;
system.debug('====endpoint======'+endpoint);
HttpRequest httpReq=new HttpRequest();
HttpResponse httpResp = new HttpResponse();
Http http = new Http();
httpReq.setMethod('GET');
httpReq.setEndpoint(endpoint);
String username=lnpSetting.Endpoint_Username__c;
String password=lnpSetting.Endpoint_Password__c;
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
httpReq.setHeader('Authorization', authorizationHeader);
httpReq.setHeader('content-type','application/json; charset=utf-8');
httpReq.setTimeout(120000);
try{
httpResp = http.send(httpReq);
System.debug('Wait response' + httpResp);
} catch (Exception e) {
system.debug(LoggingLevel.Error, 'Error HTTP response code = ' + httpResp.getStatusCode() + '; calling '+endpoint );
}
}
}


Then in anonymous windows, I run following code:



System.debug('Staring call wait call: ' + System.now().format('HH:mm:ss.SSS'));
SalesforceHelper.waitCall('50000');
System.debug('Finish call wait call: ' + System.now().format('HH:mm:ss.SSS'));
Custom_Record__c record = new List<Custom_Record__C>();
Integer counter = 0;
while (record.size() == 0 && counter < 10){
System.debug('Staring call wait call for counter + ' + counter + ': ' + System.now().format('HH:mm:ss.SSS'));
SalesforceHelper.waitCall('60000');
System.debug('Finish call wait call for counter + ' + counter + ': ' + System.now().format('HH:mm:ss.SSS'));
counter++;
rec = new List<Custom_Record__C>();
}


The code above suppose do following, first wait 50 seconds, then "query" the record, but it return empty, then loop with the counter and try to "query" back the record for 10 times.



But when I run the code, I realize, only the 50 second wait run and first two 60 second wait are run successfully, the rest 8 call in the loop not even hit the web service, and when I check the debug log I found System.HttpRequest[Endpoint=null, Method=null] and System.HttpResponse[Status=null, StatusCode=0], why this happen? And how I can fix this issue?



PS: The original way I want because that Custom_Record__c might be not created yet in the future call method when the time it runs, so I need a way to wait for external library to create it for use. I cannot wait that forever, so I use counter to control that, and I got this issue.










share|improve this question




























    up vote
    0
    down vote

    favorite












    Since salesforce doesn't have sleep method, I create a helper function to use web service to wait specific timeout



    public class SalesforceHelper {
    public static void waitCall(String timeout){
    System_Settings__c lnpSetting = System_Settings__c.getValues('System Properties');
    String endpoint=lnpSetting.Base_Endpoint_URL__c + 'salesforceHelper/wait?timeout=' + timeout;
    system.debug('====endpoint======'+endpoint);
    HttpRequest httpReq=new HttpRequest();
    HttpResponse httpResp = new HttpResponse();
    Http http = new Http();
    httpReq.setMethod('GET');
    httpReq.setEndpoint(endpoint);
    String username=lnpSetting.Endpoint_Username__c;
    String password=lnpSetting.Endpoint_Password__c;
    Blob headerValue = Blob.valueOf(username + ':' + password);
    String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
    httpReq.setHeader('Authorization', authorizationHeader);
    httpReq.setHeader('content-type','application/json; charset=utf-8');
    httpReq.setTimeout(120000);
    try{
    httpResp = http.send(httpReq);
    System.debug('Wait response' + httpResp);
    } catch (Exception e) {
    system.debug(LoggingLevel.Error, 'Error HTTP response code = ' + httpResp.getStatusCode() + '; calling '+endpoint );
    }
    }
    }


    Then in anonymous windows, I run following code:



    System.debug('Staring call wait call: ' + System.now().format('HH:mm:ss.SSS'));
    SalesforceHelper.waitCall('50000');
    System.debug('Finish call wait call: ' + System.now().format('HH:mm:ss.SSS'));
    Custom_Record__c record = new List<Custom_Record__C>();
    Integer counter = 0;
    while (record.size() == 0 && counter < 10){
    System.debug('Staring call wait call for counter + ' + counter + ': ' + System.now().format('HH:mm:ss.SSS'));
    SalesforceHelper.waitCall('60000');
    System.debug('Finish call wait call for counter + ' + counter + ': ' + System.now().format('HH:mm:ss.SSS'));
    counter++;
    rec = new List<Custom_Record__C>();
    }


    The code above suppose do following, first wait 50 seconds, then "query" the record, but it return empty, then loop with the counter and try to "query" back the record for 10 times.



    But when I run the code, I realize, only the 50 second wait run and first two 60 second wait are run successfully, the rest 8 call in the loop not even hit the web service, and when I check the debug log I found System.HttpRequest[Endpoint=null, Method=null] and System.HttpResponse[Status=null, StatusCode=0], why this happen? And how I can fix this issue?



    PS: The original way I want because that Custom_Record__c might be not created yet in the future call method when the time it runs, so I need a way to wait for external library to create it for use. I cannot wait that forever, so I use counter to control that, and I got this issue.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Since salesforce doesn't have sleep method, I create a helper function to use web service to wait specific timeout



      public class SalesforceHelper {
      public static void waitCall(String timeout){
      System_Settings__c lnpSetting = System_Settings__c.getValues('System Properties');
      String endpoint=lnpSetting.Base_Endpoint_URL__c + 'salesforceHelper/wait?timeout=' + timeout;
      system.debug('====endpoint======'+endpoint);
      HttpRequest httpReq=new HttpRequest();
      HttpResponse httpResp = new HttpResponse();
      Http http = new Http();
      httpReq.setMethod('GET');
      httpReq.setEndpoint(endpoint);
      String username=lnpSetting.Endpoint_Username__c;
      String password=lnpSetting.Endpoint_Password__c;
      Blob headerValue = Blob.valueOf(username + ':' + password);
      String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
      httpReq.setHeader('Authorization', authorizationHeader);
      httpReq.setHeader('content-type','application/json; charset=utf-8');
      httpReq.setTimeout(120000);
      try{
      httpResp = http.send(httpReq);
      System.debug('Wait response' + httpResp);
      } catch (Exception e) {
      system.debug(LoggingLevel.Error, 'Error HTTP response code = ' + httpResp.getStatusCode() + '; calling '+endpoint );
      }
      }
      }


      Then in anonymous windows, I run following code:



      System.debug('Staring call wait call: ' + System.now().format('HH:mm:ss.SSS'));
      SalesforceHelper.waitCall('50000');
      System.debug('Finish call wait call: ' + System.now().format('HH:mm:ss.SSS'));
      Custom_Record__c record = new List<Custom_Record__C>();
      Integer counter = 0;
      while (record.size() == 0 && counter < 10){
      System.debug('Staring call wait call for counter + ' + counter + ': ' + System.now().format('HH:mm:ss.SSS'));
      SalesforceHelper.waitCall('60000');
      System.debug('Finish call wait call for counter + ' + counter + ': ' + System.now().format('HH:mm:ss.SSS'));
      counter++;
      rec = new List<Custom_Record__C>();
      }


      The code above suppose do following, first wait 50 seconds, then "query" the record, but it return empty, then loop with the counter and try to "query" back the record for 10 times.



      But when I run the code, I realize, only the 50 second wait run and first two 60 second wait are run successfully, the rest 8 call in the loop not even hit the web service, and when I check the debug log I found System.HttpRequest[Endpoint=null, Method=null] and System.HttpResponse[Status=null, StatusCode=0], why this happen? And how I can fix this issue?



      PS: The original way I want because that Custom_Record__c might be not created yet in the future call method when the time it runs, so I need a way to wait for external library to create it for use. I cannot wait that forever, so I use counter to control that, and I got this issue.










      share|improve this question















      Since salesforce doesn't have sleep method, I create a helper function to use web service to wait specific timeout



      public class SalesforceHelper {
      public static void waitCall(String timeout){
      System_Settings__c lnpSetting = System_Settings__c.getValues('System Properties');
      String endpoint=lnpSetting.Base_Endpoint_URL__c + 'salesforceHelper/wait?timeout=' + timeout;
      system.debug('====endpoint======'+endpoint);
      HttpRequest httpReq=new HttpRequest();
      HttpResponse httpResp = new HttpResponse();
      Http http = new Http();
      httpReq.setMethod('GET');
      httpReq.setEndpoint(endpoint);
      String username=lnpSetting.Endpoint_Username__c;
      String password=lnpSetting.Endpoint_Password__c;
      Blob headerValue = Blob.valueOf(username + ':' + password);
      String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
      httpReq.setHeader('Authorization', authorizationHeader);
      httpReq.setHeader('content-type','application/json; charset=utf-8');
      httpReq.setTimeout(120000);
      try{
      httpResp = http.send(httpReq);
      System.debug('Wait response' + httpResp);
      } catch (Exception e) {
      system.debug(LoggingLevel.Error, 'Error HTTP response code = ' + httpResp.getStatusCode() + '; calling '+endpoint );
      }
      }
      }


      Then in anonymous windows, I run following code:



      System.debug('Staring call wait call: ' + System.now().format('HH:mm:ss.SSS'));
      SalesforceHelper.waitCall('50000');
      System.debug('Finish call wait call: ' + System.now().format('HH:mm:ss.SSS'));
      Custom_Record__c record = new List<Custom_Record__C>();
      Integer counter = 0;
      while (record.size() == 0 && counter < 10){
      System.debug('Staring call wait call for counter + ' + counter + ': ' + System.now().format('HH:mm:ss.SSS'));
      SalesforceHelper.waitCall('60000');
      System.debug('Finish call wait call for counter + ' + counter + ': ' + System.now().format('HH:mm:ss.SSS'));
      counter++;
      rec = new List<Custom_Record__C>();
      }


      The code above suppose do following, first wait 50 seconds, then "query" the record, but it return empty, then loop with the counter and try to "query" back the record for 10 times.



      But when I run the code, I realize, only the 50 second wait run and first two 60 second wait are run successfully, the rest 8 call in the loop not even hit the web service, and when I check the debug log I found System.HttpRequest[Endpoint=null, Method=null] and System.HttpResponse[Status=null, StatusCode=0], why this happen? And how I can fix this issue?



      PS: The original way I want because that Custom_Record__c might be not created yet in the future call method when the time it runs, so I need a way to wait for external library to create it for use. I cannot wait that forever, so I use counter to control that, and I got this issue.







      salesforce






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 5 at 19:43

























      asked Nov 5 at 19:38









      Vincent Zheng

      4810




      4810





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53161082%2fhttp-call-return-null-for-request-and-response-in-loop-for-salesforce%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53161082%2fhttp-call-return-null-for-request-and-response-in-loop-for-salesforce%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()