I need to post created record from Org1 to Org2. I want to "put" my JSON into Map and just "open" Map in another Org. Needed result: fields from new Org1 Object's record = fields from new Org2 Object's record. How can I do that?

Callout from Org1:

public static void postCalloutResponseContents() {           
Settings__c settings = [SELECT ConsumerKey__c, ClientSecret__c, Username__c, Password__c, SecurityToken__c
                        FROM Settings__c
                        WHERE Name = 'OurSettings'];

String consumerKey = settings.ConsumerKey__c;
String consumerSecret = settings.ClientSecret__c;
String username = settings.Username__c;
String password = settings.Password__c + settings.SecurityToken__c;
String request = 'grant_type=password&client_id=' + consumerKey +'&client_secret=' + consumerSecret +
                 '&username=' + username + '&password='+password;

Http ourHttp = new Http();
HttpRequest ourRequest = new HttpRequest();
ourRequest.setEndpoint(System.Label.Job_Advertisement_URL + '/services/oauth2/token');

Position__c pos = [SELECT Name, Description__c FROM Position__c WHERE Name = 'Title22'];

HttpResponse response = ourHttp.send(ourRequest);        
OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(response.getbody(), OAuth2.class);

if(objAuthenticationInfo.ACCESS_TOKEN != null){

    JSONGenerator gen = JSON.createGenerator(true);    
    gen.writeStringField('title', pos.Name);
    gen.writeStringField('description', pos.Job_Description__c);
    String jsonString = gen.getAsString();
    System.debug('jsonMaterials: ' + jsonString);

    Http finalHttp = new Http();
    HttpRequest finalRequest = new HttpRequest();

    finalRequest.setHeader('Authorization','Bearer '+ objAuthenticationInfo.ACCESS_TOKEN);

    finalRequest.setEndpoint(System.Label.Job_Advertisement_URL + '/services/apexrest/jobAdvertisementShow');
    HttpResponse finalResponse = finalHttp.send(finalRequest);
    System.debug('RESPONSE BODY: '+ finalResponse.getBody());

public class OAuth2{
    public String ACCESS_TOKEN{get;set;}    

Post service from Org2:

global static Job__c postJob(String title, String description){
    Job__c thisJob = new Job__c(
        Title__c = title,
        Description__c = description

        insert thisJob;
    } catch(Exception e){
        System.debug('Error: ' + e.getMessage());
    return thisJob;
  • You can also email from source org and in the target org you can write a inbound email handler for this, not sure how secure it's. – itzmukeshy7 Oct 3 '18 at 14:37
  • It's not about email. We create the same object in Org2, while create in Org1. – MichaelLev19 Oct 3 '18 at 14:40

You can utilize a Map of values in your POST method. Your method will look something as below:

global static void myRESTMethod(Map<String, String> mapOfValues) {

    String value;
    for(String key : mapOfValues.keySet()) {
        value = mapOfValues.get(key);  
        //  all my other code
    // all my other code

Then you construct the JSON in the request body when calling your API as below to be able to be processed by your POST method. Note the name of the outermost attribute here, that should map to the parameter name on your POST method.

    "mapOfValues" : {
                "key1" : "Value for key1",
                "key2" : "Value for key2",
                "key3" : "Value for key3"

For more details around data types you can use with a custom Apex REST API, refer to the documentation.

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.