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