Site icon Salesforce News Technology Stuff

Setup ANT and Clone Change Set

ANT is a very useful tool in Salesforce. We are using it for very long time to do the migration and take backups. Deployment using change set is a time consuming task, sometimes we need to wait long time before they get available in target org. And if any error occurs then we need to repeat the same steps again. Second issue which we face is there is no standard option provided to clone an inbound change set to outbound change set.
So today we will setup ANT and Clone change set. We can follow the same steps to do migration from one org to another or generate package.xml using change set or unmanage package.

  1. Download the ant zip file and extract it in your PC. If Java is not already installed then download and install that as well.
  2. Now we need to setup these under Environment variable. So click on computer icon > properties > Advanced system settings > Environment variables > System Variables > New and add two variables. One is ANT_HOME and JAVA_HOME.
  1. After that we need to download setup file from salesforce. You can download that from here.
  2. Once download, extract this and you will find ant-salesforce.jar file. copy this file in lib folder of ant setup.
  3. You will also find build.xml and build.properties files. Keep these file safe as we will use them to deploy/retrieve metadata.
  4. Now for demo purpose I will use developer edition and we will clone a unmanaged package. The steps will be same if you want to clone a change set.
  5. Now set user credntials in build.properties. If you have whitelisted your ip in salesforce then you don’t need security token.
  1. Now we will retrieve our changeset/package components. For this we need to use this command in build.xml
<target name="test">
      <mkdir dir="retrieveCS"/>
      <!-- Retrieve the contents into directory 
      <sf:retrieve username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" retrieveTarget="retrieveCS" packageNames="ANT_demo"/>
</target>
  1. Once we suceessfully retrieve our metadata, just go to the retrieveCS directory and open package.xml. If we want we can also use this package.xml for any other purpose. With just one line of code we are able to generate package.xml.
  2. In package.xml find the full name part and update it to match outbound change set/ another package name.
  1. now we will deploy this metadata again in our org. It will populate the change set/ unmanage package.
<target name="test">
      <!-- Upload the contents of the "mypkg" package -->
      <sf:deploy username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="retrieveCS" rollbackOnError="true"/> 
</target>

So using just few lines of code, we have easily Setup ANT and Clone Change Set. We have many other options available which you can find in the sample build.xml which you will get with zip. I suggest you to try them in your developer edition org to get better experience without any risk. If you want to do migration, then simply remove the fullname, description attribute and while doing deployment update the credentials of target org and it will deploy the components.

To reterive metadata using package.xml, you can use below code.

<!-- Retrieve the contents into another directory -->
<sf:retrieve username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" retrieveTarget="retrieveUnpackaged" unpackaged="unpackaged/package.xml"/>
If you still didn't setup SFDX, then you can find complete details here.

Did you like the post or do you have any questions, let me know in comments. Hapapy programming 🙂

Exit mobile version