2

I'm trying to send data from one SFDC org to another org via Rest API. I'm properly able to create Rest Resource in 'receiving org' in this way :

@httpPost
global static string DepositAmount(string Accountid, integer amount, date depositDate)
{  
 // ----My code doing processing
}

'Sending org' is sending data entered by user via VF page(using custom controller). As mentioned, user in inputting data in 3 fields of types :

1. Text
2. Number
3. Date

Now I'm able to make successful Rest API call if I send 'hardcoded' values to 'receiving org for above 3 fields in this way :

String strbody = '{  "AccountId" : "0019000001Gchjb", "depositAmount" : "4555", "depositDate" :"06/01/2014" }'
 req.setBody(strbody);

But I'm unsure How to set input Fields as request body for the API call ? I was thinking to try JSON serialization using Maps

Map<String,String> postBody = new Map<String,String>();

But Maps accepts data of one type. Here I've 3 types of Data . If I convert my data into string, my receiving org starts giving error as it accepts 3 values of different types( string, number, date)

How could I proceed to set body in receiving org ?

1

JSON doesn't support all data types that are available in APEX. For an example there is no Date type in JSON. So I'd recommend you changing your method to accept 1 parameter of type String, which will be the JSON string itself. Then you can parse it and convert the JSON types to APEX accordingly. As you mentioned, I'd use a map and serialise/deserialise in order to pass the JSON string across:

Sending org

Id accountId = account.Id; // assume you have an instance of the account record
Decimal amount = 123.23; // input field value
Date depositDate = system.today(); // assumption agian

Map <String, String> jsonMap = new Map <String, String> ();
jsonmap.put('AccountId', account.Id); 
jsonmap.put('depositAmount', String.valueOf(amount));
jsonmap.put('depositDate', String.valueOf(depositDate));

String strbody = JSON.serialize(jsonMap);
req.setBody(strbody);

Receiving org

global static string DepositAmount(string jsonString)
{
    Map<String, Object> jsonRes = (Map<String, Object>)JSON.deserializeUntyped(jsonString);
    Id accountId = (Id)jsonRes.get('AccountId');
    Decimal amount = Decimal.valueOf((String)jsonRes.get('depositAmount'));
    Date depositDate = Date.valueOf((String)jsonRes.get('depositDate'));
}
  • 1
    Oh! Failed to apply my thinking in that direction! This seems like logical input.Thanks for that. Will try to apply it in my org and mark it as solution, if I don't come across any significant issue. – curious_techy Jun 29 '15 at 6:52
  • Hi Bachovski - I tried exactly what you've mentioned but in response in 'sending org', I'm getting this error : RESPONSE_BODY[{"message":"Unexpected parameter encountered during deserialization: depositDate at [line:1, column:17]","errorCode":"JSON_PARSER_ERROR"}] .What it is not able to deserialize depostiDate? Am I missing something ? – curious_techy Jun 30 '15 at 5:01

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.