ant migration tool

Salesforce Ant Migration Tool - The Complete Guide

Ant Migration Tool is one of the three tools available for Salesforce deployment and it is a totally Java based command line for moving metadata between one organization to another Salesforce organization.

Certain steps to be followed to discuss ANT Migration Tool:

1.Prerequisite:

  1. Java version 7 (1.7).
  2. Download the .zip file of the Spring '20 Ant Migration Tool.(or whatever be the latest at the time of using the Ant Migration Tool).
  3. Save the .zip file locally, and extract the contents to the directory of your choice.

 2. Required Orgs for Ant Migration Tool:

a.One is Source Org.

b.Another is Target Org.
dont miss out iconDon't forget to check out: Deployment In Salesforce

3.Certain steps to be followed to discuss ANT Migration Tool:

  1. Make a Connected App in your target org.(In Lightning Component i.e. Setup > Apps > App Manager > New Connected App.

a.Click on New Connected App and fill the details as per your needs & your callback URL looks like this

Don't forget to check out: Creating Wrapper Class in Salesforce Visualforce Pages

https://<domain>.my.salesforce.com/services/oauth2/callback

b.Just click on Save and you will see the Client Id and Client Secret key(Save that one, we will need it for Integration).

2. Create an Apex Class in Target Org to retrieve 10 Contacts  from Target Org. Go to Developer Console and create a new Apex Class.

@RestResource(urlMapping='/v1/getContacts/*')
global with sharing class Retrieve_10_contacts {
    @HttpGet
    global static list&lt;contact&gt; retrieve_10_contacts(){
        RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
        List&lt;contact&gt; listContact =[Select Id , Name from Contact LIMIT 10 ];
        return listContact ;
    }
}

3. Create a Remote Setting on Source Org. Search “Remote Site Setting” in Quick find Box and Enter Remote Site URL of your Target org, the format is like that –

https://<domain>.my.salesforce.com

4. Create an Apex Class in Source Org, which will callout Apex class of Target Org and return you 10 Contacts.

Here is the code-

public class GetAccountUsingRESTAPI {
    private final String clientId = 'XXXXXXXXXXXXXXXX';
    private final String clientSecret = 'XXXXXX8XXX';
    private final String username = 'SalesforceUserName';
    private final String password = 'Password';
    public class deserializeResponse
    {
        public String id;
        public String access_token;
    }
    public String ReturnAccessToken (GetContactUsingRESTAPI acount)
    {
        String reqbody = 'grant_type=password&client_id='
        +clientId+'&client_secret='
        +clientSecret+'&username='
        +username+'&password='+password;
    
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setBody(reqbody);
        req.setMethod('POST');
        req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
    
        HttpResponse res = h.send(req);
        deserializeResponse response = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
        system.debug('@@@@access_token@@'+response );
        return response.access_token;
    }
    public static list callGetContact()
    {
        GetAccountUsingRESTAPI contact1 = new GetContactUsingRESTAPI();
        String accessToken;
        accessToken = contact1 .ReturnAccessToken (contact1 );
        list ListContact=new List();
        if(accessToken != null){
            String endPoint = 'https://avnish1.salesforce.com/services/apexrest/v1/getContacts/';
            Http h2 = new Http();
            HttpRequest req1 = new HttpRequest();
            req1.setHeader('Authorization','Bearer ' + accessToken);
            req1.setHeader('Content-Type','application/json');
            req1.setHeader('accept','application/json');
            req1.setMethod('GET');
            req1.setEndpoint(endPoint);

            HttpResponse res1 = h2.send(req1);
            String trimmedResponse = res1.getBody().unescapeCsv().remove('\\');
            system.debug('@@@RESPONSE@@'+trimmedResponse);

            JSONParser parser = JSON.createParser(res1.getBody());
            set conList=new set();

            while (parser.nextToken() != null) {

                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) ){
                    Contact con;
                    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == ‘Id’)) {
                        parser.nextToken();
                        String sId= parser.getText();
                        con=new Contact();
                        con.Id=sId;
                        system.debug(‘Id@@@’ + sId);

                        parser.nextToken();
                        if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
                        (parser.getText() == ‘Name’)) {
    
                            parser.nextToken();

                            string sName= parser.getText();
                            con.Name=sName;
                            system.debug(‘Name@@@’ + sName );
                        }
                    }
                    conList.add(con);
                }
                conList.remove(null);
            }
            ListContact.AddAll(conList);
            system.debug(‘ContactList@@@@’+Json.serialize(ListContact));
        }
        return ListContact;
    }
}

In the above code you will need to replace the following things:-

  • client
  • clientSecret
  • username
  • password

Also, Change the Endpoint in method ReturnAccessToken() in which org you are trying to log in. means where you have created your Connected Appreq.setEndpoint(‘https://login.salesforce.com/services/oauth2/token’);

Also, Change the Endpoint in method callGetContact() in which org you are trying to access all the Contacts. means where you have developed your Apex RestService

String endPoint = ‘https://deepak13.salesforce.com/services/apexrest/v1/getContacts/’;

Now call the function “callGetContact()”.

Feel free to ask questions in a comment box.

Thanks.

Happy Coding.

Popular Salesforce Blogs