Pages

Thursday, August 18, 2016

Salesforce Migration Tool using ANT in Windows


Prerequisites

1. Java
Make sure JDK is installed in your computer.
Check it from Control Panel\All Control Panel Items\Programs and Features



** if you do not have JDK installed, download latest JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html

Once installed successfully, check from command prompt:
C:\>java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode, sharing)

** if your JAVA_HOME variable and environment Path have not configured, follow this step:
- Go to Advanced system settings from Control Panel\All Control Panel Items\System, click Environment Variables... button, go to System variables
- New variable JAVA_HOME value C:\Program Files\Java\jdk1.8.0_102 
- Edit Path variable to add value %JAVA_HOME%\bin


2. ANT
Get latest Apache ANT from http://ant.apache.org/bindownload.cgi and select .zip archive file.
Download the file to a folder and extract it.

You need to add this into System variables from Advanced system settings from Control Panel\All Control Panel Items\System, click Environment Variables... button.
- New variable ANT_HOME value C:\SFDC\Ant\apache-ant-1.9.7-bin\apache-ant-1.9.7
- Edit Path variable to add value %ANT_HOME%\bin

Once installed successfully, check from command prompt:
C:\>ant -version
Apache Ant(TM) version 1.9.7 compiled on April 9 2016


3. Force.com Migration Tool
Download Migration Tool from https://developer.salesforce.com/page/Force.com_Migration_Tool, at this moment it will bring me to https://gs0.salesforce.com/dwnld/SfdcAnt/salesforce_ant_37.0.zip, this may change based on Salesforce release. Extract salesforce_ant_37.0.zip to a folder.


Concept

You need to work with 3 files:

1. build.properties
Where? to store variables: username, password, server url
You can find sample of this file from "sample" folder from salesforce_ant_37.0.zip
In this file, you need define: serverurl, username, and password. Sample:
sf.serverurl = https://login.salesforce.com
sf.username = mylogin@gmail.com
sf.password = mypassword8

2. build.xml
How to deploy? retrieve, deploy
This file also available in "sample" folder from salesforce_ant_37.0.zip
In this file, you need to define the target. The concept here is you need to retrieve the metadata from source org., then deploy it to target org.

For retrieve
<target name="retrieve">  
  <!--<mkdir dir="retrieve_folder"/> -->
  <sf:retrieve username="${sf.username}" 
               password="${sf.password}" 
               sessionId="${sf.sessionId}"                 
               serverurl="${sf.serverurl}" 
               maxPoll="${sf.maxPoll}" 
               retrieveTarget="retrieve_folder" 
               unpackaged="package.xml" />
</target>

** mkdir retrieve_folder, this line is just needed if you need the script to create the folder prior retrieval, you can change retrieve_folder to other folder as you want.

For deployment
<target name="deploy"> 
  <sf:deploy username="${sf.username}" 
             password="${sf.password}" 
             sessionId="${sf.sessionId}" 
             serverurl="${sf.serverurl}" 
             maxPoll="${sf.maxPoll}" 
             deployRoot="retrieve_folder" 
             rollbackOnError="true"/>
</target>

** see more parameters here

3. package.xml
What to Deploy?
You can find sample of this file from "sample\codepkg" folder from salesforce_ant_37.0.zip
You need to manually add/update all the components you want to deploy.
Sample:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata"> 
  <types>        
    <members>Access_Account</members>
    <members>View_All_Data</members>
    <name>PermissionSet</name>
  </types>
  <types>
    <members>Change__c</members>
    <name>CustomObject</name>
  </types> 
  <types>
    <members>Sales Regional</members>
    <name>Profile</name>
  </types> 
  <version>37.0</version>
</Package>

For me personally, I prefer to create a folder and put all 3 files above into that folder for each deployment.

For complete Metadata Components and Types, see https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_objects_intro.htm


Action

This is exciting part where you will start using ANT

To retieve
C:\SFDC\Ant\salesforce_ant_37.0\sample>ant retrieve
Buildfile: C:\SFDC\Ant\salesforce_ant_37.0\sample\build.xml

retrieve:
[sf:retrieve] Request for a retrieve submitted successfully.
[sf:retrieve] Request ID for the current retrieve task: 09S17000001OuaREAS
[sf:retrieve] Waiting for server to finish processing the request...
[sf:retrieve] Request Status: Pending
[sf:retrieve] Request Status: Succeeded
[sf:retrieve] Finished request 09S17000001OuaREAS successfully.

BUILD SUCCESSFUL
Total time: 28 seconds

To deploy
C:\SFDC\Ant\salesforce_ant_37.0\sample>ant deploy
Buildfile: C:\SFDC\Ant\salesforce_ant_37.0\sample\build.xml

deploy:
[sf:deploy] Request for a deploy submitted successfully.
[sf:deploy] Request ID for the current deploy task: 0Af21000002F9xACAS
[sf:deploy] Waiting for server to finish processing the request...
[sf:deploy] Request Status: Pending
[sf:deploy] Request Status: InProgress
[sf:deploy] Request Status: Succeeded
[sf:deploy] *********** DEPLOYMENT SUCCEEDED ***********
[sf:deploy] Finished request 0Af21000002F9xACAS successfully.

BUILD SUCCESSFUL
Total time: 30 seconds



Reference: