• Nikhil Khandare 1
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 2
    Replies
HI All,

I am trying to connect to the reseful webservice using HTTP callout. However, each time I am getting Read time out exception. Even I set maximum allowed time out ie 120 sec.

Using browser, postman and SoapUI, I am able to connect to the webservice. However, with only apex code, I am facing this issue. Please let me know how can I fix this error. Below is the code.

public class CraiWSCall {

    //This is used to fetch access cookie based token from Crai WS
    
    public String getAuthToken()
    {
        String cookie = '';
        String access_token = '';
        CraiCredentials__c obj = CraiCredentials__c.getValues('TokenEndpoint');
        HttpRequest req = new HttpRequest();  
        req.setMethod('POST');
        req.setEndpoint(obj.Endpoint__c + '?username=test&password=test');
        req.setHeader('Content-type', 'text/plain');
        req.setHeader('Content-length', '27');
        //req.setHeader('Host', 'puntivendita.crai.org');
        // req.setHeader('Proxy-Connection', 'Keep-Alive');
        // req.setTimeout();
        Http httpObj = new Http();
        HTTPResponse res = new HttpResponse();
        res  = httpObj.send(req); 
        System.debug('Error Messeg:' + res.getStatus());
         return cookie;
    }
}
 
HI All,

 I am executing below code. 

global class CustomIterable implements Iterator<Object>{ 
   List<Account> accs {get; set;} 
   List<Object> listOfAccounts {get; set;}
   Integer i {get; set;} 
   global CustomIterable(){ 
       string s = '{"d":[{"Sede":"Crai","Cedi":"A.R.Ce.V","CodECR":"0010001","IDPV":1,"IDNielsen":"","CodAgente":"","RagSoc":"Punto vend. 1","PartitaIVA":""},{"Sede":"Crai","Cedi":"A.R.Ce.V","CodECR":"0010001","IDPV":1,"IDNielsen":"","CodAgente":"","RagSoc":"Punto vend. 1","PartitaIVA":""}]}';
       Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s);
       List<Object> listOfAccounts = (List<Object>) m.get('d');
       System.debug('Size Of Account oBjects:' + listOfAccounts.size());
       accs = [SELECT id, name, numberofEmployees FROM Account LIMIT 10]; 
       //System.debug('size of Total Account List:' + accs.size());
       i = 0; 
   }   
   global boolean hasNext(){ 
       System.debug('Size of ListOfAccounts:' + listOfAccounts.size());
       if(i >= listOfAccounts.size()) 
           return false; 
       else 
           return true; 
   }    
   global Object next(){ 
       i=i+1; 
       return listOfAccounts[i-1]; 
   } 
}

I am getting the below error on line System.debug('Size of ListOfAccounts:' + listOfAccounts.size());
FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

I cab get the size of list listOfAccounts in cinstructor. However, I am getting the error on mentioned line.

Please help me resolve this query.

Thanks,
Nikhil Khandare
Hi All,

I am trying to call external JWT based rest webserice from salesforce. However, I am getting 400 Bad Request error. Here is the code.

    public class JWTBearerFlow 
    {     
         public static String getAccessToken(String tokenEndpoint, JWT jwt) 
         {

         String access_token = null;
         String body = 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-
         type%3Ajwt-bearer&assertion=' + jwt.issue();
         HttpRequest req = new HttpRequest();  
         Blob headerValue = Blob.valueOf('username' + ':' + 'password');
         String authorizationHeader = 'BASIC ' + 
         EncodingUtil.base64Encode(headerValue);
         req.setHeader('Authorization', authorizationHeader);
         req.setMethod('POST');
         req.setEndpoint(tokenEndpoint);
         req.setHeader('Content-type', 'application/x-www-form-urlencoded');
         req.setHeader('Content-type', 'application/json');
         req.setBody(body);
         Http http = new Http();               
         HTTPResponse res = http.send(req);
         System.debug('Error Messeg:' + res.getStatus());
         if ( res.getStatusCode() == 200 ) 
         {
             System.debug('response:' + res.getStatus());
             System.JSONParser parser = System.JSON.createParser(res.getBody());
             while (parser.nextToken() != null) 
             {
                 if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && 
                (parser.getText() == 'access_token')) 
                {
                    parser.nextToken();
                    access_token = parser.getText();
                    break;
                }
            }
         }
         return access_token;

         }
    }

         
Here is how I am giving the call to getAccessToken.

    JWT jwt = new JWT('none');
    jwt.iss = 'your issuer';
    jwt.sub = 'some subject';
    jwt.aud = 'some audience';
    JWTBearerFlow.getAccessToken('http://egrocery.flosslab.com/egrocery-
    backoffice/api/login',jwt);

Please suggest me what should bevalue of the variables
    jwt.iss
    jwt.sub
    jwt.aud

Thanks in advanced
Hi All,

I want to insert account record from one SF orgnisation to another SF org. In destination orgnaisation i have created below webservice method.
global class testSOAPAccInsert{

    webservice static Id insertAcc(String name)
    {
        Account acc = new Account();
        acc.Name = name;
        insert acc;
        return acc.Id;
    }
}

I generated wsdl from it and created class using wsdl in source orgnisation. However, when i tried to invoke this method from class generated using wsdl i am getting illegeal session id error. 

How would i get session id or how can i impelment login method in my global class. Please help.

Thanks,
Nikhil 
Hi All,

I am getting below error for SOAP sebservice call

01:26:29.470 (470158466)|USER_DEBUG|[19]|DEBUG|Response**:System.HttpResponse[Status=Unsupported Media Type, StatusCode=415]


Below is the code
String strBody = '<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/soap/envelope/"><soap12:Body><GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>FPInventoryTest</listName><viewName></viewName><query><Where><Gt><FieldRef Name="ID" /><Value Type="Counter">0</Value></Gt></Where></query><viewFields><FieldRef Name="ID" /><FieldRef Name="Title" /><FieldRef Name="Property_x0020_Code"/><FieldRef Name="Supplier_x0020_Code" /><FieldRef Name="Property_x0020_Name" /><FieldRef Name="City_x0020_Name" /></viewFields><rowLimit>100</rowLimit><queryOptions></queryOptions><webID></webID></GetListItems></soap12:Body></soap12:Envelope>';

req.setEndpoint('https://teamsitespp.travelport.com/commregion/TravelClick/_vti_bin/lists.asmx?op=GetList');

req.setHeader('Content-Type', 'application/soap+xml; charset=utf-8');
Blob body = Blob.ValueOf(strBody.unescapeHtml4());

//req.setHeader('Content-length','10000');
//req.setheader('SOAPAction','http://schemas.microsoft.com/sharepoint/soap/GetListItems');
req.setheader('HOST','teamsitespp.travelport.com');
req.setMethod('GET');
req.setBodyAsBlob(body);
HttpResponse res = httpObj.send(req);
System.debug('Response**:' + res);
System.debug('Response**:' + res.getBody());

Please let me know whats wrong with the request.
Appreciate help.

Thank You!
Nikhil
Hi All,

While calling webservice I am setting following xml request as body
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header/><soap:Body><GetList xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>FPInventoryTest</listName></GetList></soap:Body></soap:Envelope>

In response, I am getting following error messege 


Response**:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Server was unable to process request. ---&gt; Root element is missing.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

Please help me sort this out.

Thank You!
Nikhil
HI All,

I am trying to connect to the reseful webservice using HTTP callout. However, each time I am getting Read time out exception. Even I set maximum allowed time out ie 120 sec.

Using browser, postman and SoapUI, I am able to connect to the webservice. However, with only apex code, I am facing this issue. Please let me know how can I fix this error. Below is the code.

public class CraiWSCall {

    //This is used to fetch access cookie based token from Crai WS
    
    public String getAuthToken()
    {
        String cookie = '';
        String access_token = '';
        CraiCredentials__c obj = CraiCredentials__c.getValues('TokenEndpoint');
        HttpRequest req = new HttpRequest();  
        req.setMethod('POST');
        req.setEndpoint(obj.Endpoint__c + '?username=test&password=test');
        req.setHeader('Content-type', 'text/plain');
        req.setHeader('Content-length', '27');
        //req.setHeader('Host', 'puntivendita.crai.org');
        // req.setHeader('Proxy-Connection', 'Keep-Alive');
        // req.setTimeout();
        Http httpObj = new Http();
        HTTPResponse res = new HttpResponse();
        res  = httpObj.send(req); 
        System.debug('Error Messeg:' + res.getStatus());
         return cookie;
    }
}
 
HI All,

 I am executing below code. 

global class CustomIterable implements Iterator<Object>{ 
   List<Account> accs {get; set;} 
   List<Object> listOfAccounts {get; set;}
   Integer i {get; set;} 
   global CustomIterable(){ 
       string s = '{"d":[{"Sede":"Crai","Cedi":"A.R.Ce.V","CodECR":"0010001","IDPV":1,"IDNielsen":"","CodAgente":"","RagSoc":"Punto vend. 1","PartitaIVA":""},{"Sede":"Crai","Cedi":"A.R.Ce.V","CodECR":"0010001","IDPV":1,"IDNielsen":"","CodAgente":"","RagSoc":"Punto vend. 1","PartitaIVA":""}]}';
       Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s);
       List<Object> listOfAccounts = (List<Object>) m.get('d');
       System.debug('Size Of Account oBjects:' + listOfAccounts.size());
       accs = [SELECT id, name, numberofEmployees FROM Account LIMIT 10]; 
       //System.debug('size of Total Account List:' + accs.size());
       i = 0; 
   }   
   global boolean hasNext(){ 
       System.debug('Size of ListOfAccounts:' + listOfAccounts.size());
       if(i >= listOfAccounts.size()) 
           return false; 
       else 
           return true; 
   }    
   global Object next(){ 
       i=i+1; 
       return listOfAccounts[i-1]; 
   } 
}

I am getting the below error on line System.debug('Size of ListOfAccounts:' + listOfAccounts.size());
FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

I cab get the size of list listOfAccounts in cinstructor. However, I am getting the error on mentioned line.

Please help me resolve this query.

Thanks,
Nikhil Khandare