SugarCRM REST API, how to get Contacts related to an Account

scootstah picture scootstah · Oct 25, 2012 · Viewed 9.2k times · Source

I am using SugarCRM Pro 6.5.5. I am trying to do some integration into another application, so I need to do some API calls from that other application. I am using the REST API v2. At this point, I need to get a Contact that is related to an Account via the account ID. I have tried both get_relationships() and get_entry_list(), but I can't get either of them to work.

Here's my input for get_relationships():

{"session":"eujfbfsfjgni98m0mivl6jm6r2","module_name":"Accounts","module_id":"c03d0649-0525-2f90-1206-50881e87d7dd","link_field_name":"contacts"}

I have tried many variations of this, and several other options, but nothing did what I wanted. This is the output from above:

stdClass Object
(
    [entry_list] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 
                    [module_name] => Contacts
                    [name_value_list] => stdClass Object
                        (
                            [deleted] => stdClass Object
                                (
                                    [name] => deleted
                                    [value] => 0
                                )

                            [do_not_call] => stdClass Object
                                (
                                    [name] => do_not_call
                                    [value] => 0
                                )

                        )

                )

        )

    [relationship_list] => Array
        (
        )

)

I don't see how this information could ever be useful.

Next up, I tried get_entry_list() with a query to select only the Contacts that had the given account ID. Here's my input for that:

{"session":"8mol33e7kdq6girugu30dnt074","module_name":"Contacts","query":"accounts.id = 'c03d0649-0525-2f90-1206-50881e87d7dd'"}

I am using accounts.id because I have read in similar questions, that is correct. I've tried many other things, like: contacts.account_id, account_id, etc. In every case, I get a MySQL error in the log stating the column is invalid.

SugarCRM: how to get all contacts for an account via REST API

This question is relevant, and seems to be the exact same problem. I tried adding a link_name_to_fields_array option to my input, but no matter what value I provide, it results in the following MySQL error in the log:

AND jt11.deleted=0 where (Array) AND contacts.deleted=0: MySQL error 1054: Unknown column 'Array' in 'where clause'

Dafuq?

Thanks.

Answer

scootstah picture scootstah · Oct 25, 2012

Thanks to ychaouche, I figured it out.

I was able to use the get_relationships() method after all. I had tried before adding the related_fields to my input, but all that did was result in an empty result set. The answer? Set related_module_query to an empty string... and now the result set is not empty. Yep, my 3+ hours of furious Googleing last night is because of that.

So, here is some working code in case anyone else has this problem:

{
    "session": "iov9pg9kvvl60librung1h5fh6",
    "module_name": "Accounts",
    "module_id": "c03d0649-0525-2f90-1206-50881e87d7dd",
    "link_field_name": "contacts",
    "related_module_query": "",
    "related_fields": [
        "first_name",
        "last_name"
    ],
    "deleted": false
}

This got me the following result set:

stdClass Object
(
    [entry_list] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 
                    [module_name] => Contacts
                    [name_value_list] => stdClass Object
                        (
                            [first_name] => stdClass Object
                                (
                                    [name] => first_name
                                    [value] => John
                                )

                            [last_name] => stdClass Object
                                (
                                    [name] => last_name
                                    [value] => Smith
                                )

                        )

                )

        )

    [relationship_list] => Array
        (
        )

)

You can of course specify different fields in related_fields and get different pieces of data.