Saturday, June 17, 2017

Creating SOAP Web Service in Apex

Create the apex class and generate its wsdl. Download Partner wsdl as well.
global class MathOperations
{
    webservice static Integer getSum (Integer a, Integer b)
    {
        return a + b;
    }
}

Convert the wsdl files that you have downloaded to jar files using the command below:
java -classpath force-wsc-40.1.1.jar;rhino-1.7.7.1.jar;ST-4.0.8.jar;C:\Java\jdk1.8.0_121\lib\tools.jar com.sforce.ws.tools.wsdlc partner.wsdl partner_stub.jar

Using SoapUI, get the sessionId from partner wsdl:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">
   <soapenv:Header>
   </soapenv:Header>
   <soapenv:Body>
      <urn:login>
         <urn:username>username</urn:username>
         <urn:password>password</urn:password>
      </urn:login>
   </soapenv:Body>
</soapenv:Envelope>


Enter the sessionId value in custom apex class wsdl. Check the below request and response:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mat="http://soap.sforce.com/schemas/class/MathOperations">
   <soapenv:Header>
      <mat:SessionHeader>
         <mat:sessionId>00D6A000001Vait!ARwAQPfREKhyBFwVu3F.vFEXYDvLIiR3w8K5hlZvemfv_vFPGECj.QLj3HXsOVFQzVb7OQAG.T9QFWqUzh5P0PP0rPhASOdo</mat:sessionId>
      </mat:SessionHeader>
   </soapenv:Header>
   <soapenv:Body>
      <mat:getSum>
         <mat:a>5</mat:a>
         <mat:b>8</mat:b>
      </mat:getSum>
   </soapenv:Body>
</soapenv:Envelope>




<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/schemas/class/MathOperations">
   <soapenv:Body>
      <getSumResponse>
         <result>13</result>
      </getSumResponse>
   </soapenv:Body>
</soapenv:Envelope>



You can call this web service using Java like below:
package wsc;

import com.sforce.soap.MathOperations.Connector;
import com.sforce.soap.MathOperations.SoapConnection;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class SalesforceSoapStarter {

    private static final String AUTH_ENDPOINT =  "https://login.salesforce.com/services/Soap/u/40.0";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";

    public static void main(String[] args) throws ConnectionException {

     ConnectorConfig config = new ConnectorConfig();
        config.setUsername(USERNAME);
        config.setPassword(PASSWORD);
        config.setAuthEndpoint(AUTH_ENDPOINT);
        
        PartnerConnection connection = new PartnerConnection(config);

        String sessionId = config.getSessionId();
        
        SoapConnection soap = Connector.newConnection("","");
        soap.setSessionHeader(sessionId);
        System.out.println(soap.getSum(4, 9));
    }

}

Refer this link to set up Java developer environment:
https://developer.salesforce.com/docs/atlas.en-us.salesforce_developer_environment_tipsheet.meta/salesforce_developer_environment_tipsheet/salesforce_developer_environment_overview.htm
Share This:    Facebook Twitter

0 comments:

Post a Comment

Total Pageviews

My Social Profiles

View Sonal's profile on LinkedIn

Tags

__proto__ $Browser Access Grants Accessor properties Admin Ajax AllowsCallouts Apex Apex Map Apex Sharing AssignmentRuleHeader AsyncApexJob Asynchronous Auth Provider AWS Callbacks Connected app constructor Cookie CPU Time CSP Trusted Sites CSS Custom settings CustomLabels Data properties Database.Batchable Database.BatchableContext Database.query Describe Result Destructuring Dynamic Apex Dynamic SOQL Einstein Analytics enqueueJob Enterprise Territory Management Enumeration escapeSingleQuotes featured Flows geolocation getGlobalDescribe getOrgDefaults() getPicklistValues getRecordTypeId() getRecordTypeInfosByName() getURLParameters Google Maps Governor Limits hasOwnProperty() Heap Heap Size IIFE Immediately Invoked Function Expression Interview questions isCustom() Javascript Javascript Array jsForce Lightning Lightning Components Lightning Events lightning-record-edit-form lightning:combobox lightning:icon lightning:input lightning:select LockerService Lookup LWC Manual Sharing Map Modal Module Pattern Named Credentials NodeJS OAuth Object.freeze() Object.keys() Object.preventExtensions() Object.seal() Organization Wide Defaults Override PDF Reader Performance performance.now() Permission Sets Picklist Platform events Popup Postman Primitive Types Profiles Promise propertyIsEnumerable() prototype Query Selectivity Queueable Record types Reference Types Regex Regular Expressions Relationships Rest API Rest Operator Revealing Module Pattern Role Hierarchy Salesforce Salesforce Security Schema.DescribeFieldResult Schema.DescribeSObjectResult Schema.PicklistEntry Schema.SObjectField Schema.SObjectType Security Service Components Shadow DOM Sharing Sharing Rules Singleton Slots SOAP API SOAP Web Services SOQL SOQL injection Spread Operator Star Rating stripInaccessible svg svgIcon Synchronous this Token Triggers uiObjectInfoApi Upload Files VSCode Web Services XHR
Scroll To Top