The salesforce platform is very powerful and extensible . A lot of the customizations are possible with the API’s that the platform provides. In this post I write about how to use these API’s.

The API’s can be used on all different types of editions like Developer edition,Enterprise edition etc.,

Firstly we will need a salesforce org to play around with . Head over to https://developer.salesforce.com/form/signup/freetrial.jsp and spin up a new developer org. Developer Edition provides a free and easy solution so that you can use Salesforce for testing and development.

Once you have an org login to it and head over to Setup -> API as shown below

alt text

There a bunch of API familes that the platform provides. The whole list can be found at https://developer.salesforce.com/docs/apis . In this post we will be working with Metadata,Enterprise and Tooling API’s.

Metadata and Enterprise API’s

Metadata API

As the name implies the Metadata API allows us to work with metadata .

  • Click on Generate Metadata WSDL from the above screenshot.
  • On My machine it opens up a new tab and spits out an XML file. Download and save it to metadata.xml in some location on your local file system.

Enterprise API

  • Click on Generate Enterprise WSDL.
  • Save it to enterprise.xml as you did above for the metadata api

Force.com Web Service Connector

  • We are going to generate a jar file out of the downloaded xml . To do that head over to https://mvnrepository.com/artifact/com.force.api/force-wsc
  • Pick the version you want to work with . I am using the latest version . At the time of this writing its 61.1.0 .
  • We need to download an Uber jar work with . So Click on View All in the Files section

alt text

  • That will List all the files in the repo . Download the uber jar . force-wsc-61.1.0-uber.jar to the same location you downloaded the metadata.xml to .

Generating the jar

In this setup I have downloaded the xml to My Home Directory. ~/Desktop/salesforce . ~ generally refers to the home directory.

  • Open up a terminal and change to the above directory.
cd ~/Desktop/salesforce/
  • Generate the jar. Java is needed.
java -jar force-wsc-61.1.0-uber.jar metadata.xml metadata.jar
  • If all goes well you should see something like this. Please note that the number of files might be different.
[WSC][wsdlc.main:72]Generating Java files from schema ...
[WSC][wsdlc.main:72]Generated 1813 java files.
[WSC][wsdlc.main:72]Compiled 1816 java files.
[WSC][wsdlc.main:72]Generating jar file ... metadata.jar
[WSC][wsdlc.main:72]Generated jar file metadata.jar

Now we have everything needed to use the metadata API and work with metadata in your Org.

Do the same for the enterprise api and generate enterprise.jar

Using the Generated Jars

I usually prefer working with Intellij IDE’s when I work with java. They are miles ahead of anything else.

  • Download the Community edition from their website.
  • Create a New Project. I prefer to use maven for my build system . Create as shown below

alt text

Now lets use the generated jar .

  • Add the metadata.jar and enterprise.jar to the resources.
  • Add the force-wsc dependency . Your dependencies should look something like this

Dependencies

<dependencies>
        <dependency>
            <groupId>com.force.api</groupId>
            <artifactId>force-wsc</artifactId>
            <version>61.1.0</version>
        </dependency>

        <dependency>
            <groupId>com.sforce</groupId>
            <artifactId>metadata</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/metadata.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.sforce</groupId>
            <artifactId>enterprise</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/enterprise.jar</systemPath>
        </dependency>
    </dependencies>

Login Utility class

Now lets go ahead and login . Use the below Utility class to login . You can write your own as well.


import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.LoginResult;
import com.sforce.soap.metadata.MetadataConnection;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class SalesforceLogin {

    public static EnterpriseConnection loginEnterpriseApi(String userName, String password, String url) throws ConnectionException {
        LoginResult loginResult = loginToSalesforce(userName, password, url);
        return createEnterpriseConnection(loginResult);
    }

    public static MetadataConnection loginMetadataApi(String userName, String password, String url) throws ConnectionException {
        LoginResult loginResult = loginToSalesforce(userName, password, url);
        return createMetadataConnection(loginResult);
    }

    private static MetadataConnection createMetadataConnection(LoginResult loginResult) throws ConnectionException {
        ConnectorConfig config = new ConnectorConfig();
        config.setServiceEndpoint(loginResult.getMetadataServerUrl());
        config.setSessionId(loginResult.getSessionId());
        return new MetadataConnection(config);
    }

    private static EnterpriseConnection createEnterpriseConnection(LoginResult loginResult) throws ConnectionException {
        ConnectorConfig config = new ConnectorConfig();
        config.setServiceEndpoint(loginResult.getServerUrl());
        config.setSessionId(loginResult.getSessionId());
        return new EnterpriseConnection(config);
    }


    private static LoginResult loginToSalesforce(String username, String password, String loginUrl) throws ConnectionException {
        ConnectorConfig config = new ConnectorConfig();
        config.setAuthEndpoint(loginUrl);
        config.setServiceEndpoint(loginUrl);
        config.setManualLogin(true);
        return new EnterpriseConnection(config).login(username, password);
    }
}

After this you should be able to login . Note the login endpoint below. It must be BASE_URL/services/Soap/c/60.0 . In my case its https://migsoftware-dev-ed.develop.my.salesforce.com/services/Soap/c/60.0

import com.sforce.soap.metadata.MetadataConnection;
import com.sforce.ws.ConnectionException;

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


        MetadataConnection connection = SalesforceLogin.loginMetadataApi(
                "YOUR_USER_NAME", "YOUR_PASSWORD", "BASE_URL/services/Soap/c/60.0");

    }
}