+ Start a Discussion
RICARDO PALMARICARDO PALMA 

Sending JSON parameters.

Hi,
I'm doing an integration with a third party and I have to send the accid to the third party to get some information back. I got the result when I type the accid, but I what I really need to do is set up a params as variable not a literal.

Here is what I want to do:
This doesn't work.
Just for testing I'm setting up manually the accid but this value is going to be the accid on the opportunity. The values is going to different base on the opportunity.

string accid = "CtjSFlLYIls4-gAe";
string s = '{"jsonrpc": "2.0","id": 1,"method": "acct.phone.get","params": [accid]}';

I got an error: {"jsonrpc":"2.0","error":{"message":"invalid JSON in request body","code":-32700}}

It looks like I'm not using the right JSON format to set a parameter. 

But when I type the id on the params I got the information back.
string s = '{"jsonrpc": "2.0","id": 1,"method": "acct.phone.get","params": ["CtjSFlLYIls4-gAe"]}'; 

I hope someone can help me.
Thanks.
 
BalajiRanganathanBalajiRanganathan
it should be simple. you have to use String concatenation. for example

String a = 'cd';
String f = 'b' + a + 'e'; //now f will have bcde

so for your case.

string accid = "CtjSFlLYIls4-gAe";
string s = '{"jsonrpc": "2.0","id": 1,"method": "acct.phone.get","params": [' + accid + ']}';
RICARDO PALMARICARDO PALMA
Thanks Balaji,
It works great!!!
Sumit Arora 9Sumit Arora 9
Hi Ricardo,

Are you able to invoke json-rpc from Salesforce? Can you share some details pls

Regards
Sumit
RICARDO PALMARICARDO PALMA
Hi Sumit, Sorry, I was on vacation. Here is what I did. HttpRequest req = new HttpRequest(); req.setEndpoint('here your url to connect to the third party'); //*HERE IS WHERE I SET UP THE USER AUTHENTICATION HEADER*// req.setHeader('Content-Type', 'application/json'); req.setHeader('Accept', 'application/json'); req.setMethod('POST'); String email = 'her you user name or email'; String password = 'here your password'; Blob headerValue = Blob.valueOf(email + ':' + password); String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue); req.setHeader('Authorization', authorizationHeader); //*HERE IS WHERE I SET UP THE USER AUTHENTICATION HEADER*// string s = '{"jsonrpc": "2.0","id": 1,"method": "here your method name","params": ["CtjSFlLYIls4-gAe"]}'; req.setbody(s); Http httpLocal = new Http(); HTTPResponse resLocal = httpLocal.send(req); String BodyLocal = resLocal.getBody(); The BodyLocal will return the result from json rpc I hope this helps. Regards. *Ricardo Alberto Palma|* *Salesforce Developer* *|* *Dominion Enterprises* *6500 International Parkway, Ste 1000* *|* *Plano, TX 75093* *214-273-2103* (o) *|* *210-787-9353* (c) *|* *ricardo.palma@dominionenterprises.com *
Sumit Arora 9Sumit Arora 9
Hi Ricardo,

Thank you for your reply. We have a different issue. We are trying to do JSON RPC over TCP.

Regards
Sumit