Can a 'before' logic hook stop processing a bean

Hi,

I'm writing a logic hook to pre-process emails being imported via the SNIP email archiving feature.

I'm using the before_email_import logic hook but I've also tested it for the more generic before_save logic hook in the emails module.

Basically, I have a condition in the hook to check emails before they are imported and if certain conditions are met, to not import the email. The thing is, I don't want to return an error - I just want it to accept the email and not perform the import into the database.

If I throw an API exception or even just use die(), then sugar's SNIP server receives a non-successful message (I assume from it's behaviour) and retries a few times before giving up. 

Due to the amount of emails being processed, I want to avoid importing the emails and having to clean them up afterwards using something like a scheduler. Also, I can't alter what is being sent into the SNIP server and so filter beforehand.

Any suggestions welcome.

Steve

  • Hey,

    What I understood you are looking for is to skip importing of few emails based upon few conditions.

    eventually, before_save can't stop saving anyway, until you make a die and your die will throw an error.

    The only way, i can see put your code in this way.

    1) Create an after save hook rather than before save.

    2) On basis of all of your conditions, you can delete this recently import email via mark_deleted function, so ideally it is going to save your time to delete by yourself, this process will be internally managed by Sugar itself this way.

    Hope that helps.

  • I found the solution - it was to use the before_email_import logic hook and set $bean->id to NULL. There is an undocumented condition in the SNIP codebase to abandon an import in this case.

  • Hello Ashish Dwivedi,

    You can also throw an error message on the top of the screen by setting the following:

    $string_error = "Please update the following fields.... "

    ...

    if($flagError == true){
       throw new SugarApiExceptionMissingParameter($string_error);
    }

    Regards, 

    Angel M.

  • Hi  ,

    since the 13.0.3 patch, this way (throw an exception) is not working anymore.

    Do you find a way to replace this ?

    Fred

  • Hello Frédéric, 

    The exception should halt the record save.
    If this is not happening in 13.0.3 I believe you might be hitting the issue 

    #93899 (portal.sugarondemand.com/.../93899)

    The issue was already addressed in version 14, and I can see that our developers have been providing hotfixes for versions 13.x
    Would you mind creating a case to double check if that's the scenario that you are facing? 

    I hope this helps. 


    Cheers, 

    André