AnsweredAssumed Answered

Relating records using Rest Api

Question asked by Kinjal Parikh on May 25, 2016

Hi,

We are trying to relate two modules Accounts and contacts using Rest Api but the problem is that we need to get all the ids of accounts module and then pass those ids to get_relationships function. Kindly find the below code

 

<?php

    $url = 'http://localhost/service/v4_1/rest.php ';

   $username = "******";

    $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;

 

$get_entry_list_parameters = array(

      

         //session id

         'session' => $session_id,

 

         //The name of the module from which to retrieve records

         'module_name' => 'Accounts',

 

         //The SQL WHERE clause without the word "where".

         'query' => "",

 

         //The SQL ORDER BY clause without the phrase "order by".

         'order_by' => "",

 

         //The record offset from which to start.

         'offset' => '0',

 

         //Optional. A list of fields to include in the results.

         'select_fields' => array(

              'id',

         ),

 

         /*

         A list of link names and the fields to be returned for each link name.

         Example: 'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))

         */

         'link_name_to_fields_array' => array(

         ),

 

         //The maximum number of results to return.

         'max_results' => '',

 

         //To exclude deleted records

         'deleted' => '0',

 

         //If only records marked as favorites should be returned.

         'Favorites' => false,

    );

 

    $get_entry_list_result = call('get_entry_list', $get_entry_list_parameters, $url);

 

  $array = $get_entry_list_result->id;

  echo $array;

 

$get_relationships_parameters = array(

 

         'session'=>$session_id,

 

         //The name of the module from which to retrieve records.

         'module_name' => 'Accounts',

 

         //The ID of the specified module bean.

         'module_id' => '$array',

 

         //The relationship name of the linked field from which to return records.

         'link_field_name' => 'contacts',

 

         //The portion of the WHERE clause from the SQL statement used to find the related items.

     

  'related_module_query' => '',

         //The related fields to be returned.

  'related_fields' => array(

  'id',

   'name',

   ),

 

         //For every related bean returned, specify link field names to field information.

         'related_module_link_name_to_fields_array' => array(

         ),

 

         //To exclude deleted records

         'deleted'=> '0',

 

         //order by

         'order_by' => '',

 

         //offset

         'offset' => 0,

 

         //limit

         'limit' => 500,

    );

 

$get_relationships_result = call("get_relationships", $get_relationships_parameters, $url);

 

    echo "<pre>";

    print_r($get_relationships_result);

    echo "</pre>";

?>

We want to get all the ids for accounts module in $array and then pass that variable in get_relationships function to get the related records for all accounts. Can someone help me please?

Outcomes