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

custom/modules/Opportunities/recalculateAmount.js

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.