0

I have two Salesforce orgs connected and I am trying to pass a created Opportunity from one to the other using Apex REST callouts. Everything seems to be connected properly because when I test with a "GET" method, I get a successful HTTP code (200).

The receiving org's code is very simple (still in the proof of concept stage so not much here just to see if it passes through correctly).

@HttpPost
global static ID postOpp(String name) {
    Opportunity o = new Opportunity(
    name = name);

    o.closedate = date.newInstance(2020,10,10);
    o.stagename = 'Qualification';
    o.accountID = '0015x000026OM6g';       
    
    insert o;
    return o.Id;
}

When I use workbench, and test a POST request in this org, I see it is successful and the opp is created. Now I am trying to send an opp from my other Salesforce org. I've tried setting the body a few different ways but nothing seems to be working.

    Http http = new Http();
    HttpResponse response = new HttpResponse();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('callout:HS_Integration');
    request.setMethod('POST');
    
    JSONGenerator j = JSON.createGenerator(true);
    j.writeStartObject();
    j.writeStringField('Name', 'test cm 4');
    j.writeEndObject();
    String jsonS = j.getAsString();
    
    request.setBody(jsonS);
    
    response = http.send(request);

I don't think anything is wrong with my authorization or named credentials because of the successful HTTP code. As you can see I try using setBody() with a basic JSON string, I've also tried it with a much simpler string I saw some people use ('name=TestOpp'), but the opp is not getting created in the target org. How am I supposed to use setBody to pass over information correctly? Can someone explain what I may be missing?

Thanks

0

When using automatic parameters, there's a top-level "request" object you need to pass in. Your code should look like:

String body = JSON.serialize(
  new Map<String, Object> {
    'request' => new Map<String, Object> {
      'name' => 'test cm 4'
    }
  }
);
request.setBody(body);

Also note that the parameters are case-sensitive; you cannot use Name instead of name.

The payload should look like:

{ "request": { "name": "test cm 4" } }

You may also find the documentation helpful.

6
  • Thanks, I tried your code snippet instead of what I had for the body and I still am getting a 200 return code but I am not seeing the opportunity created in my org. If that json body is in the right format do you have any other ideas on what could be missing?
    – user88797
    Oct 2 '20 at 13:56
  • I'm starting to think the issue isn't with my body but with my named credential. Right now my credential URL is 'myorg.salesforce.com' and my callout is directly to that, but do I need to append something to my request.setEndpoint() to point directly to the Opportunity service? I've tried request.setEndpoint('callout:HS_Integration/services/apexrest'); but that didn't work either, I got an invalid session error. clearly I am connected to my target org in some way but maybe I am not pointing to the opportunity services. Any idea?
    – user88797
    Oct 2 '20 at 15:02
  • @user88797 It would be callout:HS_Integration/services/apexrest/urlFragment (where urlFragment is the urlResource='/urlFragment' in the RestResource annotation). P.S. Why not just use the normal Salesforce REST API at this point?
    – sfdcfox
    Oct 2 '20 at 17:16
  • Hi sfdcfox, In my target orgs RestResource class I defined it as just this: "@RestResource(urlMapping='/Opportunity/*')" but when I change my endpoint to "request.setEndpoint('callout:HS_Integration/services/apexrest/Opportunity');" I get this error: "14:21:46:327 USER_DEBUG [38]|DEBUG|[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]" But when I omit everything after HS_integration I get a successful HTTP code. any idea why?
    – user88797
    Oct 2 '20 at 19:22
  • @user88797 Because you're reaching the base login URL. If your session is invalid, check your OAuth settings on your Named Credential. You need to make sure you're connected.
    – sfdcfox
    Oct 2 '20 at 20:58

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.