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.

 

One thought on “Adding Javscript to SugarCRM UI

  1. Hey there,

    I wanted your help on javascript for view.detail.php
    I need to hide couple of shortcut buttons in detail view via java
    I have this
    ========================================================
    require_once(‘include/MVC/View/views/view.detail.php’);

    class LeadsViewDetail extends ViewDetail
    {
    function display()
    {
    parent::display();

    require_once(“modules/ACLRoles/ACLRole.php”);

    global $current_user;

    $role_to_check = “ADMIN”;
    $acl_role_obj = new ACLRole();
    $user_roles = $acl_role_obj->getUserRoles($current_user->id);

    if ( in_array($role_to_check,$user_roles) )

    echo <<<EOHTML

    document.getElementById(‘form’).convert.style.display=’none’;

    EOHTML;
    }
    }
    }
    =========================================================

    I got the ACL to work, but the JAVA don’t.
    I’m using CE 6.5.16
    I need to hide Convert Lead, Delete, Duplicate, Edit via java.

    Thanx in advance.

Comments are closed.