[Salesforce] Registering and updating static resources with Tooling API (Java sample SOAP API)

What is Tooling API?

The Tooling API provides SOAP and REST interfaces that allow you to build custom development tools for Force.com applications. This time, we will use SOAP API in Tooling API to register and update static resources. Use Enterprise WSDL.

Development flow

  1. Enterprise WSDL generation and acquisition
  2. Import the required Jar files into your project Generate an Enterprise Jar file and import it into the project using the WSDL obtained in 3.1.
  3. Source implementation

Description

1. Enterprise WSDL generation and acquisition

Please refer to the Salesforce manual.

2. Import the required Jar files into your project

The sample requires the "Force.com Web Service Connector" package. Maven import example is added to the following setting pom.xml file. For other project management tools, see mvnrepository.

pom.xml


<!-- https://mvnrepository.com/artifact/com.force.api/force-wsc -->
<dependency>
    <groupId>com.force.api</groupId>
    <artifactId>force-wsc</artifactId>
    <version>48.1.0</version>
</dependency>

Generate an Enterprise Jar file and import it into the project using the WSDL obtained in 3.1.

Create an Enterprise Jar file with the following source

java –classpath force-wsc-30.0.0.jar;ST4-4.0.7.jar;antlr-runtime-3.5.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar

There are many examples of eclipse on the net for importing into a project, so search on Google and omit this. The following is an example of importing vscode.

  1. Distribute the Jar file generated above to the folder you want to save.
  2. Add the following sole to the ".classpath" file under the project save folder.

.classpath


<classpathentry kind="lib" path="C:\xxx\.jar\enterprise.jar"/>

4. Source implementation

4.1. Import the required classes

//For reading static resources
import java.nio.file.Paths;
import java.io.IOException;
import java.nio.file.Files;

//For Tooling API calls
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.soap.enterprise.sobject.StaticResource;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

4.2. Generate Enterprise Connection

getEnterpriseConnection


    private static EnterpriseConnection getEnterpriseConnection() throws ConnectionException {        ConnectorConfig config = new ConnectorConfig();
        config.setUsername("username"); //Actually specify the sandbox login account
        config.setPassword("password"); //Actually specify the sandbox login password

        return Connector.newConnection(config);
    }

4.3. Upload static resources

createStaticResource


    private static void createStaticResource(EnterpriseConnection connection) throws IOException, ConnectionException{
        
        StaticResource sr = new StaticResource();
        sr.setName("test");
        // STATICRESOURCE_FILE_PATP:Full path of static resource file
        byte[] Body = Files.readAllBytes(Paths.get(STATICRESOURCE_FILE_PATH));
        sr.setBody(Body);
        sr.setCacheControl("Public");
        sr.setContentType("application/json");

        SaveResult[] ret = connection.create(new SObject[]{sr});
        for(SaveResult r: ret) {
            System.out.println(r.getId());
            System.out.println(r.getSuccess());
            if (r.getErrors().length > 0) {
                System.out.println(r.getErrors()[0]);
            }
        }
    }

4.4. Upgrade static resources

updateStaticResource


    private static void updateStaticResource(EnterpriseConnection connection) throws IOException, ConnectionException{
        // QUERY_STATICRESOURCE_SQL:SOQL for inspecting static resources(select id form staticresource where name = 'test')
        QueryResult qr = connection.query(QUERY_STATICRESOURCE_SQL);
        SObject[] srs = qr.getRecords();
        StaticResource sr = (StaticResource)srs[0];
        byte[] Body = Files.readAllBytes(Paths.get(STATICRESOURCE_FILE_PATH));
        sr.setBody(Body);
        SaveResult[] ret = connection.update(new SObject[]{sr});
        for(SaveResult r: ret) {
            System.out.println(r.getId());
            System.out.println(r.getSuccess());
            if (r.getErrors().length > 0) {
                System.out.println(r.getErrors()[0]);
            }
        }
    }

4.5. Main method

main


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

        EnterpriseConnection connection = getEnterpriseConnection();
        // createStaticResource(connection);
        updateStaticResource(connection);
    }

reference

1. Metadata API 2. Force.com Web Service Connector 3. Call Salesforce SOAP API from Java

Recommended Posts

[Salesforce] Registering and updating static resources with Tooling API (Java sample SOAP API)
Tips for using Salesforce SOAP and Bulk API in Java
Generate Java client code for Salesforce SOAP API
Handle Java 8 date and time API with Thymeleaf with Spring Boot
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Sample code to parse date and time with Java SimpleDateFormat
[Java] Stack area and static area
[Java] JavaConfig with Static InnerClass
Sample of using Salesforce's Bulk API from Java client with PK-chunking