AnsweredAssumed Answered

Customize what data is displayed in ListView

Question asked by Francisco Casado Fernández on Oct 16, 2019
Latest reply on Oct 18, 2019 by Francisco Casado Fernández

Hi,

 

I'm working on linking multiple accounts to a single opportunity, so I have created a new relationship between Opportunities and Accounts so that there is a subpanel below each Opportunity with all the related accounts. Opportunities have a main account and then there may be multiple accounts linked to it, to sum it up.

 

In the listview and record view I display the main account name, along other things. The problem I'm facing is that whenever I add a new account to the "related accounts" section, the account displayed in the listview changes from the main account to the last added, but the record view still displays the original main account. I thought that maybe the list view displayed the last item added, so I manually changed the "date_modified" field in the database to trick Sugar into thinking that the main account was the latest one added, but it doesn't work. When I remove all the related accounts, the account displayed on the listview goes back to the original main account.

 

Im adding the relationship with this "Before Relationship Add" hook:

 

function before_add_method($bean, $event, $arguments)
{
    if($arguments['related_module'] == 'Accounts')
    {
        global $GLOBALS;
        $table = 'accounts_opportunities';
               
        try{
            $fieldDefs = $GLOBALS['dictionary'][$table]['fields'];
            $GLOBALS['db']->insertParams($table, $fieldDefs, array('id'=>create_guid(), 'opportunity_id'=>$arguments['id'], 'account_id'=>$arguments['related_id'], 'date_modified'=>date('Y-m-d H:i:s')));
        } catch (Exception $ex) {
            $GLOBALS['log']->fatal($ex->getMessage());
        }
    }
}

 

And removing said relationship with this "Before Relationship Delete" hook:

 

function before_remove_method($bean, $event, $arguments)
{
    global $GLOBALS;
          
    try{
        $query = 'UPDATE accounts_opportunities SET deleted = true WHERE opportunity_id = ? AND account_id = ?';
        $conn = $GLOBALS['db']->getConnection();
        $conn->executeQuery($query, array($arguments['id'], $arguments['related_id']));
    } catch (Exception $ex) {
        $GLOBALS['log']->fatal($ex->getMessage());
    }           
}

 

Is there something I can do to make the list view display the main account and not the last added? Currently using Sugar 9.1.0 + MySQL 5.7 + PHP 7.3.

 

Thanks in advance,

Regards.

Outcomes