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;
    }

}
  • 3
    What problems were you seeing with this approach? The docs definitely say you can pass sObjects as parameters. – metadaddy Apr 19 '13 at 16:19
  • Can you post your code you tried ????? – Mohith Shrivastava Apr 19 '13 at 17:58
7
{
"reqst": {
    "acct": {
        "Name": "Test Account 1",
        "AccountNumber": "00000001",
        "Site": "Site1",
        "Website": "www.site1.com"
    },
    "cons": [
        {
            "Name": "Test Contact1",
            "email": "testcontact1@test.com",
            "LastName": "TLName2"
        },
        {
            "Name": "Test Contact2",
            "email": "testcontact2@test.com",
            "LastName": "TLName3"
        },
        {
            "Name": "Test Contact3",
            "email": "testcontact3@test.com",
            "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;
  • 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.. – Basti Apr 19 '13 at 12:15
  • @Basti maybe blob.toString() instead of String.valueOf() ... my bad – bigassforce Apr 20 '13 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!

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.