1

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;
    }
}
4

May be because you are posting a POST request

i.e. req1.setMethod('POST');

And you have declared your service as PUT method

i.e. @httpPut

So you can try either by making

req1.setMethod('PUT');
or
@httpPost

Edit: Add the below changes as well to avoid getting 'Jason parse error'.

Update the JSON that you are sending in request body.

Remove These line;


Map<Id, Account> postBody = new Map<Id, Account>();
for(Account acc : (List<Account>)jsonToSubmit) postBody.put(acc.Id, acc);


String requestBody = JSON.serialize(postBody);

And add this instead;


List<Account> accList = (List<Account>)jsonToSubmit;
String requestBody = '{"req":{"accounts":'+JSON.serialize(accList)+'}}';

I referred this post.

  • I tried this but that didn't solve the problem – saint Apr 8 '16 at 19:31
  • @saint But now you might not be getting "Method Not Allowed". The error you are getting is because of the JSON body that you are setting. – Saumya Ranjan Satapathy Apr 9 '16 at 3:07
  • @saint I have updated my answer for JSON PARSER issue as well. Check and let me know whether it worked for you or not. – Saumya Ranjan Satapathy Apr 9 '16 at 3:50
  • thanks that fixed the 405 issue. but i get [Status=Bad Request, StatusCode=400] error. looking at the meaning, it shows that the session ID or OAuth token used has expired or is invalid. im not quite sure how that ties in with my code. I have the correct cliend_id and secret. could it be a problem with the callback i specified in my target org? thx – saint Apr 9 '16 at 12:04
  • Exactly @saint. You are getting that because the JSON you are sending in request body is incorrect. Did you try what I have mentioned in 'Edit' part? As that part is to deal with this [Status=Bad Request, StatusCode=400, "message": "Unexpected parameter encountered during deserialization: req1 at [line:1, column:10]", "errorCode": "JSON_PARSER_ERROR"] – Saumya Ranjan Satapathy Apr 9 '16 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.