1

Hope everyone is well. I was wondering if someone could provide some guidance on how to setup Named Credentials to connect two sandboxes together for using REST OAuth to post data from Sandbox A ---> Sandbox B

I believe the solution might be similar to the following post: Connect to Salesforce production REST API from Sandbox using Named Credentials

But I need some guidance to get started. I already created the resource class in target with the following:

@RestResource(urlMapping = '/propel/productSync/v1/item') global with sharing class PropelProductSyncItemResource { @HttpPost global static String doPost() { String message; String query; RestRequest request = RestContext.request; PDLM__Item__c item = (PDLM__Item__c)JSON.deserialize(request.requestBody.toString(), PDLM__Item__c.class); PDLM__Item__c itemToUpdate; try { //Retrieve matching Item record query = SysUtils.generateDynamicSOQLQuery('PDLM__Item__c') + ' WHERE Name = \'' + item.Name + '\' LIMIT 1'; itemToUpdate = (PDLM__Item__c)Database.query(query).get(0); System.debug(itemToUpdate); //Update matching Item record try { itemToUpdate.PDLM__Description__c = item.PDLM__Description__c; itemToUpdate.PDLM__Designated_Where_Used_Top__c = item.PDLM__Designated_Where_Used_Top__c; itemToUpdate.PDLM__Has_Quality__c = item.PDLM__Has_Quality__c; itemToUpdate.PDLM__Category_Name__c = item.PDLM__Category_Name__c; itemToUpdate.Legacy_Categories__c = item.Legacy_Categories__c; itemToUpdate.PDLM__Category_Control__c = item.PDLM__Category_Control__c; itemToUpdate.Prod_Beneficial_Owner__c = item.Prod_Beneficial_Owner__c; itemToUpdate.PDLM__Primary_Key__c = item.PDLM__Primary_Key__c; itemToUpdate.CurrencyIsoCode = item.CurrencyIsoCode; itemToUpdate.Legal_Owner__c = item.Legal_Owner__c; itemToUpdate.PDLM__Has_Open_Quality__c = item.PDLM__Has_Open_Quality__c; itemToUpdate.PDLM__Has_Parent__c = item.PDLM__Has_Parent__c; update itemToUpdate; message = 'Item ' + itemToUpdate.Name + ' updated successfully'; } catch (DMLException de) { System.debug('An error occured while updating matching item record(s): ' + de.getMessage()); message = de.getMessage(); } } catch (QueryException qe) { System.debug('An error occured while querying matching item record(s): ' + qe.getMessage()); message = qe.getMessage(); } return message; } } 

Now I have created a class I am using to test with the following code:

public class PropelProductSyncService { @future(Callout=true) public static void syncItem(Id publishingTableItemId) { try { Propel_Publishing_Table__c publishingTableItem = Database.query(SysUtils.generateDynamicSOQLQuery('Propel_Publishing_Table__c') + ' WHERE Id = \'' + publishingTableItemId + '\''); Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint(publishingTableItem.Endpoint__c); request.setMethod('POST'); request.setHeader('Content-Type', 'application/json;charset=UTF-8'); request.setBody(publishingTableItem.Payload__c); HttpResponse response = http.send(request); System.debug(response.getBody()); // Parse the JSON response if(response.getStatusCode() != 201) { System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus()); } else { System.debug(response.getBody()); } } catch (QueryException qe) { System.debug('An error occured while querying publishing table record(s): ' + qe.getMessage()); throw qe; } } } 

But when I execute this code I get the following response:

15:16:11:212 USER_DEBUG [22]|DEBUG|The status code returned was not expected: 302 Found 

I believe this is the result of authenticating to the other sandbox, just not sure what the first step would be hence the desire to use named credentials

Update As per the instructions provided by @BritishBoyinDC I have created a Connected App in Sanbox B with the following: Connected App

I have also created an External Credential in the same sandbox with the following: External Credential

Added Related Named Credentials and one Principal as per the suggessted: enter image description here

What else do I need to create in the target sandbox or is the above sufficient, if so, what else must I create or configure to then be able to make my callouts from Sandbox A

1
  • You will need to actually authenticate against the target org to push data. I'd recommend looking at the docs/Trailhead content for Connected Apps (which are set up in the target org), where the credentials for those will be used in the Named Credentials. Commented Aug 30, 2024 at 13:47

1 Answer 1

2

I think the easiest way now is to use the Client Credentials Flow. I reference it in this question\answer, but to summarize:

In the Target Org, you create a Connected App which includes the Access the Salesforce API Platform scope. You select the Enable OAuth Settings, and then check the Enable Client Credentials Flow. This will then enable you to generate a Client Id and Client Secret.

In the source Org, you create a External Credential, and select Client Credentials with Client Secret Flow as the Authentication Flow Type You can then add the Client Id and Client Secret from the Target Org as the Principals.

You then create a Named Credential that uses the External Credential, and set the URL to be the domain of the Target Org (e.g. https://mycustomdomain.my.salesforce.com You can then use that Named Credential in Apex or Flow, and it will invoke the rest api as you require

2
  • 1
    I updated my post as per your instructions, I think I might be missing some steps, could you kindly advise what else to do? Commented Aug 30, 2024 at 14:38
  • What is the value in request.setEndpoint(publishingTableItem.Endpoint__c)? A 302 suggest a url redirect - so I would check the endpoint, and also look in the response headers to see what info they have i.e. in the code that handles a not 201, debug using getHeaderKeys() and getHeader(key) to see if there is a redirect value or something Commented Aug 30, 2024 at 17:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.