0

I have created an apex rest httppost class. This requires two parameters which are passed via response. I am trying to test this via workbench by passing two parameters.

@RestResource(urlMapping='/OpportunityDetails/*')
global class OppDetails 
{  
    @HttpPost   
    global static void getDetails()
    {
        try
        {         
            RestRequest     request    = RestContext.request;
            RestResponse    response   = RestContext.response;
            Opportunity opp = (Opportunity)JSON.deserialize(request.requestBody.toString(),Opportunity.class);
            System.debug('----myopp: '+ opp);
        }
        catch(Exception ex)
        {

        }
   }

The JSON deserialized value is empty even though I am passing two values.

I am not sure how to test this using postman but this is how I am testing via workbench. My idea is to get both the parameter values and then insert based on ID.

Workbench Screenshot

Debug logs show Response body as null.

RestResponse:[headers={}, responseBody=null, statusCode=null]
1

That's because you don't have any the fields opportunityId or customerId` in your opportunity object, and you are using the opportunity sobject class to deserialize. You should only pass the field that are on your object if you want to use the opportunity sobject class.

{ "Id": "006...", "AccountId": "001..." }

Also you should check request body for what you are getting from workbench not response body. You did not set a response body, so the response in workbench would be empty.

| improve this answer | |
  • I edited your answer to be more assertive and add some clarification. Please feel free to review and make any further adjustments if you'd like. – sfdcfox Apr 12 at 15:43
  • @sfdcfox thankyou – User6670 Apr 12 at 15:46
  • Thanks @sfdcfox , I was not passing the exact field names as they exist in Opportunity object. Now, its working. – SalesforceTechie Apr 13 at 6:00

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.