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.
salesforce
add a comment |
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.
salesforce
add a comment |
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.
salesforce
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
salesforce
edited Nov 5 at 19:43
asked Nov 5 at 19:38
Vincent Zheng
4810
4810
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password