SOAP Webservice

1.Consume SOAP wsdl




WSDL Code :





<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://parks.services/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="ParksImplService" targetNamespace="http://parks.services/">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://parks.services/" elementFormDefault="unqualified" targetNamespace="http://parks.services/" version="1.0">

  <xs:element name="byCountry" type="tns:byCountry"/>

  <xs:element name="byCountryResponse" type="tns:byCountryResponse"/>

  <xs:complexType name="byCountry">
    <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="byCountryResponse">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
  </wsdl:types>
  <wsdl:message name="byCountry">
    <wsdl:part element="tns:byCountry" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="byCountryResponse">
    <wsdl:part element="tns:byCountryResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="Parks">
    <wsdl:operation name="byCountry">
      <wsdl:input message="tns:byCountry" name="byCountry">
    </wsdl:input>
      <wsdl:output message="tns:byCountryResponse" name="byCountryResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="ParksImplServiceSoapBinding" type="tns:Parks">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="byCountry">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="byCountry">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="byCountryResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="ParksImplService">
    <wsdl:port binding="tns:ParksImplServiceSoapBinding" name="ParksImplPort">
      <soap:address location="https://th-apex-soap-service.herokuapp.com/service/parks"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>


WSDL To Apex :

//Generated by wsdl2apex

public class ParkService {
    public class byCountryResponse {
        public String[] return_x;
        private String[] return_x_type_info = new String[]{'return','http://parks.services/',null,'0','-1','false'};
        private String[] apex_schema_type_info = new String[]{'http://parks.services/','false','false'};
        private String[] field_order_type_info = new String[]{'return_x'};
    }
    public class byCountry {
        public String arg0;
        private String[] arg0_type_info = new String[]{'arg0','http://parks.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://parks.services/','false','false'};
        private String[] field_order_type_info = new String[]{'arg0'};
    }
    public class ParksImplPort {
        public String endpoint_x = 'https://th-apex-soap-service.herokuapp.com/service/parks';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://parks.services/', 'ParkService'};
        public String[] byCountry(String arg0) {
            ParkService.byCountry request_x = new ParkService.byCountry();
            request_x.arg0 = arg0;
            ParkService.byCountryResponse response_x;
            Map<String, ParkService.byCountryResponse> response_map_x = new Map<String, ParkService.byCountryResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://parks.services/',
              'byCountry',
              'http://parks.services/',
              'byCountryResponse',
              'ParkService.byCountryResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.return_x;
        }
    }
}


Apex class to call ParkService  class and it's method : 

/**=====================================

Developer Name : Pratiksha Narvekar

=====================================**/

public class ParkLocator {
    public static List<String> country(String objString){
    ParkService.ParksImplPort objClass = new ParkService.ParksImplPort();
    return objClass.byCountry(objString);
    }
}

Test Class

/**=====================================
Developer Name : Pratiksha Narvekar

=====================================**/

@isTest
private class ParkLocatorTest {
    @isTest static void testCallout() {              
        Test.setMock(WebServiceMock.class, new ParkServiceMock());
        List <String> result = ParkLocator.country('Test');
    }
}


Mock class : 

/**
Developer Name : Pratiksha Narvekar
**/


@isTest
global class ParkServiceMock implements WebServiceMock {
   global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {
        ParkService.byCountryResponse response_x = 
            new ParkService.byCountryResponse();
        response_x.return_x = new List < String > {'a', 'b'};
        response.put('response_x', response_x); 
   }
}




2.Expose SOAP wsdl



/**===================================== *Class Name      : SOAPWebserviceExampleOne
*Test Class Name :
*Description     : SOAPWebserviceExampleOne it is a soap webservice class.
*Developer Name  : Pratiksha Narvekar
*Created Date    : 31-10-2017
=====================================**/
global class SOAPWebserviceExampleOne {
    global static list<Response> listReq = new list<Response>();
    global class Response{
    ID LeadID{get;set;}
    string status{get;set;}
    string message{get;set;}
    }
    webservice static list<Response> createLead(String name, String ContactNumber, String EmailID){
    if(!string.isBlank(name) && !string.isBlank(ContactNumber) && !string.isBlank(EmailID)){
    Lead objLead = new Lead();
    Response objReq = new Response();
    objLead.LastName = name;
    objLead.Email = EmailID;
    objLead.MobilePhone = ContactNumber;
    objLead.Company = 'test';
        Database.SaveResult objSr = Database.insert(objLead, false);
        if(objSr.isSuccess()){
        objReq.LeadID = objSr.getID();
        objReq.status = '200';
        objReq.message = 'Lead created successfully...';
       
        }
        else{
        for(Database.Error err : objSr.getErrors()) {
        objReq.LeadID = null;
        objReq.status = String.valueof(err.getStatusCode());
        objReq.message = err.getMessage()+err.getFields();
        }
        }
        listReq.add(objReq);
    }
    return listReq;
    }
    }

Create WSDL :




<?xml version="1.0" encoding="UTF-8"?>
<!--
 Web Services API : SOAPWebserviceExampleOne
-->
<definitions targetNamespace="http://soap.sforce.com/schemas/class/SOAPWebserviceExampleOne" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://soap.sforce.com/schemas/class/SOAPWebserviceExampleOne">
 <types>
  <xsd:schema elementFormDefault="qualified" targetNamespace="http://soap.sforce.com/schemas/class/SOAPWebserviceExampleOne">
   <xsd:element name="AllowFieldTruncationHeader">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="allowFieldTruncation" type="xsd:boolean"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="CallOptions">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="client" type="xsd:string"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="DebuggingHeader">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="categories" minOccurs="0" maxOccurs="unbounded" type="tns:LogInfo"/>
      <xsd:element name="debugLevel" type="tns:LogType"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:complexType name="LogInfo">
    <xsd:sequence>
     <xsd:element name="category" type="tns:LogCategory"/>
     <xsd:element name="level" type="tns:LogCategoryLevel"/>
    </xsd:sequence>
   </xsd:complexType>
   <xsd:simpleType name="LogCategory">
    <xsd:restriction base="xsd:string">
     <xsd:enumeration value="Db"/>
     <xsd:enumeration value="Workflow"/>
     <xsd:enumeration value="Validation"/>
     <xsd:enumeration value="Callout"/>
     <xsd:enumeration value="Apex_code"/>
     <xsd:enumeration value="Apex_profiling"/>
     <xsd:enumeration value="Visualforce"/>
     <xsd:enumeration value="System"/>
     <xsd:enumeration value="Wave"/>
     <xsd:enumeration value="All"/>
    </xsd:restriction>
   </xsd:simpleType>
   <xsd:simpleType name="LogCategoryLevel">
    <xsd:restriction base="xsd:string">
     <xsd:enumeration value="None"/>
     <xsd:enumeration value="Finest"/>
     <xsd:enumeration value="Finer"/>
     <xsd:enumeration value="Fine"/>
     <xsd:enumeration value="Debug"/>
     <xsd:enumeration value="Info"/>
     <xsd:enumeration value="Warn"/>
     <xsd:enumeration value="Error"/>
    </xsd:restriction>
   </xsd:simpleType>
   <xsd:simpleType name="LogType">
    <xsd:restriction base="xsd:string">
     <xsd:enumeration value="None"/>
     <xsd:enumeration value="Debugonly"/>
     <xsd:enumeration value="Db"/>
     <xsd:enumeration value="Profiling"/>
     <xsd:enumeration value="Callout"/>
     <xsd:enumeration value="Detail"/>
    </xsd:restriction>
   </xsd:simpleType>
   <xsd:element name="DebuggingInfo">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="debugLog" type="xsd:string"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="SessionHeader">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="sessionId" type="xsd:string"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:simpleType name="ID">
    <xsd:restriction base="xsd:string">
     <xsd:length value="18"/>
     <xsd:pattern value="[a-zA-Z0-9]{18}"/>
    </xsd:restriction>
   </xsd:simpleType>
   <xsd:complexType name="Response">
    <xsd:sequence/>
   </xsd:complexType>
   <xsd:complexType name="address">
    <xsd:complexContent>
     <xsd:extension base="tns:location">
      <xsd:sequence>
       <xsd:element name="city" type="xsd:string"/>
       <xsd:element name="country" type="xsd:string"/>
       <xsd:element name="countryCode" type="xsd:string"/>
       <xsd:element name="geocodeAccuracy" type="xsd:string"/>
       <xsd:element name="postalCode" type="xsd:string"/>
       <xsd:element name="state" type="xsd:string"/>
       <xsd:element name="stateCode" type="xsd:string"/>
       <xsd:element name="street" type="xsd:string"/>
      </xsd:sequence>
     </xsd:extension>
    </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="location">
    <xsd:sequence>
     <xsd:element name="latitude" type="xsd:double"/>
     <xsd:element name="longitude" type="xsd:double"/>
    </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="createLead">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="name" type="xsd:string" nillable="true"/>
      <xsd:element name="ContactNumber" type="xsd:string" nillable="true"/>
      <xsd:element name="EmailID" type="xsd:string" nillable="true"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="createLeadResponse">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="result" minOccurs="0" maxOccurs="unbounded" type="tns:Response" nillable="true"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
  </xsd:schema>
 </types>
 <!-- Message for the header parts -->
 <message name="Header">
  <part name="AllowFieldTruncationHeader" element="tns:AllowFieldTruncationHeader"/>
  <part name="CallOptions" element="tns:CallOptions"/>
  <part name="DebuggingHeader" element="tns:DebuggingHeader"/>
  <part name="DebuggingInfo" element="tns:DebuggingInfo"/>
  <part name="SessionHeader" element="tns:SessionHeader"/>
 </message>
 <!-- Operation Messages -->
 <message name="createLeadRequest">
  <part element="tns:createLead" name="parameters"/>
 </message>
 <message name="createLeadResponse">
  <part element="tns:createLeadResponse" name="parameters"/>
 </message>
 <portType name="SOAPWebserviceExampleOnePortType">
  <operation name="createLead">
   <input message="tns:createLeadRequest"/>
   <output message="tns:createLeadResponse"/>
  </operation>
 </portType>
 <binding name="SOAPWebserviceExampleOneBinding" type="tns:SOAPWebserviceExampleOnePortType">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="createLead">
   <soap:operation soapAction=""/>
   <input>
    <soap:header use="literal" part="SessionHeader" message="tns:Header"/>
    <soap:header use="literal" part="CallOptions" message="tns:Header"/>
    <soap:header use="literal" part="DebuggingHeader" message="tns:Header"/>
    <soap:header use="literal" part="AllowFieldTruncationHeader" message="tns:Header"/>
    <soap:body use="literal" parts="parameters"/>
   </input>
   <output>
    <soap:header use="literal" part="DebuggingInfo" message="tns:Header"/>
    <soap:body use="literal"/>
   </output>
  </operation>
 </binding>
 <service name="SOAPWebserviceExampleOneService">
  <documentation></documentation>
  <port binding="tns:SOAPWebserviceExampleOneBinding" name="SOAPWebserviceExampleOne">
   <soap:address location="https://ap5.salesforce.com/services/Soap/class/SOAPWebserviceExampleOne"/>
  </port>
 </service>
</definitions>


Run this using developer console.





No comments:

Post a Comment