phprestsugarcrmsugarbean

Joining two SugarCRM modules on linked field using SugarQuery()


Background and Context

I am attempting to create a custom REST module in SugarCRM 8.0 that joins two modules on a linked field. The two modules are;

These two modules are a linked field named w002_consumerleads_accounts_1 such that when I invoke the following endpoint I get a JSON response with all the ConsumerLead data associated with that specific account uuid.

/rest/v11_4/Accounts/{ account_uuid }/link/w002_consumerleads_accounts_1

However, instead of returning the joined results based on the account uuid, my custom module returns the the joined results based on a different account attribute that uniquely identifies an account.

/rest/v11_4/customer/{ customer_id }/leads

The reason I am attempting to build a custom module for this ( instead of using the predefined rest api listed above ) is due to the fact that the system invoking this call does not know the sugar account uuid, and instead knows a business key that uniquely identifies the account ( and is an attribute on the Accounts module ).

The Issue

When I use SugarQuery in my custom rest module to join Accounts and ConsumerLeads my results do not match the results specified in the first rest api ( the predefined rest api ). The result only returns the Accounts module, and does not join the ConsumerLeads module.

The issue is with the joining of the two modules on the linked field; the issue is not related to using the customer uuid v. the account uuid.

Reference Implementations

Based on the SugarCRM Developers Guide I written the following piece of code.

    public function GetLinkLeads($api, $args) {

        try {

            $query = new SugarQuery();
            $query->from(BeanFactory::getBean('Accounts'));
            $query->join('w002_consumerleads_accounts_1');
            $query->limit(10);
            $results = $query->execute();

            return $results;

        }  catch ( Exception $e ) {
            return $e->getCode();
        }

        return 1;
    }

Based on all the information I can gather, this function should return the first 10 account records joined with their ConsumerLeads. However, the response only contains the Accounts module data, it does not join the ConsumerLeads data.

Additionally, I have also tried the following way.

public function GetLinkLeads($api, $args) {

    try {

        $account = BeanFactory::getBean('Accounts', '4606e963-9213-7764-d83f-4cc050c8473d');

        if ( $account->load_relationship('w002_consumerleads_accounts_1') ) {

            $lead = $account->w002_consumerleads_accounts_1->getBeans();

            return $lead;
        }

    }  catch ( Exception $e ) {
        return $e->getCode();
    }

    return 1;
}

Where 4606e963-9213-7764-d83f-4cc050c8473d is an account uuid that has associated ConsumerLeads, but I still cannot get ConsumerLeads data to return.

Question

I would like to either:

I have found this exact question on Stack Overflow and the implementation above is based off the recommendation from that post. However, it still is not joining the records.


Solution

  • What you want to achieve should work OOTB using the filter API and specifying the required field's contents in your requests filter definition. Then you should receive the linked record you wanted.

    It should work similar to https://stackoverflow.com/a/50608304/4068476

    As quick test you could try calling this in your browser or program (here: GET, that's why I pass filter in query string)

    /rest/v11_4/w002_ConsumerLeads?filter[0][w002_consumerleads_accounts_1.consumer_id]={ customer_id }
    

    Notes: