Errors in Logic Hooks

Hi,

I am using a before save logic hook on a custom module. This hook makes API calls to do some work in other services related to the entry being created or updated. If these calls fail I would like to send and display a specific error message to the client. I have not been able to do this. Especially when editing a record. On create we use appendErrorMessage and a redirect but this does not work on updates. I also tried creating an “Alert” bean and that bean is persisted in the database but is never displayed to the user.

What seems to happen is that the client detects that there is an error and displays the message with the tag LBL_ERROR_SAVING.

As far as I can tell this is happening in angular in the file core/app/core/src/lib/views/record/store/record-view/record-view.store.ts.

The catchError function is triggered and the hard-coded message is displayed. I have looked at the objects available at that point and have not been able to get the error message down to those objects.

So is there a way to return error messages from a logic hook that can be displayed to the client? Just to be clear in this error case the changes should not be persisted.

I am using suiteCRM 8.2.0.

I have looked at a few other forum posts on this issue and so far they have not been helpful.

Any help would be great.

Thanks,

Bram

Hi @bcymet,

You can use the SugarApplication::appendErrorMessage() method to add an error message to be displayed to the user. You can call this method in your before save logic hook when you encounter an error.

Here’s an example:

if ($api_call_failed) {
    $app = \Sugarcrm\Sugarcrm::getApplication();
    $app->appendErrorMessage('API call failed.');
    return false;
}

This will add an error message to the session that will be displayed to the user on the next request.

Regarding updates, you can use the SugarBean::update_existing() method to update an existing record. If the update fails, you can call appendErrorMessage() as above to display an error message to the user.

I hope this helps! Let me know if you have any further questions.