How to prevent logic hook firing from another module or on import

Hi all.

I've created a logic hook which basically checks if the record is updating and if so, check if there is a contact assigned and if not, do not allow save. This works fine on my test site but not on a customers site. They have two issues:

1) The first being when they save an account, it's also running this code, can I ONLY have this code run if the record is saved directly in opportunities?

2) Is there anyway to stop the code firing on import? So only on an actual edit it triggers?

Here is my code:

class oppcontactcheck
{
function validateDuplicateRecord( $bean, $event, $arguments )
{
if (!isset($arguments['isUpdate']) || $arguments['isUpdate'] == true)
{
//Opens Database variable
global $db;
$crm_id = $bean->id;
//Checks how many leads exist with the criteria contact name and mobile number
$oppContactCheck = "select count(*) as count from opportunities o join opportunities_contacts oc on o.id = oc.opportunity_id join contacts c on c.id = oc.contact_id"
. " where o.id = '".$crm_id."'";
$oppContactCheckResult = $db->query($oppContactCheck, true, 'Error Update');
$row = $db->fetchByAssoc($oppContactCheckResult);
//Assigns the count of the above to a variable
$count = $row['count'];
$id;
$conn = $GLOBALS['db']->getConnection();
$GLOBALS['log']->info('### Count: ' . $count);

//Change this to change the message that pops up
$duplicateoppContactMessage = "Please add a contact to the record";

//Checks if count is greater than 0
if ($count == 0)
{
throw new SugarApiExceptionInvalidParameter($duplicateoppContactMessage);
}
}
}
}

/resized-image/__size/320x240/__key/communityserver-discussions-components-files/54/Capture.PNG

I've also added a picture as my formatting is being wiped when copying.

Thanks! 

Parents Reply Children
  • Hi Daniel,

    Two reasons to consider SugarQuery:

    • Even if you are of SQL background (as many of us are), it does not mean we don't still make mistakes every now and then - eg forgetting an apostrophe in the wrong place. By wrapping an object layer around that, we reduce the opportunity for accidents ending up in production.
    • It makes your queries more portable between different database servers - may not be relevant for the implementation you're working on today - but I know a lot of people build libraries of code they reuse in different parts of an organisation, and you may one day reuse that code in a next client - or wish to share it publicly. SugarQuery makes that a lot easier.

    Equally, I acknowledge that it takes a bit more time to learn, and you may not feel its justified if you're just working on the one query that will never get used anywhere else. 

    Regards,

    Adam

  • ,

    Definitely i would use SugarQuery, also for security reasons (eg: what if a malicious user (or an integration coming from malicious places) pushes into your crm a record with id something like '"; truncate opportunities; --' ? The good old bobby tables!

    Even if it won't work due to further sanitisation, you get my point. Prepared statements would add an additional layer of protection thanks to SugarQuery.

    There is also an alternative ORM way of achieving what you attempted to do: "does this opportunity have any contacts related to it?" see here: https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_10.0/Data_Framework/Models/SugarBean/#Fetching_Related_Record_IDs

    Finally, that type of validation I would probably put it on the api itself. I would extend the PUT /Opportunities/<id> api with this additional check. That way it would not trigger on imports, but only when an edit fires from the api perspective (if that's your overall objective).

    Cheers

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States