0

I'm making a SF org where i need to retrieve data from a 2nd SF org. I have created Connected App in my Source Org, and i have created Auth. Provider and Named Credential in the Portal Org (that the users are gonna use).

In the Portal Org, i have this Apex code:

public with sharing class AuthCallout {
 
    public static Http http = new Http();
    public static HTTPResponse response;
    public static HttpRequest request;
 
    public static void getContact(Id conId) {
 
        request = new HttpRequest();
        request.setMethod('GET');
        request.setEndpoint('callout:DataOrgAPI/services/apexrest/Contact/' + conId);
 
        response = http.send(request); 

        System.debug(response.getBody());
    }

The Data Org has this Apex Code:

@RestResource(urlMapping='/Contact/*')
global with sharing class ContactAPI {
  
    @HttpGet
    global static Contact doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String conId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Contact result = [SELECT Id, Name FROM Contact WHERE Id = :conId];
        return result;
    }
}

To test to see if i can find the data in the Source Org, I've tried to run this in Anonymous Window.

AuthCallout.getContact('0037S00000CF66fQAD');

Currently getting StatusCode 302 Code in the response.

But I'm not able to find Contacts in the Data Org, only in my Portal Org. I also retrieve nothing from response.getBody().

Any ideas on how i can find the values in the Data Org?

2 Answers 2

0

The Id you have (for a given record) is parctically guaranteed to be different between orgs.
I.e. you can't use the Id from your portal org to query in your data org

You need to do two things here:

  1. Ensure that there is a piece of data that is the same in both orgs (e.g. the Contact's Email, or a custom field that you can mark as an "external Id")
  2. Adjust your callout (portal org) and Apex Rest Resource (data org) to query using that instead of the Id
4
  • Additional context: Ids for setup objects (users, record types, profiles, etc...) will be the same between production orgs and sandbox orgs provided that the sandbox was created or refreshed after that data became available in the production org. Full and partial copy sandboxes also will have matching Ids for records of non-setup objects (Account, Contact, Opportunity, etc...). It's important to note that this is between a production org and sandboxes. If you see a record with the same Id (and for the same SObject) between two production orgs, you may want to buy several lottery tickets.
    – Derek F
    Commented Oct 31, 2022 at 13:39
  • Thank you for the response Derek! I have changed from Id to Email now. So method is getContact(String conEmail) and variables are conEmail of course, in the Portal Org. In the Source Org i have changed the variables aswell + the query to search for a Email. However, i'm still getting StatusCode=302. I've searched for '[email protected]' as it is in both standard Contacts email in the Dev Ed org from Trailhead.
    – JonasH
    Commented Oct 31, 2022 at 14:15
  • @JonasH that status code would be important information to include in your question. 302 = your URL isn't wrong, but it's also not right. You should also be getting the appropriate URL from that response (in the location header).
    – Derek F
    Commented Oct 31, 2022 at 14:20
  • Yes, i see i forgot to add that, sorry about that. I cant seem to find location header that you're talking about. But i see the Named Cred. in debug gives me the correct URL in the Named Cred. Request. I'll also add what i get from the Named Cred Response: 'NAMED_CREDENTIAL_RESPONSE NamedCallout[Named Credential Id=0XA7Q000000wmMA, Named Credential Name=DataOrgAPI, Status Code=302, Content-Type=null, Response Size bytes=0, Overall Callout Time ms=73, Connect Time ms=1'. Not sure if this helps alot, as this is my first go at Named Cred etc with two different SF orgs.
    – JonasH
    Commented Oct 31, 2022 at 14:34
0

I've found the issue.

As im using the orgs given by Trailhead, they have suffix xx.trailblaze.lightning.force.com. However, the Auth.Provider gave me a Callback URL with the suffix xx.trailblaze.my.salesforce.com. Having fixed this, i'm now getting StatusCode=200!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .