SugarCRM REST API如何获取模块的团队列表

时间:2013-06-11 00:08:32

标签: rest sugarcrm

有没有办法通过使用SugarCRM Pro的REST API获取当前模块记录所属的团队列表?

我已经通过将关系数组传递给'Teams'来尝试get_entry_list - 这将返回空数组。

它应该与get_user_team_id类似,但对于特定模块。

我也尝试了get_relationships方法,但也没有运气。

我的猜测是因为团队关系是Link clas,而不是Link2和所有其他模块。

2 个答案:

答案 0 :(得分:0)

为了检索团队,我相信您需要执行以下操作:

1)使用get_entry_list检索记录,确保返回team_set_id字段。 2)一旦你拥有了team_set_ids,你就可以对模块'TeamSets'进行另一次get_entry_list调用,过滤查询参数中的那些id。然后,您可以使用“link_name_to_fields_array”参数返回团队集中的各个团队。

下面是一个例子

<?php

$url = "http://sugar_url/service/v4_1/rest.php";
$username = "admin";
$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;

//get list of records --------------------------------

$get_entry_list_parameters = array(

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

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

    //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(
        array(
            'name' => 'teams',
            'value' => array(
                'id',
                'name'
            ),
        ),
    ),


    //The maximum number of results to return.
    'max_results' => '100',

    //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);

echo '<pre>';
print_r($get_entry_list_result);
echo '</pre>';

?>

答案 1 :(得分:0)

是。在请求中指定team_name字段:

https://xxx.sugarondemand.com/rest/v10/Accounts/7e785902-ff39-4a54-87a4-e0d38b6b6ed1?&fields=name,team_name

响应:

{
   "id": "7e785902-ff39-4a54-87a4-e0d38b6b6ed1",
   "name": "15527-1",
   "date_modified": "2016-11-28T12:05:17-05:00",
   "team_name":    [
            {
         "id": "5272c830-5339-11e6-9195-06ef03a1f4b3",
         "name": "Team1",
         "name_2": "",
         "primary": true
      },
            {
         "id": "370d8fe4-e431-79fb-ec42-5565ecba3187",
         "name": "Web Service",
         "name_2": "CRM",
         "primary": false
      }
   ],
   "_acl": {"fields": {}},
   "_module": "Accounts"
}