Online Help > API Guide > API - Examples

API - Examples

Tags:  

Example 1: Get All Records from the "Contacts" Module

Programming Language

  • JAVA

Prerequisite

  • JDK 1.6
  • commons-codec-1.3.jar, commons-httpclient-3.0.1.jar and commons-logging-1.1.jar files are available in the CLASSPATH

Code Snippet

import java.io.*;
import java.util.*;
import java.net.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.PartSource;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;

public class TestAPI
{
    public static void main(String a[])
    {   
        try   
        {
            /*----------------------------Fetch Ticket from your Zoho CRM Account---------------------------- */
            String ticket = getIAMTicket("ZohoCRM","yourusername","yourpassword");
            String apikey = "xxxxxxxxxxxxxxxxx"; //your API key
            /*----------------------------Fetch Ticket from your Zoho CRM Account ---------------------------- */

            String targetURL = "http://crm.zoho.com/crm/private/xml/Contacts/getAllRecords";
            String paramname = "content";
            PostMethod post = new PostMethod(targetURL);
            post.setParameter("ticket",ticket);
            post.setParameter("apikey",apikey);
            HttpClient httpclient = new HttpClient();
            PrintWriter myout = null;

            /*-------------------------------------- Execute the http request--------------------------------*/
            try
            {
                long t1 = System.currentTimeMillis();
                int result = httpclient.executeMethod(post);
                System.out.println("HTTP Response status code: " + result);
                System.out.println(">> Time taken " + (System.currentTimeMillis() - t1));
            /*-------------------------------------- Execute the http request--------------------------------*/
               
                /* ---------------------------writing the response to a file--------------------*/
                myout = new PrintWriter(new File("response.xml"));
                myout.print(post.getResponseBodyAsString());
               /* ---------------------------writing the response to a file--------------------*/
               
                /*-----------------------Get response as a string ----------------*/
                String postResp = post.getResponseBodyAsString();
                System.out.println("postResp=======>"+postResp);
                /* ---------------------Get response as a string ----------------------------*/
                 if(postResp.equals("Invalid Ticket Id"))
                 {
                     // generate the ticket id  again and call the API
                 }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }   
            finally
            {
                myout.close();
                post.releaseConnection();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
    }
    /*-------------------------Get Ticket ---------------------------------*/
    public static String getIAMTicket(String serviceName, String loginId, String password)
    {
        String strTicket = null;
        try {

            String iamUrl = "http://accounts.zoho.com/login?servicename="+serviceName+"&FROM_AGENT=true&LOGIN_ID="+loginId+"&PASSWORD="+password;
            URL u = new URL(iamUrl);
            HttpURLConnection c = (HttpURLConnection)u.openConnection();
            InputStream in = c.getInputStream();
            InputStreamReader ir=new InputStreamReader(in);
            BufferedReader br =new BufferedReader(ir);

            String strLine  = null;

            while ((strLine = br.readLine()) != null) {
                if(strLine != null && strLine.startsWith("TICKET")) {
                    strTicket = strLine.substring(7);
                }
            }

            in.close();
        }
        catch (Exception e){
            e.printStackTrace();

        }
        return strTicket;
    }
    /* --------------------Get Ticket----------------------------------*/
}
Download: Soruce Code

Example 2: Get All Records from the "Contacts" Module

Programming Language

  • PHP

Prerequisite

  • LAMP or WAMP

Code Snippet

<?php
$file = fopen("http://crm.zoho.com/crm/private/xml/Leads/getAllRecords?ticket=<ticket>&apikey=<apikey>", "r") or exit("Unable to open file!");
while(!feof($file))
  {
      $theData=fgets($file);
  }
fclose($file);
echo $theData;
?>


Example 3: Insert products into Invoice

API Method: insertRecords

XML Format: http:// crm.zoho.com/crm/private/xml/Leads/insertRecords?apikey=[API Key]&ticket=[Ticket]

XML Data:

<Invoices>
   <row no="1">
       ...all other invoice attributes...

        <fieldlabel value="Product Details">
            <product no="1">
                <fieldlabel value="Product Id">___your_zoho_productId___</fieldlabel>
                <fieldlabel value="Product Name">___your_zoho_product_name___</fieldlabel>
                <fieldlabel value="Quantity">1</fieldlabel>
                <fieldlabel value="List Price">1.00</fieldlabel>
                <fieldlabel value="Discount">0</fieldlabel>
                <fieldlabel value="Total">1.00</fieldlabel>
                <fieldlabel value="Total After Discount">1.00</fieldlabel>
                <fieldlabel value="Tax">0</fieldlabel>
                <fieldlabel value="Net Total">1.00</fieldlabel>
            </product>
       </fieldlabel>

      ...any other invoice attributes...
    </row>
</Invoices>


Example 4: Create multiple Potentials for Account

API Method: insertRecords

XML Format: http:// crm.zoho.com/crm/private/xml/Leads/insertRecords?apikey=[API Key]&ticket=[Ticket]

XML Data:

<Accounts>
<row no="1">
<fieldlabel value="AccountId">Your__Account__ID</fieldlabel>
<fieldlabel value="Email">test@test.test</fieldlabel>
<fieldlabel value="boolean flag">TRUE</fieldlabel>
<fieldlabel value="First contact">01.01.2009</fieldlabel>
<fieldlabel value="Last Login">05.10.2009</fieldlabel>
</row>
</Accounts>

<Potentials>
<row no="1">
<fieldlabel value="Potential Name">First Potential</fieldlabel>
<fieldlabel value="Description">description of the potential</fieldlabel>
<fieldlabel value="Closing Date">01.04.2009</fieldlabel>
<fieldlabel value="Account ID">Your__Account__ID</fieldlabel>
<fieldlabel value="Email">test@test.test</fieldlabel>
<fieldlabel value="Stage">"-data-"</fieldlabel>
<fieldlabel value="boolean flag">TRUE</fieldlabel>
<fieldlabel value="product">FREE</fieldlabel>
<fieldlabel value="Date of Birth">01.01.1970</fieldlabel>
<fieldlabel value="Mailing City">Germany</fieldlabel>
</row>
</Potentials>


Example 5: Insert Date Fields

API Method: insertRecords

XML Format: http:// crm.zoho.com/crm/private/xml/Leads/insertRecords?apikey=[API Key]&ticket=[Ticket]

XML Data:
<Accounts>
<row no="1">
<fieldlabel value="Account Name">TestUser</fieldlabel>
<fieldlabel value="Email">test@test.test</fieldlabel>
<fieldlabel value="boolean flag">TRUE</fieldlabel>
<fieldlabel value="First contact">01/01/2009</fieldlabel>
<fieldlabel value="Last Login">05/10/2009</fieldlabel>
</row>
</Accounts>'

Note: Date must be in MM/dd/yyyy format.



Related Topics

 

Submit Feedback

OR

Submit your App



 RSS of this page


New to Zoho CRM?  Sign up for Free!  or Request a Demo
 
© 2009, Zoho Corp. All rights reserved.