About this question
I am trying to use REST in posting/inserting List of Account records from an origin org (OrgA) into a target org (OrgB). I have this error of "[45]|DEBUG|RESPONSE_BODY : System.HttpResponse[Status=Method Not Allowed, StatusCode=405]" which failing on the line where i do : HttpResponse res1 = http2.send(req1); How can I solve this statuscode 405 issue? In my origin org I have the following code:
public class submitDataToTargetOrg { public static Map oauthCustomSettings = Utils.getCustomSettingsKeyValueMapFull('Org_Credentials'); public static List oauthCustomSettingsValues = oauthCustomSettings.values(); public static String clientId = oauthCustomSettingsValues.get(0).subStringBetween('|ClientId : |', '|ClientId|'); public static String clientSecret = oauthCustomSettingsValues.get(0).subStringBetween('|ClientSec : |', '|ClientSec|'); public static String userName= oauthCustomSettingsValues.get(0).subStringBetween('|UserName : |', '|UserName|'); public static String password= oauthCustomSettingsValues.get(0).subStringBetween('|Password : |', '|Password|'); public static void submitDataUsingGet(List jsonToSubmit) { public static void submitDataUsingGet(List jsonToSubmit) { List accList = (List)jsonToSubmit; String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+userName+'&password='+password; Http http = new Http(); HttpRequest req = new HttpRequest(); req.setBody(reqbody); req.setMethod('POST'); req.setEndpoint('https://login.salesforce.com/services/oauth2/token'); HttpResponse res = http.send(req); OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class); if(objAuthenticationInfo.access_token!=null){ String requestBody = '{"req":{"accounts":'+JSON.serialize(accList)+'}}'; String requestBody = JSON.serialize(postBody); Http http2 = new Http(); HttpRequest req1 = new HttpRequest(); string endP = 'https://eu6.salesforce.com/services/apexrest/getAccountData';//+jsonToSubmit; req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token); req1.setHeader('Content-Type','application/json'); req1.setMethod('POST'); req1.setBody(requestBody); req1.setEndpoint(endP); HttpResponse res1 = http2.send(req1); //List deserializedAccounts = (List)JSON.deserialize(jsonToSubmit, List.class); //if (deserializedAccounts.size() > 0) Database.delete([SELECT Id FROM Account WHERE Id IN: deserializedAccounts]); } } In the target org I have the following:
@RestResource(urlmapping = '/getAccountData/*') global class accountRestService { PRIVATE STATIC FINAL BOOLEAN LOG_THIS_CLASS = TRUE; global class requestBody { global List accounts; } @httpPOST global static List populateAccountObject(accountRestService.requestBody req){ //List deserializedAccounts = (List)JSON.deserialize(req.accounts, List.class); if (LOG_THIS_CLASS) System.debug('the request in populateAccountObject.. : ' + req); insert req.accounts; //String AccId = RestContext.request.params.get('AccountId'); return req.accounts; } }