8

I only found solutions that pass objects or primitiv datatypes to the REST API, so do I need to pass these to rebuild my sObject or is it possible to pass it as an sObject JSON? I tried to, but I can't easy handle it in my API.

@RestResource(urlMapping='/Log/*')
global with sharing class RESTLogController {

@HttpPost
    global static String doPost(Log__c log) {
        insert log;
        return log.Id;
    }

}
2
  • 3
    What problems were you seeing with this approach? The docs definitely say you can pass sObjects as parameters.
    – metadaddy
    Commented Apr 19, 2013 at 16:19
  • Can you post your code you tried ????? Commented Apr 19, 2013 at 17:58

3 Answers 3

7
{
"reqst": {
    "acct": {
        "Name": "Test Account 1",
        "AccountNumber": "00000001",
        "Site": "Site1",
        "Website": "www.site1.com"
    },
    "cons": [
        {
            "Name": "Test Contact1",
            "email": "[email protected]",
            "LastName": "TLName2"
        },
        {
            "Name": "Test Contact2",
            "email": "[email protected]",
            "LastName": "TLName3"
        },
        {
            "Name": "Test Contact3",
            "email": "[email protected]",
            "LastName": "TLName"
        }
    ]
   }
  }

Here is the REST API class

@RestResource(urlMapping='/DemoUrl/*')
global with sharing class MyRestResourcedemo{

 //User defined Class

  global class RequestWrapper{
   Account acct;
   Contact[] cons;
 }

 //Response Wrapper Class

  global class ResponseWrapper{           
   public String StatusCode;
   public String StatusMessage;
   public Account acct;
   public Contact[] cons;    
  }

  @HttpPost
  global static ResponseWrapper doPost(RequestWrapper reqst){

   ResponseWrapper resp = new ResponseWrapper();

  try{
     insert reqst.acct;
      for(Contact c:reqst.cons){
           c.AccountId = reqst.acct.Id;
     }
   Upsert reqst.cons;
  }
   catch( Exception e ){
   resp.statusCode = 'Error';
     resp.statusMessage = 'Exception : ' + e.getMessage();
   }
   resp.statusCode = 'Done';
    resp.statusMessage = 'Test success message';
   resp.acct = reqst.acct;
    resp.cons = reqst.cons;
     return resp;
  }
}

Hope the above code helps .Also this shows how even from wrapper we can extract Sobject.So i think variable name should get you the necessary mapping.

4

I think you can use the System.RestRequest and System.RestResponse contexts to achieve that. There's some good examples of REST handlers in the help. Some speculative pseudocode:

@RestResource(urlMapping='/Log/*')
global with sharing class RESTLogController {

  @HttpPost
  global static String doPost()
  {
    String body = System.RestContext.request.requestBody.toString();
    Log__c log = (Log__c)System.Json.deserialize(body, Log__c.class);

    insert log;
    return log.Id;
  }

}

Or by manipulating the response:

System.RestResponse response = System.RestContext.response;
response.responseBody = log.Id;
2
  • 1
    thanks! that's a leaner solution :) I've still some problems in getting the JSON from the blob, but I'll get it on monday..
    – itsmebasti
    Commented Apr 19, 2013 at 12:15
  • @Basti maybe blob.toString() instead of String.valueOf() ... my bad Commented Apr 20, 2013 at 10:40
2

@metadaddy thanks for leading me back to the documentation. I relied on google and DF Topics, but never thought of the official doc. REST API doc is the official solution. thanks guys for spending time with this simple/stupid question!

You must log in to answer this question.

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