Salesforce Deployment
By: Muhammad Sabir
Tags: Technology, Salesforce, CRM

Salesforce Deployment

There are many ways to deploy to Salesforce platform, using Change Sets, Eclipse, ANT or any combination of these. All of these techniques have different pros and cons and have been discussed million times by now. I am going to discuss only ANT which looks the perfect tool to me. Here is the three-step deployment process.

First create a list of artifacts in an XML file. These artifacts are classes, pages, workflows, approval processes and e.t.c. This list is called package.xml in ANT language. Its a very simple XML document with very few tags inside. After creating the package.xml, you retrieve these artifact’s source code from your source salesforce server which is your sandbox most of the time and save them locally. This can be done by executing one of the tasks of ANT and package.xml from previous step is used for this ANT task. When you have all the artifacts and source code downloaded locally, you push them to your target salesforce server which is a production server most likely.

#1 is time taking and tedious job and has more chances of important artifacts not being included in the list and is done manually by administrators, architects and developers. When you miss an artifact, you pay the price by a failed deployment.

In order to avoid any error, I have created a tool called Package Generator which generates package.xml file for all the artifacts which have been created/modified since your last deployment. Its an ANT task and works just like other tasks provided by Salesforce. It uses metadata API’s to pull metadata and filter them based on the create/modified date. Its available to download from here.

#2 and #3 are also done by executing ANT tasks provided by Salesforce.

In order to setup for ANT, please follow the steps in this guide. Please note that you will need to install JDK version 8 since package generator task is not supported by older Java versions.

After you are done with the guide, please go to this repository and download packagegenerator.jar file and copy into the lib folder inside your ANT home.

Now you are ready to rock. Execute all three tasks generatepackage, retrievepackage and deploypackage from following build.xml.

<project name=”Deployment Script”

default=”generatepackage” basedir=”.”

xmlns:sf=”antlib:com.salesforce”> <property file=”build.properties”/>



<target name=”generatepackage”

description=”Generates package.xml”>

<taskdef name=”packagegenerator”

classname=”org.sf.tools.PackageGeneratorTask”

classpath=”packagegenerator.jar”>

</taskdef>

<packagegenerator username=”${sf.test.username}”

password=”${sf.test.password}”

serverurl=”${sf.sandbox.serverurl}”

changeDate=”10/31/2014 13:30"

outputFile=”packages/package.xml”

metadataTypes=””>

</packagegenerator>

</target> <target name=”retrievepackage”>

<mkdir dir=”source-v1"/>

<sf:retrieve username=”${sf.test.username}”

password=”${sf.test.password}”

serverurl=”${sf.sandbox.serverurl}”

maxPoll=”${sf.maxPoll}”

pollWaitMillis=”${sf.pollWaitMillis}”

retrieveTarget=”source-v1"

unpackaged=”packages/package.xml”>

</sf:retrieve>

</target> <target name=”deploypackage">

<sf:deploy username=”${sf.prod.username}”

password=”${sf.prod.password}”

serverurl=”${sf.prod.serverurl}”

maxPoll=”${sf.maxPoll}”

pollWaitMillis=”${sf.pollWaitMillis}”

deployRoot=”source-v1”

rollbackOnError=”true”

logType=”Debugonly”

trace=”true”

checkOnly=”false”>

</sf:deploy>

</target> </project>

generatepackage Task

First you declare the ANT target and name it. This name is used to call it from command prompt just like any other ANT task. After that, you define the task using taskdef tag since its not a default task by salesforce but a custom task and than after the definition, you basically invoke it and pass all the parameters needed for the task. Parameters include username, password and server url. This task pull all the artifacts which have been created or modified after the changeDate and create the outputFile. Parameter changeDate can have date with/without time. When time is not mentioned, it assumes it as 23:59.

retrievepackage Task

This task is provided by salesforce. Please take a look here for the details

deploypackage Task

This task is also provided by salesforce. Please take a look here for the details.

Recommendations

Avoid using special characters and spaces in the names of artifacts like email templates and layouts. ANT tries to encode and decode those special characters but fails.

ANT retrieve task retrieves profiles with only the permissions for the objects/artifacts which are part of the package, which means it will not pull the permissions which are not relevant or are not modified, so you do not need to manually set permissions after the deployment. Profiles are perfect to move using ANT.

If you have created a new profile out of an existing one, DONT create it using ANT, instead create it manually and then update using ANT otherwise profile will not have those permissions which were added/modified after you forked it.

Take a look at package.xml file after you execute packagegenerator task and remove all irrelevant artifacts which can appear due to other developers working on the same box or testing artifacts. Keep the list which is relevant to your stuff.

Have fun and let me know if you find any problems with the packagegenerator. Feel free to clone and modify it.