function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Simon234Simon234 

Apex Callout from one Org to another with method GET

Org 2 has GET Method:
@RestResource(urlMapping='/jobShow/*')
global with sharing class RestJob {

    @HttpGet
    global static List<Job__c> getJob(){
        List<Job__c> jobList;
        
        try{
            jobList = [SELECT Description__c 
                          FROM Job__c];  
        } catch(Exception e){
            System.debug('Error: ' + e.getMessage());
        }
        return jobList;     
    } 
}
Org 1 has Remote Site with link to Org 2: https://***.lightning.force.com

Org 1 also has a Callout Method:
public class HttpCalloutJob {
    
    public String getCalloutResponseContents(String url) {
    Http h = new Http();
    HttpRequest req = new HttpRequest();
req.setEndpoint('https://***.lightning.force.com/services/apexrest/jobShow');  //I don't know: is it right Link or not?
    req.setMethod('GET');

    HttpResponse res = h.send(req);   
    System.debug('Body: ' + res.getBody());
    return res.getBody();     
  }
}
Then I try to Execute this part
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://***.lightning.force.com/services/apexrest/jobShow');
req.setMethod('GET');
HttpResponse res = h.send(req);      
System.debug('Body: ' + res.getBody());
in Execute Anonymous Window, and become empty Body. But from workbench all is ok: it's not empty.

I think my link is wrong or I didn't connect my Orgs. How can I fix it?
v varaprasadv varaprasad
Hi Simon,

Directly we cannot connect to other org.To connect first we need to authentication with Oauth.

Steps to get Accestoken : 

Create one connected app it will contain client id ,client secret and callback URL.

Ex : 

http://salesforceprasad.blogspot.com/2017/04/how-to-test-salesforce-rest-api.html

Once u receive access token then you need the pass that token in your class
 
String authorizationHeader = 'Bearer accestoken';//add here received accestoken after bearer


Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://***.lightning.force.com/services/apexrest/jobShow');
req.setMethod('GET');
//add below 2 lines
req.setHeader('ContentType', 'Application/json')
req.setHeader('Authorization', authorizationHeader);

HttpResponse res = h.send(req);      
System.debug('Body: ' + res.getBody());


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1



 
v varaprasadv varaprasad
Try below code to generate accetoke : 
 
String reqbody = 'grant_type=password&client_id='+Consumer_Key+'&client_secret='+Consumer_Secret+'&username='+UserName+'&password='+Password_SecurityToken;
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setBody(reqbody);
        req.setMethod('POST');
        req.setEndpoint('https://ap2.salesforce.com/services/oauth2/token');
        HttpResponse res = h.send(req);
     // OAuth2 Details after remote login to target
        system.debug('==res=='+res.getBody);

Note : if we excute above code in anonymous window it will show null on responce.

​Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
Raj VakatiRaj Vakati
As you are executing with in the salesforce .. you no need any connected app or consumer key and secret key 

try below code 
iit will work 
 
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint( System.URL.getSalesforceBaseUrl().toExternalForm()+'/services/apexrest/jobShow');
req.setHeader('Authorization',  'Bearer ' + UserInfo.getSessionId());

req.setMethod('GET');

HttpResponse res = h.send(req);      
System.debug('Body: ' + res.getBody());



 
Sachin HoodaSachin Hooda
Hi Raj Vakati,
No offense but I'm little unclear about it.
Because it's still two different ORGs, how you gonna prove the authorization to the other ORG?
Like the Session-Id is generated with credentials of another ORG and being passed to another. How the ORG decides whether you've got bucks in your pocket and should be allowed or it shouldn't be.
Or if it is allowed, can't we access any other org with SF login or test URL?