SugarCRM Rest API Example: How to get all contacts for an account

sugarcrmdevelopers —  July 11, 2013

This relies on the following PHP Wrapper Class: http://github.com/asakusuma/SugarCRM-REST-API-Wrapper-Class/

For a slightly better formatted answer see my
Stackoverflow Post Here.

<code>/**
 * returns an array of contacts that are related to the accountId passed as a param.
 * The array returned will be an array of associative arrays.
 * @param $accountId
 * @param array $contactSelectFields optional sets the different items to return, default includes id, email1, name, title, phone_work, and description
 * @return array
 *
 */
public function getAllContactsAtOrganization( $accountId, $contactSelectFields=array("id", "email1", "name", "title", "phone_work", "description")) {

    $sugar = new Sugar_REST( SUGAR_REST_URL, SUGAR_USER_NAME, SUGAR_PASSWORD);

    $fields = array( "Accounts" =&gt; array("id", "name"),
        "Contacts" =&gt;  $contactSelectFields);
    $options = array(
        'where' =&gt; "accounts.id='$accountId'"
    );
    $apiResult = $sugar-&gt;get_with_related("Accounts", $fields, $options );


    $contacts = array();
    foreach( $apiResult['relationship_list'][0]['link_list'][0]['records'] as $almostContact) {
        $curr = array();
        foreach($contactSelectFields as $key) {
            $curr[$key] = $almostContact['link_value'][$key]['value'];
        }
        $contacts[] = $curr;
    }

    //print_r($contacts);

    return $contacts;
}
</code>

Sample Return

<code>Array
(
    [0] =&gt; Array
        (
            [id] =&gt; 47e1376c-3029-fc42-5ae2-51aeead1041b
            [email1] =&gt; johndoe@gmail.com
            [name] =&gt; Blake Robertson
            [title] =&gt; CTO
            [phone_work] =&gt; 8881112222
            [description] =&gt; Opinionated developer that hates SugarCRM's REST API with a passion!
        )

    [1] =&gt; Array
        (
            [id] =&gt; 4c8e3fcf-8e69-ed7d-e239-51a8efa4f530
            [email1] =&gt; csmith@mailinator.com
            [name] =&gt; Carolyn Smith
            [title] =&gt; Director of Something
            [phone_work] =&gt; 832-211-2222
            [description] =&gt; She's a smooth operator...
        )
)
</code>

For Reference Purposes

Here’s the “rest-data” (nicely formatted)

Used print_r of the php array

<code>Array
(
    [session] =&gt; 9j7fm4268l0aqm25kvf9v567t3
    [module_name] =&gt; Accounts
    [query] =&gt; accounts.id='e583715b-7168-5d61-5fb1-513510b39705'
    [order_by] =&gt;
    [offset] =&gt; 0
    [select_fields] =&gt; Array
        (
            [0] =&gt; id
            [1] =&gt; name
        )

    [link_name_to_fields_array] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [name] =&gt; contacts
                    [value] =&gt; Array
                        (
                            [0] =&gt; id
                            [1] =&gt; email1
                            [2] =&gt; name
                            [3] =&gt; title
                            [4] =&gt; phone_work
                            [5] =&gt; description
                        )

                )

        )

    [max_results] =&gt; 20
    [deleted] =&gt; FALSE
)
</code>

Post Body

method=getentrylist&inputtype=JSON&responsetype=JSON&restdata={“session”:”iov5a257lk5acsg9l3ll6kuej3”,”modulename”:”Accounts”,”query”:”accounts.id=’e583715b-7168-5d61-5fb1-513510b39705’”,”orderby”:null,”offset”:0,”selectfields”:[“id”,”name”],”linknametofieldsarray”:[{“name”:”contacts”,”value”:[“id”,”email1”,”name”,”title”,”phonework”,”description”]}],”maxresults”:20,”deleted”:”FALSE”}method=logout&inputtype=JSON&responsetype=JSON&rest_data={“session”:”iov5a257lk5acsg9l3ll6kuej3”}