Hi Guys,
So this time it was quite a long time. Anyways lets start with integration part this time . Let me just walk you through scenario.
Scenario:
I have a requirement that I want to integrate some website with salesforce. thougfh it was a just one way integration. I just have to bring some data into the salesforce. Obviously what comes into your mind is Http callout!! Right :)
Lets get started ...
As I have explained above that we want to fetch data from one website into salesforce. So i tried the hhtp callout to their exposed API. As expected I should get the response as status code = ok and 200 . But I got status=Moved Temporarily, StatusCode=302. Then I started looking for solution. I got many but one which I found was very easy. So , i attempted the one and luckily it worked for me.
Why does this happen?
We get this response because we are hitting the wrong api. It gets changes with location . So we need to find the same . The location of server gets changed.
Solution:
We have to find the location which comes with header. So we need to get location from header and we need to write handler for it.
here is the code for you .
Now, just try to get through this code. It will work for you too.
happy coding !!
Thanks
So this time it was quite a long time. Anyways lets start with integration part this time . Let me just walk you through scenario.
Scenario:
I have a requirement that I want to integrate some website with salesforce. thougfh it was a just one way integration. I just have to bring some data into the salesforce. Obviously what comes into your mind is Http callout!! Right :)
Lets get started ...
As I have explained above that we want to fetch data from one website into salesforce. So i tried the hhtp callout to their exposed API. As expected I should get the response as status code = ok and 200 . But I got status=Moved Temporarily, StatusCode=302. Then I started looking for solution. I got many but one which I found was very easy. So , i attempted the one and luckily it worked for me.
Why does this happen?
We get this response because we are hitting the wrong api. It gets changes with location . So we need to find the same . The location of server gets changed.
Solution:
We have to find the location which comes with header. So we need to get location from header and we need to write handler for it.
here is the code for you .
if(res.getStatusCode() >=300 && res.getStatusCode() <= 307 && res.getStatusCode() != 306) {
do {
redirect = false; // reset the value each time
String loc = res.getHeader('Location'); // get location of the redirect
system.debug('loc===='+loc);
if(loc == null) {
redirect = false;
continue;
}
req = new HttpRequest();
req.setEndpoint(loc);
req.setMethod('GET');
res = http.send(req);
system.debug('========='+res.getbody());
// Parse entire JSON response.
JSONParser parser = JSON.createParser(res.getbody());
while(parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
root= (RootObject)parser.readValueAs(RootObject.class);
system.debug('data =====4'+root.items);
}
if(root==null){
ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.INFO,'No Records Found');
ApexPages.addmessage(msg);
}
}
Now, just try to get through this code. It will work for you too.
happy coding !!
Thanks
No comments:
Post a Comment