Add a Logic Hook to Case Generation - SugarCRM Community Edition
In a previous article I covered how to interact with the SugarCRM Community Edition REST API to add a case note. In this article I'll explain how you can execute custom-logic whenever a Case is created. The use case here is that we need to add a URL to the Case Description that the case handler needs to be able to service the customer.
In order to perform custom logic each time a case is created we have to use a SugarCRM Logic Hook.
===
In a previous article I covered how to interact with the SugarCRM Community Edition REST API to add a case note. In this article I'll explain how you can execute custom-logic whenever a Case is created. The use case here is that we need to add a URL to the Case Description that the case handler needs to be able to service the customer.
In order to perform custom logic each time a case is created we have to use a SugarCRM Logic Hook.
References
- Simple SugarCRM Logic Hook Example [http://cheleguanaco.blogspot.de]
- Newbee to logic hooks - tutorials? [forums.sugarcrm.com]
- Business Logic Hooks [support.sugarcrm.com]
What is a Logic Hook?
A Logic Hook allows you to extend CRM application logic in an 'upgrade safe' way. You can do things like updating a record value before it is saved to the database, performing API calls to other systems in your environment, sending email messages and other things like that.
What are you trying to accomplish?
We are going to use a Logic Hook to add a custom-built URL to each Case that gets created in SugarCRM. This combined with what I wrote about last time allows us to add case notes outside of SugarCRM, which was a requirement for the job I was doing (they have other systems which need to add notes at certain stages based on the actions taken during the case).
Create a Logic Hook that adds a URL before Cases are saved
In order to create the logic hook we need to create or alter 2 files:
- The <sugarcrmRoot>/custom/modules/Cases/logic_hooks.php file
- And <sugarcrmRoot>/custom/modules/Cases/logic_hooks/addUrl.php file
How to use the logic_hooks.php file
The logic_hooks.php file was laid down when I performed my install of SugarCRM Community Edition version 6.5.18. It is used to map custom application logic to the appropriate event. In my case I want to tie into the before_save logic hook so the changes I make will be reflected in the DB.
To use this file, look for or create an array named for the logic hook or event you want to tie into. Then, attach your hook to the array being sure to include the order in which your custom logic should be fired.
Here's what my logic_hooks.php file looks like:
?php // Do not store anything in this file that is not part of the array or the hook version. This file will // be automatically rebuilt in the future. $hook_version = 1; // // This is where all the logic hooks tie into $hook_array = Array(); // // This is the 'before_save' event that we want to tie into $hook_array['before_save'] = Array(); // // This logic hook comes 'out of the box' and serves as a great reference for how to make this work // When you create an array, the parameters should be like this: // position, description, file, className, functionName $hook_array['before_save'][] = Array(1, 'Cases push feed', 'modules/Cases/SugarFeeds/CaseFeed.php','CaseFeed', 'pushFeed'); // // This is my custom logic hook to add a URL to the Case Description // It is set to fire in the second position, after the built-in activity happens $hook_array['before_save'][] = Array(2, 'Add URL to Case Description', 'custom/modules/Cases/addUrl.php','UrlGenerataor', 'generateUrl'); ?>
Purpose and Function off the addUrl.php File
The logic_hooks.php file expects a hook to be defined in its own PHP class file. The addUrl.php file contains the class and method that the CRM logic hook parser will use to perform my specified set of actions.
This is the meat of this solution as it:
- Obtains the current description text set on the case
- Obtains the case ID
- Generates a URL that contains the case ID
- Appends the generated URL to the case description
Here's what's in that file:
?php if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); /* * Copyright 2014 BoredWookie * * Special thanks to this guy: http://cheleguanaco.blogspot.de/2009/06/simple-sugarcrm-logic-hook-example.html * * License: MIT * Disclaimer: For educational purposes, be sure you know what you are doing! */ // This class deals with attaching a URL to the case so the agent can generate a work agreement class UrlGenerataor { function generateUrl($bean, $event, $arguments){ // get the description text $description = $bean->description; // get the case GUID $caseguid = $bean->id; // Generate the URL to the agreement generator $agreementGeneratorUrl = "https://domain.tld/specific/url/tree/action3.html?caseguid=".$caseguid; // Append the URL to the description text (Ensures we don't overwrite something important) $description = $description."\r\n\r\n".$agreementGeneratorUrl."\r\n\r\n"; $bean->description = $description; } }
That's great, but where do these files go?
The files we've talked about here should go in the /custom/modules/Cases/ directory. Here's a screenshot from a test server I used to stage some Sugar CRM Stuff which I hope can clarify: