SugarCRM Relationships

There are plenty of examples on-line of working with relationships between modules in Sugar using load_relationship, but one problem I’ve recently solved is, how do you know the name of the relationship you want to load. Contacts, Cases etc are easy but there are lots of others you may be interested in.

Well the answer is simple, so long as you can debug your code with something like X-debug. If you can’t you should be as it makes life for a developer very easy.

load_relationship is within the SugarBean class (data/SugarBean.php). Step into it and break just after this line.

$fieldDefs = $this->getFieldDefinitions();

Now look through all the $fieldDefs until you find the relationship you are interested in. I’m interested in membership, so this is the one I want. The {name} is what you use {relationship} is the name you will see when you look at relationships in Studio.

I hope this helps someone.

SugarCRM Inbound Emails

I’ve just recently been asked to add a logic hook to inbound emails, so that the emails could be used to be updated the associated contacts. I’m not going to provide the full details here but just a few hints.

Firstly when you set up Inbound Emails, you must make the mailbox a group mailbox, if you want to use logic hooks. Non group emails go to the table email_cache not emails.

Note also that it is the scheduled task that polls the email account for new emails, so you need to wait for it run, normally once a minute.

Also, inbound emails only get imported if they remain unread. Read emails are ignored. Once Sugar has imported them, they are set as read on the server.

To setup a logic hook, you need to do it on after_save on the Emails module.

Thats all, but I can provide source if you need it.




SugarCRM 7 Logic Hooks

I haven’t worked with SugarCRM for a couple of years so thought I would build a logic hook to refresh my memory. This one really should really be built into Sugar as an option. I think it would be the preferred behaviour for most people.

When you reassign an Account, this logic also reassigns all the Accounts contacts as well. Makes total sense that this is what you would normally do.

Anyway, these are the steps. Note the file names are mentioned in the source

1. Create a file like this

2. And a class like this

3. Check that all directories and files are readable by Sugar

4. Quick Repair

5. Give it a try

I hope that helps someone.


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


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)) {
if(time() >= $cutoff) {

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

SugarCRM 7 Record View

I’ve spent some time working on the newish Sugar 7, and don’t intend to review it any detail. There is one very small change that I don’t like, and that is to do with module views in Studio.

In earlier versions you could have a Detail View to display a record, and a separate Edit View to all creation and editing of records. These have been replaced by a single Record View.

This seems a backwards step. In many cases there are details that you want to show users, but not allow them to edit. An example might be sales figures that are updated by an integration process. Users should never be able to amend these, so in the past they would be just left off the Edit View. They would be included in the Detail View as they are potentially useful.

I can’t find anywhere a discuss on why this change was needed, and to me it seems a backwards step. I’m aware of the fact that people inherently don’t want change, so is this me just rebelling to something that has changed?

I would appreciate all any feedback.


Assigning Child Records In SugarCRM

If an account is assigned to another user, surely all the account’s contacts should also be reassigned. The same goes for other child records, such as documents, opportunities etc.

This requirement came up recently in the LinkedIn SugarCRM Network, so I thought I would put together a logic hook to see how it might be done.

The following logic hook was designed for Accounts, but could be applied to any module. Also I’ve specified in the code which child modules are to be updated. You may not need all related modules to be reassigned.

There are three issues that should be considered before implementing this type of logic, as follows.

(a) You will be saving related records, and if these have associated workflow or logic hooks, then these will get fired from this logic hook. You need to look carefully at each child module you use, just to be sure your are not going to initiate unwanted alters and actions.

(b) In some cases an account will have a huge number of records associated with it, and re-assigning them all via this logic hook could take some time. If this is the case it would be better to adopt a different solution (see below).

(c)  When a user creates a new contact, this logic hook will not ensure that the assigned user matches the account. This is because in this case the  accounts logic hook is never fired. To ensure all assignments are correct, you could also add logic hooks on each child module, or use a separate over-night process to ensure all records are assigned correctly (see below).

An alternative would be to run an overnight process to do the reassignments. This would involve similar logic, but run at a time of day that wouldn’t affect users. It would only look at recently updated records. This would handle (b) and (c) above, but not (a).

You will need to add the following to custom/modules/Accounts/logic_hooks.php

And add the following as a file custom/modules/Accounts/Reassign.php


I hope this is helpful. If you can suggest any improvements or have any comments, they would be very much appreciated.



Adding and Removing Teams Within Sugar

From time to time you will need to change the teams on a records, via code, probably from a logic hook. This sounds easy but I had quite a lot of difficulty with it. I’ll give you what I learned

Firstly, Sugar have documented here very well, but there are aspects to it that just don’t work. Note that the code snippets below all come from the Sugar site, with a few modifications.

From their examples I found the following.

1. It is imperative that the primary team (team_id) be one of the teams to be included. If you are removing the primary team from the list of teams, make sure you allocate a new primary_team, and save the record, before you start adding and removing teams.

The code below shows setting a new primary team, and saving the record.

2. Use replace(), not add() and remove() as suggested in the Sugar article.

I had all sorts of problems using add(0 and remove(). When I turned to replace(), everything worked fine. I’m using Professional 6.5.14, so it may have been corrected in later editions.

The following is all you need, just replacing the array with all the teams you want included.

With the above you don’t need to worry about TeamSets at all.

Let me know if you have any comments or suggestions.





Adding Javscript to SugarCRM UI

I had a case where the logic needed for a calculated field within Sugar was just too complicated for Formula Editor. Even if I had managed to get it working it would have been hard to modify in future.

So I started looking round for ways to use Javascript for this type of client validation, and I found that it was very easy to implement. Although my requirement was just to produce a calculated field, I started to think of all sorts of things you could do with Javascript, including  using Ajax to retrieve and display data, and to  do all sorts of validation, and other changes to the UI

For the present it is just the calculated field. The following a the few simple steps you need to follow. In this example I’ve assumed you are working with opportunities. The same logic would apply to any other module just as easily.

1. Add a reference to the javascript file you are going to add

Add the following at the end of custom/modules/Opportunities/metadata/editviewsdef.php

 2. Add the javascript file


What the above is doing is using JQuery to add a function that gets called whenever the ‘description’ file changes. When this happens, the field ‘next_step’ has its value changed, and an alert box is shown.

While this functionality if obviously of no value, you can do any logic and updating you like here. Also, we are using the JQuery function ‘change’ (which is just Javascript’s ‘onchange’ function), but of course you can use any of the JQuery trigger functions here.

3. Quick Repair, then browser refresh

This is needed to implement the new logic.

That is it really. One quirk I found was with multiple dependent drop-down lists where there were three levels. It seems that if more than one drop down list has it’s value change, only the first of them calls the Javascript on change. I need to investigate this further, but for now just be warned.

I would appreciate any comments of suggestions.


When Sugar User Interface Goes Wrong

When you work with Module Builder and Studio, usually things go well. The only thing to remember is that once you start using Studio to amend things, you can’t go back to Module Builder. If you do you loose the Studio amendments, and that is really how it should work.

My problem today came from exporting customization from a development instance of Sugar and loading it into the production instance. I’ve done this many times, as a means of migrating custom fields, layouts and code (logic hooks). This time it didn’t work, and I’m not sure why. Certainly the manifest file of the exported module looked a mess, so it it is not surprising that it caused all sort or problems.

I’m not going to bore you with going through all the steps I had to take, but I would like to document the few general rules that I found applied. These need more testing out, so don’t depend on them 100%. If I have any wrong, or missed anything, please let me know.

1.    If custom fields don’t appear in the fields_meta_data table in the database, nothing else will solve your problems.

2. When you edit any vardef type fields, always do a Quick Repair afterwards. There is no need to clear cache or log off or anything else.

3. Sometimes you need to go into Studio, and save an deploy each of DetailView, EditView etc, to correct some type of problems.

I’ll add some more notes here from time to time, but I would be interested if you have any further suggestions or ideas.


Adding Fields To An Existing Module Without Using Module Builder

I had the need to take an existing module that had been built earlier with Module Builder, but needed some fields added. In fact there were a large number to add, so much in fact that I didn’t fancy entering them one by one in Module Builder.

As all were very similar fields I thought I should be able to edit files within the module directly. As it turned out this was quite possible.

The steps I went through were as follows.

1. Obtain the existing module. This is a zip file you can locate in the upload/modules directory

2. Unzip this file

3. Edit the vardefs file for the module you want to add fields to. If the module was Test, the file would be SugarModules/modules/Test/vardefs.php

When you look at this file you will see the format of the various fields. Just add and edit as needed.

Note you may wish to edit the language files as well, though I didn’t.

4. Zip up the folder again. Something like the following

zip -r ../ *

Will give you a file that you can upload.

If the module exists, you will need to uninstall the previous version first. You can conserve the data.

5. Use Module Loader to install the modules.

That’s it. Most times you won’t want to do it this way, but in my case I needed to duplicate 5 fields 15 times. An horrible job in Module Builder.