Get all audit recordes for a module with Sugar Query?!

Is it possible to get all audit records for a specific module, Similar to a database query to load the table. Not only the ones linked to specific record.

What is the sugar query equivalent of this MySQL call;

SELECT *

FROM accounts_audit

Best regards

Gustav Lindstrom

  • 1) For example, you need get audit for module Accounts:

    $bean = new Account();

    $bean->retrieve($id); //audit log only for record

    $audit = new Audit();

    $audit_for_module = $audit->getAuditLog($bean);

    2) By custom query:

    $auditTable = 'Account';

    $query = "SELECT {$auditTable}.*

      FROM {$auditTable}

      ORDER BY {$auditTable}.date_created DESC";

    $db = DBManagerFactory::getInstance();

    $results = $db->query($query);


  • Not sure if I understand the answer, non of the alternatives is utilizing sugarquery. The second one seem to be loading the accounts table not the accounts_audit table?!

  • Gustav Lindström написал(а):

    ...The second one seem to be loading the accounts table not the accounts_audit table?!

    Yeah.. sorry.

    $auditTable = 'accounts_audit';

  • Yes that will get the right data but I would prefer if I could use sugar query. I creating an api end point and with sugar query I get format handeling

  • I've found this, it will load audit info for one record with sugar query.

            $this->requireArgs($args,array('module', 'record'));

            $focus = BeanFactory::getBean($args['module'], $args['record']);

            if (!$focus->ACLAccess('view')) {

                throw new SugarApiExceptionNotAuthorized('no access to the bean');

            }

            $auditBean = BeanFactory::newBean('Audit');

            return array(

                'next_offset' => -1,

                'records' => $auditBean->getAuditLog($focus),

            );

  • There is no sugar query funktion for retriving all audit row for example all accounts.

    You can look at the getAuditLog function in '\modules\Audit\audit.php' and rewrite the code too get all aduit row not only the ones linked to a record

    I ended up doing doing something like this (I'm gonna add date formating as well)

    $auditTable = 'accounts_audit';
    $query = "SELECT {$auditTable}.*
    FROM {$auditTable}
    ORDER BY {$auditTable}.date_created DESC";
    $db = DBManagerFactory::getInstance();
    $results = $db->query($query);
    $return = array();
    while ($row = $db->fetchByAssoc($results)) {
             $return[] = $row;

            }

            return $return;