Nilesh Gajare
Nilesh Gajare

Reputation: 6398

get Contacts based on Account in SugarCRM REST API in C# .Net

i want to retrieve all contacts based on account in sugarcrm rest api using C# .net

i have tried

json = serializer.Serialize(new
            {
                session = sessionId,
                module_name = "accounts",
                query = "",
                order_by = "",
                offset = "0",
                select_fields = "",
                link_name_to_fields_array = "",
                max_results = "2000",
                deleted = 0
            });
            values = new NameValueCollection();
            values["method"] = "get_entry_list";
            values["input_type"] = "json";
            values["response_type"] = "json";
            values["rest_data"] = json;

            response = client.UploadValues(sugarUrl, values);
            responseString = Encoding.Default.GetString(response);

            var accountsList = serializer.Deserialize<Dictionary<string, dynamic>>(responseString);

i am able to get all accounts and contacts but i am not getting relations between them i.e which contact belong to which account

Thanks for help in advance

UPDATE :

object[] linkNameToFieldsArray = new object[1] 
{ 
    new object[2, 2] 
    { 
        { "name", "contacts" }, 
        { "value", new string[2] 
            { "id", "last_name" 
        } 
    } 
};
json = serializer.Serialize(new
            {
                session = sessionId,
                module_name = "accounts",
                query = "",
                order_by = "",
                offset = "0",
                select_fields = "",
                link_name_to_fields_array = linkNameToFieldsArray , ***//just added this to get related records***
                max_results = "2000",
                deleted = 0
            });
            values = new NameValueCollection();
            values["method"] = "get_entry_list";
            values["input_type"] = "json";
            values["response_type"] = "json";
            values["rest_data"] = json;

            response = client.UploadValues(sugarUrl, values);
            responseString = Encoding.Default.GetString(response);

            var accountsList = serializer.Deserialize<Dictionary<string, dynamic>>(responseString);

Upvotes: 1

Views: 1308

Answers (1)

Matthew Poer
Matthew Poer

Reputation: 1682

Assuming a SugarCRM 6.4 or 6.5 system and API version REST v4_1...

I don't know the C#/.NET syntax/lingo, but 'link_name_to_fields_array' needs to be an array with keys of module names (e.g. "Contacts") and values that are arrays of the fields you want. The JSON would look like this:

{
    "session":"asdfasdfsrf9ebp7jrr71nrth5",
    "module_name":"Accounts",
    "query":"",
    "order_by":null,
    "offset":0,
    "select_fields":[
        "id",
        "name"
    ],
    "link_name_to_fields_array":[
        {
        "name":"contacts",
            "value":[
                "id",
                "last_name"
            ]
        }
    ],
    "max_results":"2",
    "deleted":false
}

Also - I wrote this to help non PHP-devs interact with this version of the API, since documention is largely PHP based. You may find it useful: https://gist.github.com/matthewpoer/b9366ca4197a521a600f

Upvotes: 1

Related Questions