2

I read this post and learned that same class can be used to exposed for consuming SOAP and REST API: Exposing same apex method via SOAP and REST API?

My question is: Which of the below is the recommended approach?

  1. Single method that be can be used for both SOAP and REST like below:

    @RestResource(urlmapping = '/someUrl/*')
    global class SOAPRESTClass {
        @HttpGet
        webservice static void myMethod(){`
            System.debug('From myMethod');
        }
    
    }
    
  2. Create 2 methods one can be used for SOAP and other can be used as REST.

What is the best practice. Are there any side effects for option 1?

  • I don't think there are any negative side-effects. It seems solely advantageous and would probably simplify your development cycle. – Adrian Larson Oct 15 '16 at 16:01
  • Which answer to choose option 1 or 2? – Santanu Boral Oct 15 '16 at 17:22
2

Generally speaking, if you can use both REST and SOAP in the same line of code, you may as well go for it. It's less unit testing, fewer lines of code, and better API unification. I would imagine that there's a few situations where having distinct REST/SOAP methods might make sense, perhaps if you have more than 32 parameters (the limit for Apex method parameters), but in the majority of cases, I would recommend using the pattern you've described if you want the flexibility of both REST and SOAP. As an added bonus, you get to support REST XML and JSON in one broad sweep. You'll want to test your API calls in each of the three modes (SOAP, REST XML, and REST JSON) to make sure it works as you expect, but the principle is sound. There really isn't a "best practice" for this sort of thing. If you need both REST and SOAP, use it. If you only need REST or SOAP, use whichever is appropriate for your case. It's usually easy enough to add REST or SOAP support if you need it later.

  • @SantanuBoral Sadly, I've never obtained a cert, but I can't possibly imagine that they'd prefer two methods over one. I'd argue strongly for a single one, but I don't know what the cert expects. – sfdcfox Jan 4 '17 at 5:07

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.