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.


Moving to Amazon AWS EC2 and RDS, and PHP 7

Moving a large web application from a dedicated host running PHP 5.5 to Amazon AWS EC2 with RHEL and PHP 7.1.2 presented much less in the way of challenges than I had expected. Maybe I’m a pessimist but I expected it to be a long drawn out affair.

As with any software change, we created a branch from our master source in Git, and worked on this. We would merge that back into master once the AWS EC2 instance was finally in production.

PHP had to be configured, which is expected as we had installed  PHP 7.1.2 and not yet touched its configuration. Having the old server as an example of what we needed made it very easy.

The application itself needed reconfiguration, as is normal when you move to new host. The database credentials needed to point to our database instance at AWS RDS, and other server specific changes like file locations needed to be made.

One issue I had was testing the application. We had to leave the current domain pointing to the old hosting as it was still in active use. How then to test the new server, while making sure that requests from it went back to the old server, without being noticed. If this happened, it wouldn’t be a complete test.

I  added an entry to the hosts file (normally /etc/hosts) on both the EC2 hosting and my test client (my MAC). If ever there was a stray link to the old domain, it would always be directed to the the EC2 host.

Obviously these host file changes were removed when the new host was in production.

There were a number of coding issued to be resolved, mostly to do with functions that had long been deprecated, but now finally removed (like split()). Beyond that a few file permission issues, but they are normal.

Before going live I had to transfer newly created files from the old web server. I used rsync as this is the most reliable way to ensure all new and amended files have been transferred. And it is very quick and easy to use.

Form a user’s point of view the new configuration seems about 20% faster, but we feel comfortable that we can increase the EC2 instance to a more powerful option should this ever be needed.


Setting Up An Amazon Web Service

Most of the software I write at work can’t be included in my blogs for obvious reasons, but when it comes to research over the holidays, that is a different matter. In this case I’m planning to move our applications across to AWS so I thought I would play round with it to see what was involved. I sure there are opportunities and pitfalls so the best way to find these is to have a go.

What I’m aiming at is cloud hosting for PHP applications, their databases and lots of file storage (photos and video). We will start with PHP 7 and resolve any compatibility problems as we migrate.

1.  Set up a free AWS account

Although its a free account, you have to enter credit card details. I’m a bit worried that I might start getting changed for things when not expecting it. I must keep track of options I select, to make sure they are free (for now anyway).

2. Build Your Virtual Machine

As a part of signing up you get the following options. I chose Launch a virtual machine. You also need to give it a name (GregsTest).

Then choose an operating system. I started with Amazon Linux, but got stuck trying to get PHP7 working. I changes to Red Hat Linux.

You then need to choose an instance type. Might as well go for something small and free for now.

Finally we need to set up private key. You keep this on your machine, and it gets used whenever you connect into your instance. AWS keep your public key.

Be sure to keep a safe copy of this private key as without it you are lost. AWS don’t keep it so you are on your own.

In this case the download will be a file GregsTest.pem

I know we have chosen everthing, but somehow we need to go through it again. We are asked to choose a machine instance as below. As mentioned, I went for Red Hat

Then an instance type

It will build now and then show you a list of your instances as follows.

You can connect to your instance with ssh as follows.

Not this refers to your private key. In this example the pem file is in the current directory but this may not always be the case. You will go straight into the shell on the new instance. There is no user name and password as the public/private keys have been used for authentication.

3.  Install Apache and PHP 7

All you need is this

And this

Well that’s it. You should be able to open that in a browser,using the AWS URL

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.