+ Start a Discussion
Abraham kumarAbraham kumar 

Urgent POST method creation in JSON format

i just need to do a POST request containing all the contact data fields whenever a user is created or updated on Salesforce and sent to our client website database. 

Please help me with class and POST method to achieve this,code on how this can be done.
Im a bit new with integration, any help highly appreciated. Please help me complete this on what needs to be done.

It is just the post method please help.

Many Thanks
Abraham
Best Answer chosen by Abraham kumar
Andy BoettcherAndy Boettcher
You'll either need to change your callout code approach to query the fields on a record and then use JSON.serialize(your record) and append it to the body of your callout, which is the quickest and easiest way - or you can create an inner class if you want a little more control over what you want the JSON to look like.

All Answers

Andy BoettcherAndy Boettcher

Could you perhaps use Workflow's "Outbound Message"?  It's not a POST but if you have more control over your client website database perhaps you could change that end to make it easier?

Otherwise you'll be looking at an APEX trigger that will fire off an @Future method to do a callout.  

Abraham kumarAbraham kumar
Hi Andy,

Can you pls tell send me the code for this  trigger that will fire off an @Future method to do a callout. or maybe an sample or some reference Please. Thanks!!

Many Thanksfor your help in advance!!
Abraham
Abraham kumarAbraham kumar
Hi Andy,

I think the outbound is a great idea, do you think when they can accept Json they can also accept xml Right?.

Although the Doc says Json
What do you think.?

Please let me know. Thanks!!

Abraham

 
Andy BoettcherAndy Boettcher

JSON and XML are two very different formats - if they only accept JSON then the Outbound Message idea won't work. =(

What you'll do is to create a trigger on the Contact object and pass the trigger.newMap.keyset() to another class annotated with @Future - check this link for the @Future method:  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_future.htm

Within the @Future class, you'll construct your JSON object - reference:  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_json_overview.htm

Finally, you'll assemble and transmit your JSON object to the REST endpoint - reference:  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http.htm

Start there - if you get a few classes up and humming and have problems, please come back here and post your code for review.

Good luck!

Abraham kumarAbraham kumar
Thank you soo soo much Andy.. Much appreciated

Let me start up right away... Hope ull help me if im stuck somewhere :)...

Many Thanks!!
Abraham
Abraham kumarAbraham kumar
Hi Andy,

This is my trigger
trigger Contactcallout on Contact (after update) {
Map<Id, String> m = new Map<Id, String>();
for (contact c : Trigger.new) {

        contact old = Trigger.oldMap.get(c.Id);
 if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.Title!=old.Title) {
 WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.Title);
            
        }
    }
    
    }

And this is my class:-
 
public class WebServiceCallout {

    @future (callout=true)
    public static void sendNotification(string Id,string Email,string First_Name,string Last_Name,string Title) {

        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

        req.setEndpoint('http://requestb.in/zdpbiwzd');
        req.setMethod('POST');
        req.setBody('Id='+EncodingUtil.urlEncode(Id, 'UTF-8')+'&Email='+EncodingUtil.urlEncode(Email, 'UTF-8')+'&First_Name='+EncodingUtil.urlEncode(First_Name, 'UTF-8')+'&Last_Name='+EncodingUtil.urlEncode(Last_Name, 'UTF-8')+'&Title='+EncodingUtil.urlEncode(Title, 'UTF-8'));
        req.setCompressed(true); // otherwise we hit a limit of 32000

        try {
            res = http.send(req);
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
        }

    }

    // run WebServiceCallout.testMe(); from Execute Anonymous to test
   // public static testMethod void testMe() {
      //  WebServiceCallout.sendNotification('My Test Customer','My City');
    }

Please can you let me know how i can test the output of this.

Im using the requestbin to get the results but im not getting any output. Please can you help me with what is my mistake. thanks a Ton!! Awaiting your response.

Thanks
Abraham

 
Abraham kumarAbraham kumar
Hi Andy,

Please let me know if this code is fine. The logic is like any changes made to the email or firstname or lastname or title fields in salesforce should send all these fields along with the contact id to the external system. Im not getting any errors Im hoping its fine . only thing is i just dont know how to test it to see the output.

please help me. thanks!!

Thanks
Abraham 
Andy BoettcherAndy Boettcher
Check your debug logs to see the status of your callout.  You'll also need to create an entry for "http://requestb.in" in Remote Sites to allow Salesforce to make callouts to that endpoint.
Abraham kumarAbraham kumar
Hi Andy,

Im soo soo happy to hear from you.

This is the error in debug log.
33.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
13:31:18.197 (197741167)|EXECUTION_STARTED
13:31:18.197 (197782312)|CODE_UNIT_STARTED|[EXTERNAL]|FutureHandler - state load
13:31:18.214 (214476904)|CODE_UNIT_FINISHED|FutureHandler - state load
13:31:18.219 (219410070)|EXECUTION_FINISHED
13:31:18.268 (268879588)|EXECUTION_STARTED
13:31:18.268 (268890335)|CODE_UNIT_STARTED|[EXTERNAL]|01pL0000000LPlP|WebServiceCallout.sendNotification
13:31:18.269 (269658181)|METHOD_ENTRY|[1]|01pL0000000LPlP|WebServiceCallout.WebServiceCallout()
13:31:18.269 (269667761)|METHOD_EXIT|[1]|WebServiceCallout
13:31:18.269 (269771382)|SYSTEM_METHOD_ENTRY|[9]|System.HttpRequest.setTimeout(Integer)
13:31:18.269 (269819755)|SYSTEM_METHOD_EXIT|[9]|System.HttpRequest.setTimeout(Integer)
13:31:18.269 (269848035)|SYSTEM_METHOD_ENTRY|[10]|System.HttpRequest.setEndpoint(String)
13:31:18.269 (269867618)|SYSTEM_METHOD_EXIT|[10]|System.HttpRequest.setEndpoint(String)
13:31:18.269 (269888299)|SYSTEM_METHOD_ENTRY|[11]|System.HttpRequest.setMethod(String)
13:31:18.269 (269905767)|SYSTEM_METHOD_EXIT|[11]|System.HttpRequest.setMethod(String)
13:31:18.269 (269982188)|SYSTEM_METHOD_ENTRY|[12]|system.EncodingUtil.urlEncode(String, String)
13:31:18.270 (270051599)|SYSTEM_METHOD_EXIT|[12]|system.EncodingUtil.urlEncode(String, String)
13:31:18.270 (270063602)|SYSTEM_METHOD_ENTRY|[12]|system.EncodingUtil.urlEncode(String, String)
13:31:18.270 (270084384)|SYSTEM_METHOD_EXIT|[12]|system.EncodingUtil.urlEncode(String, String)
13:31:18.270 (270095272)|SYSTEM_METHOD_ENTRY|[12]|system.EncodingUtil.urlEncode(String, String)
13:31:18.270 (270111042)|SYSTEM_METHOD_EXIT|[12]|system.EncodingUtil.urlEncode(String, String)
13:31:18.270 (270130916)|SYSTEM_METHOD_ENTRY|[12]|system.EncodingUtil.urlEncode(String, String)
13:31:18.270 (270146312)|SYSTEM_METHOD_EXIT|[12]|system.EncodingUtil.urlEncode(String, String)
13:31:18.270 (270152736)|SYSTEM_METHOD_ENTRY|[12]|system.EncodingUtil.urlEncode(String, String)
13:31:18.270 (270241922)|SYSTEM_METHOD_EXIT|[12]|system.EncodingUtil.urlEncode(String, String)
13:31:18.270 (270302718)|FATAL_ERROR|System.NullPointerException: Argument cannot be null.

(System Code)
Class.WebServiceCallout.sendNotification: line 12, column 1
13:31:18.270 (270315286)|FATAL_ERROR|System.NullPointerException: Argument cannot be null.

(System Code)
Class.WebServiceCallout.sendNotification: line 12, column 1
13:31:19.007 (270327896)|CUMULATIVE_LIMIT_USAGE
13:31:19.007|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 200
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 60000
  Maximum heap size: 0 out of 12000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 1
  Number of Mobile Apex push calls: 0 out of 10

13:31:19.007|CUMULATIVE_LIMIT_USAGE_END

13:31:18.270 (270363684)|CODE_UNIT_FINISHED|WebServiceCallout.sendNotification
13:31:18.276 (276246223)|EXECUTION_FINISHED

pls let me know what is wrong here . Ive have added "http://requestb.in" to remote url.

Many Thanks
Abraham
Andy BoettcherAndy Boettcher
You're having an "Argument cannot be null" error - one of the variables you're encoding is null.

You may want to wrap those encoding functions with another method that returns an empty string or the value.
Abraham kumarAbraham kumar
Hi Andy,
Ur Amazing.. :)
Got the exact result i want..
Just one little thing it shows results as below:- 

FORM/POST PARAMETERS

Id: 003L000000aeUZ5IAM
Email: integration@gmail.com
Last_Name: Int
First_Name: Test323

RAW BODYId=003L000000aeUZ5IAM&Email=integration%40gmail.com&First_Name=Test323&Last_Name=Int

Our client website database asked told me that it should be in JSON format so this would go as JSON there right. as i see the above results does not come in json format.. just bit concerned on that..

Many Thanks for making this happen
Abraham
Andy BoettcherAndy Boettcher
You'll either need to change your callout code approach to query the fields on a record and then use JSON.serialize(your record) and append it to the body of your callout, which is the quickest and easiest way - or you can create an inner class if you want a little more control over what you want the JSON to look like.
This was selected as the best answer