0

I am new to Rest Webservices and need some help. I have a requirement where the external system creates an account and is calling the Rest webservice to send the created account details as a response in JSON format. I need to write a Rest Webservice for this. I have gone through the documentations but, still getting confused as this is the first time I am working on this.

When the response is received in JSON format, how will I parse it to get the information being sent and what method will I be using to do so (@HttpPost or @HttpGet). Once the parsing is done,with the information received,I need to create a case in salesforce and send back the case ID to the external system.How do i do this.

Any help by shedding some light on this would be appreciated. Thanks in Advance.

Regards, sfdcdev

2 Answers 2

0

So, basically you have a couple of questions. I'll try to answer them one by one.

1) When retrieving information from salesforce (lets assume you want to retrieve an account) you use a GET method. When you want to do something, you use a post method. For more information, check here.

2) For your case, you would want to create a post method like this:

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

    @HttpPost
    global static Id DummyMethod(Fields that you want to pass) {
        ... create case ...
        return ID of the created case
    }
}

You can then test this by going to https://workbench.developerforce.com/login.php and to the REST explorer. There you need to type in the following URL :

/services/apexrest/Dummy

With payload :

{
       "Field1"@Note : Field1Value",
       "Field2" : "Field2Value"
}

@Note: Keep in mind the field names need to match EXACTLY the parameter names defined in the dummy method above.

3
  • Hi Koen, Thanks for the reply and information. I am not retrieving information from Salesforce. I need to get it from the response (which is in JSON format) sent by external source and create a case record.
    – sfdcdev
    Commented Oct 26, 2017 at 9:40
  • Oh okay, then I completely misunderstood the question. My apologies. Commented Oct 26, 2017 at 11:10
  • This answer was helpful for some extent.
    – sfdcdev
    Commented Nov 2, 2017 at 9:45
0

You need to use JSON.DeSerialize(body). Here's some pseudo code for you to start with.

public class CaseList {
    public CaseItem[] case;
}
public class CaseItem {
    public String CONTAINER, caseName, caseStatus;
    public Integer providerCaseId;
}

@future(callout=true)
    public static void parseJSONResponse() {        
        Http httpProtocol = new Http();
        // Create HTTP request to send.
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL.
        String endpoint = 'xxxxxxxx';
        request.setEndPoint(endpoint);
        // Set the HTTP verb to GET.
        request.setMethod('GET');

        request.setHeader('Authorization','XXX');


        // Send the HTTP request and get the response.
        // The response is in JSON format.
        HttpResponse response = httpProtocol.send(request);
        System.debug(response.getBody());

        List<Case> cList = new List<Case>();

        // Parse the data according to the CaseList class
        CaseList data = (CaseList)JSON.deserialize(response.getBody(), CaseList.class);
        // Iterate over the data
        for(CaseItem item: data.case) {
            // Do stuff with item...
        }
    }
1
  • Hi @sfdcFanBoy, Thanks for the reply. Your code seems to be making a outbound call to external service. In my case, it is inbound call. Information is sent and I need to use it to create a case record and send back the status as success or failure.
    – sfdcdev
    Commented Oct 27, 2017 at 6:58

You must log in to answer this question.

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