HOWTO: Adding your own logic with Custom Beans in Module Builder

sugarcrmdevelopers —  October 11, 2011 — 6 Comments

One of the advantages of building with Module Builder is that you get complete control of your module including the ability to add code directly to your custom beans. This means that you don’t need to feel limited by where you can interject changes in thru logic hooks when building a custom module, giving you full control over what you can do.

In order to add code to a custom bean, you first need to build your module. This can be done by hitting the publish button, but you can ignore the zip it produces. This will create two files of interest.

custom/modulebuilder/builds/{package}/SugarModules/modules/{module_name}/{module_name}.php and custom/modulebuilder/builds/{package}/SugarModules/modules/{module_name}/{module_name}_sugar.php

The “{module_name}_sugar.php” file contains the code generated by ModuleBuilder and shouldn’t be customized. “{module_name}.php” however is fair game for all your custom code needs. For instance, instead of creating an “after retrieve” logic hook, you can add the following code to your custom bean.

function retrieve($id) {
    // Custom logic here

or a “before save”

function save($check_notify = FALSE) {
    // Custom logic here

This results in code that is much easier to understand while still remaining as upgrade safe as logic hooks.

6 responses to HOWTO: Adding your own logic with Custom Beans in Module Builder


    You should deploy the package after changes – it is move your changes into modules/{module}/{module}.php


    after add my custom methods, sugarcrm dont see my changes. what can i do?
    when i use my method throw this error “PHP Fatal error: Call to undefined method”


      From that error, I would say that you have not defined your method correctly. Please check that your code is correct.


        If I write the method in custom/modulebuilder/builds/{package}/SugarModules/modules/{module_name}/{module_name}.php its doesnt work.
        But if I write the method in /sugarcrm/modules/{module_name}/{module_name}.php all works fine but its not the right way.

        The problem is that Deploy the package will lost my changes.



    I am trying to use the mark_relationships_delete as an override in a custom module, but it doesnt seem to get called when removing a relationship, it does however get called when I delete a record (as I would expect), so if I override mark_deleted then this then calls after my mark_relationships_delete but not when a plain “remove relationship” is fire. Any thoughts please?


    Has anyone managed to get a logic hook to persist after custom module redeployment?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s