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 issue? In my origin org I have the following code:
public class submitDataToTargetOrg {
public static Map<String, String> oauthCustomSettings = Utils.getCustomSettingsKeyValueMapFull('Org_Credentials');
public static List<String> 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<SObject> jsonToSubmit) {
public static void submitDataUsingGet(List<SObject> jsonToSubmit) {
List<Account> accList = (List<Account>)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<Account> deserializedAccounts = (List<Account>)JSON.deserialize(jsonToSubmit, List<Account>.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<Account> accounts;
}
@httpPOST
global static List<Account> populateAccountObject(accountRestService.requestBody req){
//List<Account> deserializedAccounts = (List<Account>)JSON.deserialize(req.accounts, List<Account>.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;
}
}