Syntax Highlighter JS

Friday, June 29, 2012

Downloading Visual Force pages that are rendered to PDF - Part 1

In this series of posts, we are going to look at how to build a tool in java that will:
  • Login into salesforce (via WSC and HTTP Call out with Session ID)
  • Query the database for a list of records
  • Build a URL from each record ID and use an HTTP call out to get the PDF
  • Save the PDF files to the local file system.
Being a rather technical post, you will need to already have eclipse installed and be familiar with Java development.

Start by opening eclipse (I recommend a new workspace for this) and creating a new java project.  My project was created in JavaSE-1.6.

Once your new project has been created, add a new java class (remember to generate the public static void main stub) to the src folder and give it a package name.  For the purposes of this example, we will use .com.test as the package and Main.Java as our class.

Next you will need to add the enterprise.jar and wsc-22.jar as an external jars in the reference libraries tab under the Java Build Path entry in your project properties.  Please note that you will have to generate the wsc-22.jar file.   This is because the WSDL will be different for each organization if they have custom objects.

Now add your imports (at the top of your class) for the newly added jar files

import com.sforce.soap.enterprise.Connector; import com.sforce.soap.enterprise.EnterpriseConnection; import com.sforce.soap.enterprise.QueryResult; import com.sforce.soap.enterprise.sobject.Opportunity; import; import;

Our example is going to be based on the opportunity object so if your are using a different object, be sure to change the .sobject.Opportunity line above to your object and/or add in additional objects.

Now you can add the following function and call it to get logged into salesforce:

private static boolean APILoginToSalesforce(String USERNAME, String PASSWORD) { boolean blnAPILoginToSalesforce = false; ConnectorConfig config = new ConnectorConfig(); config.setUsername(USERNAME); config.setPassword(PASSWORD); try { connection = Connector.newConnection(config); blnAPILoginToSalesforce = true; } catch (ConnectionException e1) { e1.printStackTrace(); } return blnAPILoginToSalesforce; } // APILoginToSalesforce

Don't for get to add your security token to the end of your password when calling this method. Now you can add the following method which will let you query against salesforce. private static QueryResult RunSOQLQuery(String SOQLQueryString) { QueryResult queryResults = new QueryResult(); try { queryResults = connection.query(SOQLQueryString); System.out.println("Found " + queryResults.getSize() + " records"); } catch (Exception e) { e.printStackTrace(); } return queryResults; }
Now we need a query result variable to hold our results. So add a line like this to your class:
private static QueryResult RecordsToProcess;

Now you can call the login function followed by the query function like so:

public static void main(String[] args) { boolean ReturnStatus; ReturnStatus = APILoginToSalesforce("USER@EMAIL.COM","PASSWORDandSecurityToken"); System.out.println("Logged in = " + ReturnStatus); if (ReturnStatus == true) { RecordsToProcess = RunSOQLQuery("SELECT ID, Name FROM Opportunity LIMIT 10"); } }

At this point you should now be logged into salesforce (via WSC) and able to query data.

In the part 2 of this series, we will look at how to download the files. For the full / working code of this example, please see the final post in this series.

No comments:

Post a Comment