Working With SugarCRM Logic Hooks

There are two ways of installing logic hooks: one works fine, the other seems to cause problems.

The way not to use is a log_hooks array within the manifest file as follows.

'logic_hooks' => array(
array(
'module' => 'Emails',
'hook' => 'after_save',
'order' => 200,
'description' => 'This logic hook looks at a saved email and adds a       history record.',
'file' => 'custom/include/classes/History.class.php',
'class' => 'History',
'function' => 'addHistory',
),

You are much better to user the copy process as follows. Follow exactly the directory names.
Also, make the actual file name something unique, so that it doesn’t match an existing file for other logic hooks.

'copy' => array (
0 => array (
'from' => '/Files/custom/Extension/modules/Emails/Ext/LogicHooks/history_logic_hooks.php',
'to' => 'custom/Extension/modules/Emails/Ext/LogicHooks/history_logic_hooks.php',
),

Where the file referred to (eg.history_logic_hooks.php) holds the definition like this.

// logic hook to handle history // 3-oct-2017
$hook_array['after_save'][]
= Array( 200, 'Save History', 'custom/include/classes/history.class.php', 'History', 'addHistory' );

Of course you also have to copy the class file you refer to.

SugarCRM Module Loader Issue

When trying to unload a new module I kept getting a bank screen, and errors in sugarcrm.log. The upload uses sys_get_temp_dir() to get the temp directory and this was set outside the document root, hence the error.

But how do yo set the system temp directly? Finally I tracked it down. You need to add the following to the php.ini file. You think it would be there, commented out, like lots of other parameters.

sys_temp_dir = "/Users/gregambrose/tmp"

Obviously, restart apache.

I hope that helps somebody

Greg

Debugging SugarCRM Scheduled Tasks

Debugging PHP is best done using an IDE (in my case Eclipse) where you can set break points and step through code and look at variables. Normal web applications are straight forward normally but scheduled task debugging is a bit of a challenge. Below are my suggestions as to how best this can be done.

Before starting I must say, don’t try this on a production server. You will be affecting the running of normal scheduled processes if you do!

You will need to make sure that the scheduled task you are debugging is the only task that will run. To achieve this, set all other job as deleted as follows (keep a note of what is active and what has previously been deleted before you start).

UPDATE job_queue SET deleted = 1
UPDATE job_queue SET deleted = 0, execute_time = '2017-01-01 00:00:00', status = 'queued' WHERE id = ''

As the execution time set is earlier than now, the job will be run.

Now to start debugging. You need to debug cron.php in the root directory, as a command line process. If you try defining it as a web application in your IDE (ie. in a browser) it will die before doing anything.

Break at this line in cron.php

$jobq->runCycle();

Then step in

In runCycle() you will see a loop through the scheduled tasks, like this.


for($count=0;$count<$this->max_jobs;$count++) {
$this->job = $this->queue->nextJob($myid);
if(empty($this->job)) {
return;
}
$this->executeJob($this->job);
if(time() >= $cutoff) {
break;
}
}

All you need do is break on executeJob() and have a look at $this->job to see if it is the one you are interested in. It should be as you have stopped all others.

Step into it and see what its doing.

Within executeJob() you find the following

$res = $this->job->runJob();

You should step into this.

The line that does all the work is this line.

$res = call_user_func_array($func, $data);

Step into that and you are in the code you want to debug!

When you have finished debugging, remember to reinstate the deleted status of all the other jobs.

If you think this can be improved, please let me know

Quality of Coding

Sometimes I think I’m on my own in believing that code should be tidy and consistent, and generally easy for other programmers to pick up and work with. I come across so much code that does the job it was intended to do but is very untidy and has only the barest of documentation.

There are lots of practical reasons for writing quality code but I would like to leave those for now and mention something more personal.

In Zend and The Art of Motorcycle Maintenance by Robert Persig, the concept of quality for its own sake is introduced. Early in the book he mentions one example from his own experience. It has always always stuck in my mind for some reason, and have nothing to do with programming.

Robert had a workshop with a big draw full of heavy tools. Every time he had to get a tool he had the dragging the draw out, which always involved lots of physical effort. One day it got him down and he decided to do something about it.

He went off and bought roller bearings and fitted them to the draw. From that time on he could open and close the draw with a single figure. It gave him great joy every time he used it.

Now the joy didn’t come from the saving of all that physical effort; it came from the pure quality he had built in. Every time he used the draw he felt good about it and himself.

He goes on to talk about how we can all build quality into everything we do. He mentioned how even very mundane jobs can be turned into something really fulfilling by concentrating on doing the job as well as it can possibly be done. In other words building in quality and getting a good feeling from it.

The best way to build in quality is to write it that way from the start. Make sure every line of code is something you are proud of. If you think you will come back later and tidy it up, if you are like me  it probably won’t happen.

 

Posted in PHP

Developing with SugarCRM

I’ve been working with SugarCRM for 18 months, and found that there isn’t a lot of online material to help you get started. There are plenty of code snippets but not many full examples you can get working quickly, and that’s what I’m always looking for.

So what I will be doing is adding examples, with enough code and explanation to get something working quickly.

I use SugarCRM Professional, and this changes from time to time. With each example I’ll indicate which version the examples relate to.

Development for Sugar falls into a number of categories as follows. I will be ignoring everything related to using Studio and Module Builder within Sugar as these don’t involve any coding and in any case are well covered elsewhere.

  • Logic Hooks
  • Custom code
  • Custom modules
  • Integration