Showing error messages and redirecting in SugarCRM

sugarcrmdevelopers —  February 14, 2013

I was working on applying some rules to SugarCRM this week. The logic hooks have allowed this to be build in before saving or deleting a file.

Thinking about the UI and the actions, I wanted to create a set of redirects which would show an error message if something could not take place. Normally, the logic hook runs through and quietly does it tasks before returning to the screen. Not very useful when you need to show a message.

Fortunately somebody was asking in the Sugar forums about redirecting users to other scripts in Sugar. John Mertic, Sugar’s community managers, wrote a short post about the SugarApplication redirect on the Sugar blog. Essentially it creates a 301 redirect (as a wrapper around the PHP header() function) or defaults to Javascript to do the redirect.

You can use a native header(); call if you really want to in the logic_hook. However this may cause issues if the base url of your Sugar instances changes. Using redirect() means that if you are internally redirecting, then the script will be aware of the Sugar root. You also do not have to be aware of moving between http:// and https:// which you would have to do with header(). Of course, it could be used to help the user to a different script, service or site if required.

The second issue was showing the error message for the user. Using the static appendErrorMessage() function in the SugarApplication API, you can stack up error messages until the View that reloads.

I’ve put together a quick gist to put it together and extend the SugarCRM example.