Tuesday 31 January 2012

Problem: failed to create task or type antlib:com.salesforce:deploy

Hi, Today while trying to deploy my org's metadata on production using Salesforce's migration tool I faced the error "Problem: failed to create task or type antlib:com.salesforce:deploy".

I was using Ubuntu 11.04 (It should be useful in MAC systems as well), so i discovered the solution to this problem is we need to download the salesforce migration tool from our deployment org's Your Name | Setup | Develop | Tools.

After downloading the zip file on your drive, extract the content and copy the ant-salesforce.jar to /usr/share/ant/lib/ 

This should resolve the particular error.





Monday 30 January 2012

Download Salesforce Attachments on Drive.

Many a times in salesforce we need to download selective attachments, in that case the SFDC's Data Export tool doesn't seem helpful as it will download all the attachments (yes you can select objects but not the records in that object). So for this purpose i have written a small Java code, that will take help of the enterprise wsdl of the org from which i need to extract the data.

To generate code stub from enterprise wsdl you need WSC jar, which can be downloaded easily from net, once you get it, go to command prompt and paste the below mentioned command, with your own directory structure and it will generate the required jar to make connections to your org.

java -classpath  < YOUR PATH OF WSC JAR>\wsc-20.jar com.sforce.ws.tools.wsdlc  <PATH where you have kept your enterprise wsdl> \enterprise.wsdl  <PATH where you want to genrate the jar> \Enterprise.jar

Include this newly generated jar into your Java(Eclipse's) build path, and you are ready to make connection to your org.

This code will also create a CSV file to relate the parent with that to its attachment(s).


package com.mypackage;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Attachment;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class ExportAttachments {
 
  static final String USERNAME = "<YOUR USER NAME>";
  static final String PASSWORD = "<YOUR PASSWORD>";
  static EnterpriseConnection connection;
  static final String ATTACHIDS ="00P50000007HYMJEA4,00P50000007HmAjEAK,00P50000004CmZ3EAK";
  static final String AccountIds = "0015000000SQSmIAAX,0015000000bBP1pAAG,0015000000bCfjTAAS,0015000000LKHCFAA5";
 
  static List<String> str = new ArrayList<String>();
  static List<String> stracct = new ArrayList<String>();
  static Map<String,String> idMap = new HashMap<String,String>();
  static Map<String,String> csvMap = new HashMap<String,String>();
 

  public static void main(String[] args) {

    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(USERNAME);
    config.setPassword(PASSWORD);
    //config.setTraceMessage(true);
    str = Arrays.asList(ATTACHIDS.split(","));
    stracct = Arrays.asList(AccountIds.split(","));
    for(String s:stracct){
        idMap.put(s,s);
    }
    try {
     
      connection = Connector.newConnection(config);
     
      // display some current settings
      System.out.println("Auth EndPoint: "+config.getAuthEndpoint());
      System.out.println("Service EndPoint: "+config.getServiceEndpoint());
      System.out.println("Username: "+config.getUsername());
      System.out.println("SessionId: "+config.getSessionId());
   
      queryAccounts();
      createCSV();
      queryAttachments();
     
    } catch (ConnectionException e1) {
        e1.printStackTrace();
    } 

  }
 
 private static void queryAccounts() {
       
        
        try {
          for(String s:idMap.keySet()){
          // query for the 5 newest contacts     
          QueryResult queryResults = connection.query("SELECT Id, VSX_ID__c " +
                  "FROM Account WHERE Id in ('"+s+"')");
          if (queryResults.getSize() > 0) {
            for (int i=0;i<queryResults.getRecords().length;i++) {
              // cast the SObject to a strongly-typed Account
              Account a = (Account)queryResults.getRecords()[i];
              csvMap.put(a.getId(),a.getVSX_ID__c());
            }
          }
           }
        } catch (Exception e) {
          e.printStackTrace();
        }   
       
      }
 
  private static void queryAttachments() {
      try{
          for(String s:str){
           QueryResult queryResults = connection.query("Select Id, ParentId, Name, ContentType, Body " +
            "From Attachment WHERE id in('"+s+"')");
       
           if (queryResults.getSize() > 0) {
               for (int i=0;i<queryResults.getRecords().length;i++) {
                  // cast the SObject to a strongly-typed Contact
                    Attachment a = (Attachment)queryResults.getRecords()[i];
                    writeOnDisk(a.getId()+"-"+a.getName(),a.getBody());
                    System.out.println("Id: " + a.getId() + " - Name: "+a.getName()+" "+" - Account: "+a.getParentId());
                }
              }
          }
      }catch (Exception e) {
          e.printStackTrace();
        }   
     
     
  }
 
  private static void writeOnDisk(String fileName, byte[] bdy){
      try
      {
          String filePath = "C://Documents and Settings//varun//Desktop//JavaExport//"+fileName;
          FileOutputStream fos = new FileOutputStream(filePath);//File OutPutStream is used to write Binary Contents like pictures
          fos.write(bdy);
          fos.close();
      }
      catch (IOException e)
      {
      System.out.println(e.getMessage());
      }
  }
 
  private static void createCSV(){
      try
      {   
          String toWrite="";
   
          if(csvMap!=null){
              for(String s:csvMap.keySet()){
                  toWrite +="\""+s+"\""+","+"\""+csvMap.get(s)+"\"\n";
              }
         
          System.out.println(toWrite);
          String file_name = "C://Documents and Settings//varun//Desktop//JavaExport/AccountVSX.csv";
          FileWriter file = new FileWriter(file_name);
          BufferedWriter out = new BufferedWriter (file);
          out.write(toWrite);
          out.close();
          }
      }
      catch (IOException e)
      {
          System.out.println(e.getMessage());
      }
  }


}