Wednesday, 27 November 2013

SugarCRM:: Retrieving Record of Multiple ID's from SugarCRM.

This post give demonstration of Retrieving record of Multiple ID's From SugarCRM.

<?php

    $url = "< YOUR SUGARCRM INSTANCE >/service/v4_1/rest.php";
    $username = "username";
    $password = "password";

    //function to make cURL request
    function call($method, $parameters, $url)
    {
        ob_start();
        $curl_request = curl_init();

        curl_setopt($curl_request, CURLOPT_URL, $url);
        curl_setopt($curl_request, CURLOPT_POST, 1);
        curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($curl_request, CURLOPT_HEADER, 1);
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

        $jsonEncodedData = json_encode($parameters);

        $post = array(
             "method" => $method,
             "input_type" => "JSON",
             "response_type" => "JSON",
             "rest_data" => $jsonEncodedData
        );

        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
        $result = curl_exec($curl_request);
        curl_close($curl_request);

        $result = explode("\r\n\r\n", $result, 2);
        $response = json_decode($result[1]);
        ob_end_flush();

        return $response;
    }

    //login ------------------------------------------------

    $login_parameters = array(
         "user_auth"=>array(
              "user_name"=>$username,
              "password"=>md5($password),
              "version"=>"1"
         ),
         "application_name"=>"RestTest",
         "name_value_list"=>array(),
    );

    $login_result = call("login", $login_parameters, $url);

    /*
    echo "<pre>";
    print_r($login_result);
    echo "</pre>";
    */

    //get session id
    $session_id = $login_result->id;

    //retrieve records --------------------------------------

    $get_entries_parameters = array(

         //session id
         'session' => $session_id,

         //The name of the module from which to retrieve records
         'module_name' => 'Accounts',

         //An array of SugarBean IDs
         'ids' => array(
             'd8ab358c-30c6-9744-130b-5292cbb3d686',
             '44357b2e-b0a6-219d-e1ce-5292cdba1965'
         ),

         //Optional. The list of fields to be returned in the results
         'select_fields' => array('id','name','billing_address_city','billing_address_country'),

         //A list of link names and the fields to be returned for each link name
         'link_name_to_fields_array' => array(
         ),
    );

    $get_entries_result = call("get_entries", $get_entries_parameters, $url);

    $filteredDetail =array();
    $j=0;
    foreach ($get_entries_result as $stdClass) {
        foreach($stdClass as $stdClassObject) {
            foreach($stdClassObject as $acc_details_package) {
                foreach($acc_details_package as $acc_details1) {
                   foreach($acc_details1 as $acc_details2) {
                     echo "<pre>";                  
                     $filteredDetail[$j] = $acc_details2;
                     echo "Data[$j] = ".$acc_details2."\n";
                     $j++;                  
                    }
                    echo "\n\n\n";
                }
            }
        }
    }

   $filtered_array = array();  $a = 0;
 
    for($index=1;$index<=sizeof($filteredDetail);$i++){
if((($index % 2)!= 0)) { $filtered_array[$a] = $filteredDetail[$index]; $index++; $a++;} else { $index++; }
    }

print_r($filtered_array);

?>

No comments:

Post a Comment