Following are the many tools available for Salesforce deployment like
- Change sets (From Salesforce site)
- Eclipse (Using “Deploy to force.com server” option in Eclipse)
- ANT (Java based tool)
We are going to discuss the ANT based migration, step by step:
Prerequisite:
JDK 1.5 or above
Step 1:
Download ANT distribution from – “http://ant.apache.org/bindownload.cgi”
Step 2:
Set Environment variable “ANT_HOME“. The path should be of parent folder of “bin”. Also add the “bin” folder to your path.
Step 3:
Check whether ANT is installed or not properly by running command “ant -version“. It might be possible that you receive message something like unable to find tools.jar. You can copy this jar from “JDK_HOME/lib/tools.jar” to “JRE/lib” folder.
In above screen you can see that before copying tools.jar I was getting warning.
Step 4:
Login to salesforce and navigate to “Your Name |Setup | Develop | Tools” and download “Force.com Migration tool”.
Unzip the downloaded file to the directory of your choice. Copy the “ant-salesforce.jar” file from the unzipped file into the ant lib directory.
To start with deployment using ANT, we will need “build.xml” and “build.properties” file. As there is no need of “build.properties” however its good to have it so that the configuration related settings are in different file. You can copy both files from “sample” folder of unzipped content from salesforce. Following is the structure of “build.properties” file.
# build.properties # Specify the login credentials for the desired Salesforce organization sf.username = <SFDCUserName> sf.password = <SFDCPasswrd> #sf.pkgName = <Insert comma separated package names to be retrieved> #sf.zipFile = <Insert path of the zipfile to be retrieved> #sf.metadataType = <Insert metadata type name for which listMetadata or bulkRetrieve operations are to be performed> # Use 'https://login.salesforce.com' for production or developer edition (the default if not specified). # Use 'https://test.salesforce.com for sandbox. sf.serverurl = https://test.salesforce.com
Step 5:
Copy all folders with source code from source organization using eclipse. Lets say the root folder name is “test1”.
Create “build.properties” from above code snippet or copy it from unzipped folder. Now create “build.xml” needed by ANT. Following is the example of build.xml file:
<project name="Shivasoft ANT Tutorial" default="deployCode" basedir="." xmlns:sf="antlib:com.salesforce"> <property file="build.properties"/> <property environment="env"/> <!-- Shows deploying code &amp;amp;amp; running tests for code in directory --> <target name="deployCode" depends="proxy"> <sf:deploy username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" deployRoot="test1"> <runTest>TestClassName</runTest> </sf:deploy> </target> <!-- Shows removing code; only succeeds if done after deployCode --> <target name="undeployCode"> <sf:deploy username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" deployRoot="removecodepkg"/> </target> <target name="proxy"> <property name="proxy.host" value=" ProxyURL " /> <property name="proxy.port" value="1234" /> <property name="proxy.user" value="UserName" /> <property name="proxy.pwd" value="Password" /> <setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}" proxyuser="${proxy.user}" proxypassword="${proxy.pwd}" /> </target> </project>
Attribute “deployRoot” means the root folder from which the code should be copied and tag means the testclasses which should run after the deployment. At last, go to the folder “test1” from command line and run command “ant deployCode”.
Checking the status of the task:
To know the status of task you can run command:
Ant targetName -Dsf.asyncRequestId=requestID
Using Proxy in ANT migration tool:
If your organization uses the proxy then add below target in “build.xml” and specify this target as dependent.
<target name="proxy"> <property name="proxy.host" value=" ProxyURL " /> <property name="proxy.port" value="1234" /> <property name="proxy.user" value="UserName" /> <property name="proxy.pwd" value="Password" /> <setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}" proxyuser="${proxy.user}" proxypassword="${proxy.pwd}" /> </target>
Retrieve content from Salesforce Organization:
create “package.xml” for the list of component to be retrieved and add following task in “build.xml”.
<!-- Retrieve an unpackaged set of metadata from your org --> <!-- The file unpackaged/package.xml lists what is to be retrieved --> <target name="test" depends="proxy"> <mkdir dir="retrieveUnpackaged"/> <!-- Retrieve the contents into another directory --> <sf:retrieve username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" retrieveTarget="retrieveUnpackaged" unpackaged="unpackaged/package.xml" unzip="false" /> </target>
“Unzip” attribute specifies that the code retrieved should be in Zip format or not. By default the value is true means it will not in zip format.
Delete components from Salesforce Organization using “destructiveChanges.xml” :
In some cases, we may want to delete some components like Object or fields from Salesforce Organization. In this case, Only “package.xml” will not work. We need to create “destructiveChanges.xml” file also. Syntax for this file is exactly same as of “package.xml”, except that here we cannot define wildcards. So, to undeploy anything from Salesforce org, we need two xml files – “package.xml” and “destructiveChanges.xml“.
Below is complete code of build.xml, which includes retrieve, Undeploy and Deploy commands.
<project name="Shivasoft Demo Org" default="deployCode" basedir="." xmlns:sf="antlib:com.salesforce"> <!-- Get all settings like Server Path, username and passwords from "build.properties" file --> <property file="build.properties"/> <property environment="env"/> <!-- Sequence 1 - Get All information from Source, Retrieve the contents into Src directory --> <target name="SFDCFetch"> <!-- --> <sf:retrieve username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" retrieveTarget="src" unpackaged="package.xml"/> </target> <!-- Sequence 3 - Deploy to Target System, Package.xml is present in Src folder --> <target name="deploy"> <sf:deploy username="${sf1.username}" password="${sf1.password}" serverurl="${sf.serverurl}" deployroot="Src"> </sf:deploy> </target> <!-- Sequence 2 - If you want to remove some components, destructiveChanges.xml and Package.xml present in Delete Folder --> <target name="unDeploy"> <sf:deploy username="${sf1.username}" password="${sf1.password}" serverurl="${sf.serverurl}" deployroot="Delete"> </sf:deploy> </target> </project>
You may also be interested to check how to automate daily Salesforce backup.
How to Create Change set from Package.xml
Step 1 : Create Empty Changeset, lets say its name is changeset2
Step 2 : add fullname tag in package.xml. Fullname needs to be the name of changeset created in Step 1
<?xml version="1.0" encoding="UTF-8"?> <Package xmlns="http://soap.sforce.com/2006/04/metadata"> <fullName>Changeset2</fullName> <types> <members>AdditionalQuestionTriggerHandler</members> ... ... </package>
Step 3 : src folder name which contains package.xml needs to have changeset name, in our case changeset2
How to Create Package.xml from Change set
Use below ANT script
<target name="retrieveChangeset"> <sf:retrieve username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" retrieveTarget="changesetfolder" packageNames="changesetname" /> </target>
Update [03-Apr-2017]
Question : Why Salesforce ANT Migration tool is giving wrong Username, Security token or password error, even when everything is correct ?
Ans : If your password contains “$”, then ANT tool ignores this characters. So, to correct this, we need to add one more “$”.
Question : I am getting unable to find tools.jar files error. I don’t have access to server so cannot place tools.jar manually. Can I fix this error using build.xml by placing jar files in relative folder?
Ans : You can include any jar file to be in class path. Add below lines in build.xml. All jar files are placed in lib folder.
<path id="class.path"> <pathelement location="${build}"/> <fileset dir="./lib"> <include name="**/*.jar" /> </fileset> </path> <taskdef resource="com/salesforce/antlib.xml" uri="antlib:com.salesforce" classpathref="class.path"/>
Question : Why I am getting error like java.lang.OutOfMemoryError: Java heap space while retrieving metadata from Salesforce.
Ans : It is possible that you are retrieving metadata of some big organization with lots of changes. In this case, Java JVM running on system needs more memory (RAM) in order to process it. It can be done in two ways
1. Before running ANT command, run below command which will set the RAM allocated for JVM to 1GB. If you are not a Windows user, then use export instead of set in below command.
set ANT_OPTS=-Xmx1g
2. Problem in above solution is that, you would need to execute set command every time before using ANT. For permanent solution, open ant.bat file in bin folder of ANT installation (Same installation folder referred in ANT_HOME). And, at very first line, write below command and save it.
set ANT_OPTS=-Xmx1g
I hope this article will help newbie to learn ant migration tool.
Leave a Reply