I'm writing an Apex class to make an http callout to an external web service using a Username and Password stored on the User object. The debugger shows it runs successfully but never makes the call out. I have created a User with the Username and password fields populated. Can anyone tell me why it fails to make the callout? Thanks.
global class cstschedulable implements schedulable{
global User cstUser {get;set;}
public Id cstUserId;
public string cstUsername {get;set;}
public string cstPassword {get;set;}
//public string cstNode {get;set;}
public Id ClientAcctId {get;set;}
public datetime cstLastUpdate{get;set;}
public string loginstring {get;set;}
public map<String, string> cstUserMap{get;set;}
global User getUserData(){
If (cstUser == null){
User cstUser = [select Id, CST_User_Name__c,CST_Password__c, CSTlastupdate__c from User where CST_User_Name__c != null ];
}
return cstUser;
}
public string UserData(User cstUser){
if(cstUser.CST_User_Name__c == null)
{
system.debug('cstUser null at UserData');
}
else{
system.debug('cstUser not null at UserData');
}
if(cstUser.CST_User_Name__c != null){
String loginstring = ('"'+cstUser.CST_User_Name__c+'" "password": "'+cstUser.CST_Password__c+'"}');
}
return loginstring;
}
global void execute(SchedulableContext sc){
}
global void returnAuth(string loginstring){
string xAuthToken;
string st_Today;
If(loginstring != null){
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setMethod('POST' ); // Method Type
req.setEndpoint('web210.ntree.com/twig-cstc3/v1/login');//salestree url
req.setHeader('Content-Type','application/json');
req.setHeader('{ "username" : ', loginstring);
res = http.send(req);
if(res.getBody() != null){
if(res.getBody() == 'Rejected: check_login returned false:'){
system.debug('Rejected: check_login returned false:');
}
}else{
system.debug('SalesTree Failed to respond');
}
// Parse entire JSON response.
JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'authenticationToken')) {
// Get the value.
parser.nextToken();
xAuthToken = parser.getText();
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'st:today')){
parser.nextToken();
parser.nextToken();
st_Today = parser.getText();
}
if (xAuthToken != null){
system.debug('Auth Token received');
}
else
{
system.debug('Auth Token not received');
}
if(xAuthToken != null){
User UpdateUser = new User();
UpdateUser.Id = Id.valueof(cstUser.Id);
UpdateUser.X_Auth_token__c = xAuthToken;
UpdateUser.CSTlastupdate__c = Datetime.now();
Update UpdateUser;
}
}
}
}
}
}
else{
system.debug('loginstring null');
}
}
}
Database.AllowCallouts
interface.