Update Record In CRM Module Using SDK PHP

The SDK is an open-source PHP library that is used for integrating your PHP application.
Here we learn how to Update Records with ZOHO API using SDK in PHP.

To integrate ZOHO API Using SDK follow below step,

1) You need to create a ZOHO Application. If you have not created then please create by using the below link,

https://accounts.zoho.com/developerconsole

2) We need to include the vendor/autoload.php file. so first we need to install PHP SDK using composer.

  • Run the below command for install composer
    To install composer on mac/ Linux system use the below link:
    https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx

    To install composer on Windows system use the below link:

    https://getcomposer.org/doc/00-intro.md#installation-windows
  •  Install PHP SDK
    1) Give the path of your client app(In which you want to add vendor folder).
    2) Run the command below:
    composer require zohocrm/php-sdk
  • The PHP SDK will be installed in your application and a package named vendor would be created.

3) A refresh token is required to use ZOHO REST API. Refer to the below URL to generate a refresh token.

https://www.thecodehubs.com/generate-refresh-token-for-zoho/

4) Create config.php file and add below code,

<?php

return array (
  'userIdentifier'	   => 'testingtest@gmail.com',
  'client_id'		   => '1000.IUDYQTEROO1024839NQXWAEK2C3Y5H',
  'client_secret'    	   => '3a0cc0d0a4fb74a808f6f4fdb75391645131765bbd',
  'redirect_uri'	   => 'http://api.testing.com/sdk/function.php',
  'token_persistence_path' => 'zcrm_oauthtokens.txt',
  'scope'		   => 'ZohoCRM.modules.ALL',
  'refresh_code'	   => 'refresh code',
);

Here,
client_id: This is getting from your Zoho app or Zoho account.
client_secret:
This is getting from your Zoho app or Zoho account.
redirect_uri:  This Callback URL that you registered during the Zoho app registration.
token_persistence_path: is a path for token storage;
Scope:  Choose what data can be accessed by your application.
refresh code: Code which is getting from the access token.

4) Create an update-record.php file and add below code

<?php
use zcrmsdk\crm\crud\ZCRMInventoryLineItem;
use zcrmsdk\crm\crud\ZCRMRecord;
use zcrmsdk\crm\crud\ZCRMTax;
use zcrmsdk\crm\setup\restclient\ZCRMRestClient;
require 'vendor/autoload.php';

$configs = include("config.php");
$client_id     = $configs['client_id'];
$client_secret = $configs['client_secret'];
$redirect_uri  = $configs['redirect_uri'];
$identifier    = $configs['userIdentifier'];
$token_persistence_path = $configs['token_persistence_path'];

class Update_contacts{
    public function __construct()
    {
      global $client_id, $client_secret, $redirect_uri, $identifier, $token_persistence_path; 
        $configuration = array(
              "client_id"              => $client_id,
              "client_secret"          => $client_secret,
              "redirect_uri"           => $redirect_uri,
              "currentUserEmail"       => $identifier,
              "token_persistence_path" => $token_persistence_path
        );
        ZCRMRestClient::initialize($configuration);
    }
   
     /* update record */
     public function update_record(){
          $recordUp = ZCRMRestClient::getInstance()->getRecordInstance("{module_name}", "{record_id}");
          $recordUp->setFieldValue("Subject", "test2312"); // set fieldname with value
          $recordUp->setFieldValue("Account_Name", "{account_id}");	
          $trigger  = array();//triggers to include
          $responseUp = $recordUp->update($trigger); // to update the record
              echo "HTTP Status Code:" . $$responseUp->getHttpStatusCode(); // To get http response code
              echo "Status:" . $responseUp->getStatus(); // To get response status
              echo "Message:" . $responseUp->getMessage(); // To get response message
              echo "Code:" . $responseUp->getCode(); // To get status code
              echo "Details:" . json_encode($responseUp->getDetails());
    }

     /* update record by id */
     public function update_record_by_id(){
          $recordUp = ZCRMRestClient::getInstance()->getRecordInstance("{module_name}", "{record_id}");
          $recordUp->setFieldValue("Subject", "test2312"); // set fieldname with value
          $recordUp->setFieldValue("Account_Name", "{account_id}");	
          $trigger  = array();//triggers to include
          $responseUp = $recordUp->update($trigger); // to update the record
              echo "HTTP Status Code:" . $responseUp->getHttpStatusCode(); //  http response code
              echo "Status:" . $responseUp->getStatus(); // response status
              echo "Message:". $responseUp->getMessage(); // response message
              echo "Code:"   . $responseUp->getCode(); // status code
              echo "Details:". json_encode( $responseUp->getDetails() );
    }
    
    /* upsert */

      public function upsert() {
           $moduleUp = ZCRMRestClient::getInstance()->getModuleInstance("Module_name");
	   $records = array();
	   $record = ZCRMRecord::getInstance("{record_id}", null); 
	   $record->setFieldValue( "Company", "test" );
	   $record->setFieldValue( "Email", "testingtest@gmail.com" );
           $record->setFieldValue( "Last_Name", "testing" );
	   array_push($records, $record);
	   $duplicate_check_fields = array('Company'); // duplicate record by company
	   $trigger = array();
	   $responseUps = $moduleUp->upsertRecords( $records,null, $duplicate_check_fields );
	   foreach ( $responseUps->getEntityResponses() as $responseUp ) {
             echo "HTTP Status Code:" . $responseUp->getHttpStatusCode(); // http response code
             echo "Status:"           . $responseUp->getStatus(); // response status
             echo "Message:"          . $responseUp->getMessage(); // response message
             echo "Code:"             . $responseUp->getCode(); // status code
             echo "Details:"          . json_encode( $responseUp->getDetails() );
           }
	}
}

$obj = new Update_contacts();
$obj->update_record();
$obj->update_record_by_id();
$obj->upsert();

Where,
Module_name
: The API name of the module
record_id: Specific Record id which you want to update.
Possible_Module_name: leads, accounts, contacts, deals, campaigns, tasks, cases, events, calls, solutions, products, vendors, pricebooks, quotes, salesorders, purchase orders, invoices, custom, and notes.
duplicate check fields for different modules:
Leads – Email, Accounts – Account_Name, Contacts – Email, Deals – Deal_Name, Campaigns – Campaign_Name, Cases – Subject, Solutions – Solution_Title, Products – Product_Name, Vendors – Vendor_Name, PriceBooks – Price_Book_Name, Quotes – Subject, SalesOrders – Subject, PurchaseOrders – Subject, Invoices – Subject, CustomModules – Name

While Upserting there are some Mandatory fields that you need to mention.

Module mandatory field
Leads Last_Name – Single Line
Contacts Last_Name – Single Line
Accounts Account_Name – Single Line
Deals Deal_Name- Single Line
Stage – Picklist
Tasks Subject – Multi-Line
Calls Subject – Multi-Line
Call_Type – Picklist
Call_Start_Time – Date/Time
Call_Duration – Single Line
Events Event_Title- Single Line
Start_DateTime – Date/Time
End_DateTime – Date/Time
Products Product_Name – Single Line
Quotes Subject- Single Line
Product_Details – Product Line Item
Invoices Subject- Single Line
Product_Details – Product Line Item
Campaigns Campaign_Name – Single Line
Price Books Price_Book_Name- Single Line
Pricing_Details- JSON Array with “from_range”, “to_range”, “discount”
Cases Case_Origin – Picklist
Status- Picklist
Subject – Single Line
Solutions Solution_Title- Single Line
Purchase Orders Subject- Single Line
Vendor_Name- Lookup
Product_Details – Product Line Item
Sales Orders Subject- Single Line
Product_Details – Product Line Item

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories