banner



How To Know If Web Service Is Rest Or Soap In Salesforce

Learning Objectives

After completing this module, you lot'll be able to:

  • Generate Apex classes using WSDL2Apex.
  • Perform a callout to send information to an external service using SOAP.
  • Test callouts past using mock callouts.

Follow Forth with Trail Together

Desire to follow along with an expert as you work through this step? Accept a look at this video, office of the Trail Together series on Trailhead Live.

(This prune starts at the 45:49 minute marker, in instance you desire to rewind and watch the beginning of the step again.)

Use WSDL2Apex to Generate Noon Code

In add-on to Remainder callouts, Apex can as well make callouts to Lather web services using XML. Working with SOAP can be a painful (but necessary) experience. Fortunately, we have tools to brand the process easier.

WSDL2Apex automatically generates Apex classes from a WSDL document. You download the web service's WSDL file, and so you upload the WSDL and WSDL2Apex generates the Apex classes for you. The Apex classes construct the SOAP XML, transmit the information, and parse the response XML into Apex objects. Instead of developing the logic to construct and parse the XML of the web service messages, let the Apex classes generated by WSDL2Apex internally handle all that overhead. If you are familiar with WSDL2Java or with importing a WSDL as a Web Reference in .NET, this functionality is like to WSDL2Apex. You're welcome.

For this example, nosotros're using a simple calculator web service to add together two numbers. It'south a groundbreaking service that is all the rage! The commencement matter we need to practice is download the WSDL file to generate the Noon classes. Click this link and download the calculator.xml file to your computer. Retrieve where y'all salve this file, because you need it in the next step.

Generate an Noon Form from the WSDL

  1. From Setup, enter Apex Classes in the Quick Detect box, and so click Noon Classes.
  2. Click Generate from WSDL.
  3. Click Choose File and select the downloaded calculator.xml file.
  4. Click Parse WSDL.

    The application generates a default class proper name for each namespace in the WSDL document and reports any errors.

    For this example, use the default class proper name. All the same, in real life information technology is highly recommended that you modify the default names to make them easier to work with and make your code more than intuitive.

    It'due south time to talk honestly about the WSDL parser. WSDL2Apex parsing is a notoriously fickle beast. The parsing process tin neglect for several reasons, such as an unsupported type, multiple bindings, or unknown elements. Unfortunately, you lot could exist forced to manually lawmaking the Apex classes that call the spider web service or employ HTTP.

  5. Click Generate Apex lawmaking.

    The final page of the wizard shows the generated classes, along with any errors. The folio besides provides a link to view successfully generated code.

The generated Apex classes include stub and type classes for calling the third-party spider web service represented by the WSDL document. These classes permit you to phone call the external spider web service from Noon. For each generated form, a second class is created with the same name and the prefix Async. The calculatorServices form is for synchronous callouts. The AsyncCalculatorServices course is for asynchronous callouts.

Execute the Callout

Prerequisites

Earlier y'all run this case, authorize the endpoint URL of the web service callout, https://th-noon-soap-service.herokuapp.com, using the steps from the Qualify Endpoint Addresses section.

At present you can execute the callout and see if it correctly adds two numbers. Accept a calculator handy to check the results.

  1. Open the Developer Panel from the Setup gear (Setup gear icon).
  2. In the Programmer Console, select Debug | Open up Execute Bearding Window.
  3. Delete all existing code and insert the following snippet.
    calculatorServices.CalculatorImplPort computer = new  calculatorServices.CalculatorImplPort(); Double x = 1.0; Double y = 2.0; Double result = calculator.doAdd(x,y); Arrangement.debug(result);
  4. Select Open Log, and so click Execute.
  5. Afterwards the debug log opens, click Debug Only to view the output of the System.debug statements. The log should display iii.0.

Test Web Service Callouts

All experienced Noon developers know that to deploy or bundle Apex code, at least 75% of that lawmaking must have test coverage. This coverage includes our classes generated by WSDL2Apex. You might have heard this before, but test methods don't back up web service callouts, and tests that perform web service callouts fail.

So, we have a little piece of work to do. To prevent tests from failing and to increment lawmaking coverage, Noon provides a built-in WebServiceMock interface and the Exam.setMock method. You tin employ this interface to receive fake responses in a test method, thereby providing the necessary examination coverage.

Specify a Mock Response for Callouts

When y'all create an Apex class from a WSDL, the methods in the autogenerated class call WebServiceCallout.invoke, which performs the callout to the external service. When testing these methods, you lot can instruct the Apex runtime to generate a fake response whenever WebServiceCallout.invoke is called. To do then, implement the WebServiceMock interface and specify a false response for the testing runtime to send.

Instruct the Apex runtime to transport this faux response past calling Test.setMock in your test method. For the first argument, pass WebServiceMock.form. For the 2d argument, laissez passer a new instance of your WebServiceMock interface implementation.

Exam.setMock(WebServiceMock.class, new MyWebServiceMockImpl());

That's a lot to grok, so let's look at some code for a complete example. In this example, you create the class that makes the callout, a mock implementation for testing, and the test class itself.

  1. In the Developer Console, select File | New | Noon Course.
  2. For the class name, enter AwesomeCalculator and then click OK.
  3. Replace autogenerated code with the following grade definition.
    public class AwesomeCalculator {     public static Double add(Double 10, Double y) {         calculatorServices.CalculatorImplPort reckoner =              new calculatorServices.CalculatorImplPort();         render reckoner.doAdd(x,y);     } }
  4. Press CTRL+S to salvage.

    Create your mock implementation to fake the callout during testing. Your implementation of WebServiceMock calls the doInvoke method, which returns the response yous specify for testing. Near of this code is average. The hardest function of this exercise is figuring out how the web service returns a response so that you lot tin fake a value.

  5. In the Developer Console, select File | New | Apex Course.
  6. For the course name, enter CalculatorCalloutMock so click OK.
  7. Replace the autogenerated lawmaking with the following class definition.
    @isTest global form CalculatorCalloutMock implements WebServiceMock {    global void doInvoke(            Object stub,            Object asking,            Map<String, Object> response,            String endpoint,            String soapAction,            String requestName,            Cord responseNS,            Cord responseName,            String responseType) {         // start - specify the response yous want to ship         calculatorServices.doAddResponse response_x =              new calculatorServices.doAddResponse();         response_x.return_x = 3.0;         // terminate         response.put('response_x', response_x);     } }
  8. Press CTRL+S to save.

    Lastly, your test method needs to instruct the Apex runtime to send the fake response by calling Examination.setMock before making the callout in the AwesomeCalculator class. Like any other test method, nosotros affirm that the right effect from our mock response was received.

  9. In the Developer Console, select File | New | Apex Form.
  10. For the class name, enter AwesomeCalculatorTest and then click OK.
  11. Replace the autogenerated lawmaking with the post-obit class definition.
    @isTest private course AwesomeCalculatorTest {     @isTest static void testCallout() {                       // This causes a imitation response to be generated         Test.setMock(WebServiceMock.class, new CalculatorCalloutMock());         // Call the method that invokes a callout         Double ten = 1.0;         Double y = 2.0;         Double event = AwesomeCalculator.add(ten, y);         // Verify that a fake upshot is returned         Arrangement.assertEquals(3.0, result);      } }
  12. Press CTRL+S to save.
  13. To run the test, select Test | Run All.

The AwesomeCalculator class should now display 100% code coverage!

Resources

  • Apex Developer Guide: SOAP Services: Defining a Class from a WSDL Document
  • Apex Developer Guide: Examination Web Service Callouts
  • Salesforce Developers Web log: Announcing the Open up-Source WSDL2Apex Generator

How To Know If Web Service Is Rest Or Soap In Salesforce,

Source: https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_soap_callouts

Posted by: meadowshopper.blogspot.com

0 Response to "How To Know If Web Service Is Rest Or Soap In Salesforce"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel