PHP, SugarCRM, Javascript, Cool Web Apps Developer


view:  full / summary

Fetch Current User Id - Sugar 7

Posted by swapnil shinde on October 5, 2014 at 10:30 AM Comments comments (0)

From any of the javascript you have included in sugar crm's view hit following code response will give you the info you need.


    App.api.buildURL( 'me' ),


    success: _.bind( function( objCurrentUser ) {


    }, this )


Create Document in Sugar

Posted by swapnil shinde on December 11, 2012 at 12:10 AM Comments comments (0)

We can create or upload the document in sugarcrm using soap.

I was finiding a way to create a doc from backend. right now its from raw PHP code, I will post the code based on Sugarcrm's convention, I mean using objects soon.

Here is the code.

if(!defined('sugarEntry')) define('sugarEntry',true);


global $db;

$doc_guid=create_guid(); //////create sugar id for doc

$doc_revision_guid=create_guid(); //////////create sugar id for doc revision

$filename="upload/".$doc_revision_guid; //////create file in sugar's upload dir

$OpenDoc = fopen($filename,'w');

$writ_to_doc="Yo"; //////content to write to file

fwrite($OpenDoc,$writ_to_doc); //////write to file

///////////insert into documents module

$qry="INSERT INTO documents (id, date_entered, date_modified, modified_user_id, created_by, description, deleted, assigned_user_id, team_id, team_set_id, document_name, doc_id, doc_type, doc_url, active_date, exp_date, category_id, subcategory_id, status_id, document_revision_id, related_doc_id, related_doc_rev_id, is_template, template_type) VALUES ('$doc_guid', now(), now(), '1', '1', '', 0, '1', '1', '1', 'Neels.doc', '', 'Sugar', '', '2012-08-24', NULL, '', '', 'Active', '$doc_revision_guid', '', NULL, 0, '')";


///////////insert into documents revions module

$qry="INSERT INTO document_revisions (id, change_log, document_id, doc_id, doc_type, doc_url, date_entered, created_by, filename, file_ext, file_mime_type, revision, deleted, date_modified) VALUES ('$doc_revision_guid', 'Document Created', '$doc_guid', '', 'Sugar', '', now(), '1', 'Neels.doc', 'doc', 'application/msword', '1', 0, now())";


Add form Validation SugarCRM

Posted by swapnil shinde on July 19, 2012 at 1:40 AM Comments comments (3)


I have seen and encountered with issues like we need to do a validation that is out of box of SugarCRM.

For e.g. we have addToValidateDateBefore which works or invokes like

if you add it on date1 and to be validated with date 2, then it will work only if date1 is newer than date2, but we need to show error if date 1 is older than date2.

We can do our normal custom coding to verify these dates but question is how do we show errors in Sugar's fashion (a red colored message under field.)

so in your js code of edit view once you need to show error just put 

add_error_style('form name', 'field name', 'your message');


add_error_style('EditView', 'replan_start_date', 'Replan Date should not be older than Booking Date');

Create Object and Retrieve record by creating Object.

Posted by swapnil shinde on March 26, 2012 at 4:05 AM Comments comments (0)


So many times we need to create records into related modules so instead of inserting records into their tables and its custom table we can create the object of that module and call its function save().

So once u call that function it will also execute its logic hooks as well.

so we just need to include the file of that module.



$object= new module();


$object->description='Created for test';


so if we need to get the id of this currently created record, u can get it using


To retrive record using object.


global $sugar_config,$beanFiles,$beanList; in your file

$bean_name = $beanList['Module_Name'];


$module_obj= new $bean_name();

$module_obj->retrieve(id of record);

now suppose if u want to find name of record.

use $module_obj->name;

you can even get custom field in same way.

Find out Email Related to Account/Contact

Posted by swapnil shinde on February 15, 2012 at 7:55 AM Comments comments (0)

As Email for all the modules which are derived from Person Template/Type will be stored in Email Module. We can access this email by finding out relationship between module and email module.

Suppose you want to find Contact's email and you have its id(generated by sugar) then you can use following query to find out the email.

"select email_address

from email_addresses e, email_addr_bean_rel ec

where bean_id = '$contact_id'

and ec.email_address_id =

and e.opt_out =0

and e.deleted =0

and ec.deleted =0"

Suppose you want to find out Account's or User's email you can just replace the contact id by account or user's id and get it.

Customize Subpanel Buttons.

Posted by swapnil shinde on February 5, 2012 at 7:05 AM Comments comments (0)

To customize subpanel buttons.

Suppose we need to hide Create and Select buttons, or if we need to navigate quick create to full form. Then we need to follow following steps.

We need to first see the Subpanel's name.(which you can find out from studio/module/Relationships).

Navigate to custom/Extension/modules/Parent_Module/Ext/Layoutdefs

create one file like custombyswapnil.php.

and code like this.


$layout_defs["parent_module"]["subpanel_setup"]["subpanel_name"]['top_buttons']= array (

    0 =>

    array (

      'widget_class' => 'SubPanelTopCreateButton',



 // 1 =>

    // array (

      // 'widget_class' => 'SubPanelTopSelectButton',

      // 'mode' => 'MultiSelect',

    // ),




Above code is for hiding select button and making create button to navigate to full form directly.

Now, if you need to apply default sort order for the subpanel records according to any  field or to add title as you want, you can find file named custombyswapnil.php in "Downloads" section. if file doesn't open in firefox use chrome.

Customize the export functionality for a module

Posted by swapnil shinde on January 31, 2012 at 3:30 PM Comments comments (0)

The easiest way to do this is by providing your own query to use in the module’s bean method create_export_query(), but this isn’t upgrade-safe for any of the out of the box modules. However, there is an upgrade-safe way to do this, although it’s a bit more indepth, which our resident Sugar Hero andopes points out below ( from this post ):


You can create the script custom/modules/Leads/entry_point_registry.php containing something like that:


if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');


$entry_point_registry['export'] = array('file' => 'custom/modules/Leads/export.php', 'auth' => true);



The script custom/modules/Leads/export.php should be similar to default export.php. Inside this script you need to call a customized export (defined in include/export_utils.php). You can defined then which fields to export.

Credit :

Disable SugarCRM AjaxUI

Posted by swapnil shinde on January 31, 2012 at 3:10 PM Comments comments (2)

In version 6.3 of SugarCRM we are introducing a new method for loading pages that uses AJAX to load the html content rather than doing a full page refresh. This has some major impacts on performance.

You also have the option of totally disabling the AjaxUI with the following line. In config_override.php


just add line above in config_override.php

Using a relate field to populate a custom field

Posted by swapnil shinde on January 31, 2012 at 2:50 PM Comments comments (1)


In edit view of your module  once you select any record in relate field, if you want to populate any other field from same module then follow the following steps.

There are times a client requests, or common sense usability dictates the need to use a relate field to populate information related to a record. Often times, you can get away with an address or phone number; but there are times where you need to use a custom field.


The situation for our client required the use of an Account relate field and a field containing an account number that comes from another system our client uses. The requirement was that when the Account relate field is filled in that the account number field be populated with the number for that account. That leaves two situations we had to account for; the use of the Sugar Quick Search and the use of the Account popup.


The first example is how to complete this task using the Sugar Quick Search:


1. In your FTP client, navigate to your /custom/Extensions/modules/{MODULE NAME}/vardefs

2. From here you can either create a new file, or edit an existing file. Look for a file name sugarfield_{name_of_your_field}.php that’s the best place to add new code and maintain organization

3. Open that up and add the following lines:


$dictionary['{YOUR MODULE NAME}']['fields']['{YOUR FIELD NAME}']['populate_list'] = array('id','name','{NAME OF DB FIELD YOU WANT TO ADD}');


$dictionary['{YOUR MODULE NAME}']['fields']['{YOUR FIELD NAME}']['field_list'] = array('account_id_c','account_name','{THE ID OF YOUR FIELD ON THE EDITVIEW LAYOUT}');


4. You’ll want to replace anything contained in curly brackets with the specific information for your application. The populate_list is a list of the database field names that you are going to be pulling from the database. The field_list is the list of fields that you are going to be placing these values.

5. Once you have this in place, go to Admin/Repair/Quick Repair and this process will rebuild the vardefs file in /custom/modules/{MODULE NAME}/Ext/vardefs/vardefs.ext.php.


That’s all there really is to it from a technology standpoint, you can add fields to the arrays to populate more than 1field as well.


The Popup is where things are a little bit more tricky, at least until you know what you need to do. Setting it up in the code is easy enough.


1. Open /custom/modules/{YOUR MODULE}/metadata/editviewdefs.php

2. Field your field definition in the file, our full example is included below:


array (

'name' => 'accounts_hdsmi_nym_customer_service_name',

'displayParams' => array (

'field_to_name_array' => array(



'accountnum_c' => 'account_number_c',


'additionalFields' => array(

'accountnum_c' => 'account_number_c',






This is really all the code you need to get this set up. In the field_to_name_array and additionalFields array the key is the database field and the value is the Sugar field you are going to be populating. Pretty straight forward.


This next part is very important regardless of whether it’s working or not. Go into Studio and click on the module that corresponds to the relate field you are working with. (Account relate field requires you to open the Accounts Module). Go into your layouts and find the layout for your Popup List View. If it’s already working, then the custom field that’s being populated is already in that list view. If it’s not working, you’ll want to add that custom field to the list view. This is very important if it is working, because if that custom field is removed from the layout, it will stop working.



Repair Rebuild through Code

Posted by swapnil shinde on January 31, 2012 at 2:40 PM Comments comments (9)

PHP Code:



$rac = new RepairAndClear();

$rac->repairAndClearAll(array('clearAll'),array(translate('LBL_ALL_MODULES')), false,true);