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.
Please refer to the Salesforce manual.
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>
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.
.classpath
<classpathentry kind="lib" path="C:\xxx\.jar\enterprise.jar"/>
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);
}
1. Metadata API 2. Force.com Web Service Connector 3. Call Salesforce SOAP API from Java
Recommended Posts