1

1.) Instantiating the Account object 2.) Serializing the object into the JSON format 3.) Creating an HTTPRequest, with parameters, and body 4.) Sending the HTTPRequest 5.) Handling the response, as well as failures.

Sample request need to be send to API for the response as follows with Account Id and Record Type.

{"action":"read", "params":[ {"record_type":"consumer", "Accountid":"471856"} ] }

Here is the class that I have developed but I'm not sure whether I'm handling the record type correctly or not ?

public class AccountInformation {
    public String record_type;  
    public String AccountId;    

    public accoutninformation(string r,string id){
        record_type=r,
        AccountId=id
    }
    accountInformation ai = new accountInformation(acct.RecordType.Name, ''    acct.Account_Id__c);
    //Serialize the object to JSON format
    JSONString = JSON.serializePretty(ai);
    System.debug('Serialized Account: ' + JSONString);
    HttpRequest req = new HttpRequest();
    //Set HTTPRequest Method
    req.setMethod('POST');
    //Set HTTPRequest header proporties
    req.setHeader('NLAuth, nlauth_account: 11111, nlauth_email: [email protected], nlauth_signature: PASSWORD,nlauth_role: 3')
    req.setHeader('content-type', 'application/json');
    req.setHeader('Accept', 'application/json');
    //Set HTTPRequest Endpoint
    req.setEndpoint('YOUR ENDPOINT URL HERE');
    //Set HTTP Request Body
    req.setBody(JSONString);
    Http http = new Http();
    try{
        HTTPResponse res = http.send(req);
        //Helpful debug messages
        System.debug(res.toString());
        System.debug('STATUS:'+res.getStatus());
        System.debug('STATUS_CODE:'+res.getStatusCode());
        System.debug('Content: ' + res.getBody());
    }
}
4
  • In the future, please put a little more work into formatting. Your code does not have a catch block. Does it actually compile?
    – Adrian Larson
    Commented Jun 17, 2015 at 17:38
  • I'm getting error at this line JSONString = JSON.serializePretty(ai); Commented Jun 17, 2015 at 18:19
  • 2
    There are so many things wrong her, for starters the property acct is never declared. Do you really need to serialize the class or can you just create the string like {record_type: 'record type id',AccountId:'accounted'}
    – Eric
    Commented Jun 17, 2015 at 18:26
  • just need to create the string everytime I enter the AccountId on the vf page and sent as a request to get the response from API Commented Jun 17, 2015 at 18:34

1 Answer 1

1

Can you please post the error message which you are getting.

As far as I can see: this is a pseudo code which explains less about your actual problem which your are facing.

My understanding:

You want to send an http request to an end point (another salesforce org). You have whitelisted the End point in connected apps.

You are sending a serializable Json data which will be accepted and parsed at the other side and a response will be sent.

What we need more is ; the actual error and the debug value of the ai (if the error is coming on it ).

2
  • This a Netsuite API where I need to make a HTTP Call. The request to API should be in the form of following JSON string so that they can send the response based on the accountid I sent. I'm struck on framing this string to be sent. {"action":"read", "params":[ {"record_type":"consumer", "Accountid":"471856"} ] } Commented Jun 18, 2015 at 14:21
  • If I understand correctly, you have to build JSON string. You have 2 options: build it manually (just concatenate string) or create helper classes. In second scenario you need 2 classes: one with String named action and List<OtherClass> named params. The Other class needs to have 2 string members record_type and Accountid. Then you can check that Json serializer returns what you need.
    – mischczu
    Commented Nov 1, 2015 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .