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: 
I have also created an External Credential in the same sandbox with the following: 
Added Related Named Credentials and one Principal as per the suggessted: 
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