How to reset field values on bean duplication?

SugarCRMWhat you need to do is inject some logic to clear selected field values before the view is displayed.

Like all other stuff made in SugarCRM, this process also depends on what type of module you’re hacking, in other words, the place where you’re going to write the logic depends if you’re hacking a custom module or a core one.

Before you perform view customizations on core modules you should check first if the view is already implemented, in this case, you should check if the following file exists:

modules/<ModuleName>/views/view.edit.php

If so, you should create a new file on:

custom/modules/<ModuleName>/views/view.edit.php

and add further customizations.

Beware that on latest SugarCRM versions, you’re able to create custom views without having to copy all the logic from the core ones, and by that I mean that now you’re able to “truly” extend the custom views from the latter.

In order to achieve this the view name should be prefixed with ‘Custom’:

// custom/modules/<ModuleName>/views/view.edit.php

require_once 'modules/<ModuleName>/views/view.edit.php

class Custom<ModuleName>ViewEdit extends <ModuleName>ViewEdit {
    /** 
     * ...
     */
}

If by instance you’re hacking a core module that doesn’t have the view defined, you should create it like the example above but with a few differences:

// custom/modules/<ModuleName>/views/view.edit.php

require_once 'include/MVC/View/views/view.edit.php';

class Custom<ModuleName>ViewEdit extends ViewEdit {
    /** 
     * ...
     */
}

One thing to keep in mind is that if you’re hacking a custom module, you are able to choose which directory suits you best. As opposite to the examples above, on the example below, the logic is written directly on the module main folder.

// modules/<ModuleName>/views/view.edit.php

require_once 'include/MVC/View/views/view.edit.php';

class <ModuleName>ViewEdit extends ViewEdit
{
    public function display()
    {
        $this->_processDuplicate();
        parent::display();
    }

    /**
     * How bean will act on edit view when it's being duplicated.
     */
    protected function _processDuplicate()
    {
        if ($this->ev->isDuplicate) {
            $this->bean-><FieldName> = '';
        }
    }
}

Thanks to Jeff Bickart of Nepo Systems and Matthew Poer of Profiling Solutions for pointing the “truly” Custom views feature out.

Facebook Twitter Linkedin Plusone
  • http://www.neposystems.com Jeff Bickart

    There a few corrections to be made to your post. When you create a custom view.edit.php in custom/modules//views/view.edit.php and your new class should be called CustomViewEdit not ViewEdit.

    Further you usually don’t need to copy the logic from the modules//view/view.edit.php you should be extending it.

    So a custom Contacts View edit would be

    require_once(‘modules/contacts/view/view.edit.php’);
    class CustomContactsViewEdit extends ContactsViewEdit

    Changing it in this manner allows for the core code to be changed from SugarCRM Version to SugarCRM version and not loose any functionality

  • http://www.profilingsolutions.com Matthew

    Very nice, Joao. The only thing to add is that not all beans have a specific view.edit.php file in their directory. Some do and some don’t… for those that do, the new class is usually CustomViewEdit and it would extend ViewEdit. For thost that don’t, they’ll extend the normal ViewEdit but they can (and probably should still be) named CustomViewEdit.

  • http://blog.joaomorais.com João Morais

    Hi @Matthew, @Jeff Bickart,

    This article was rescued from my blog backups, so it was probably obsolete, unfortunately I lost a few posts due to unpredicted hosting issues.

    Thanks for your feedback guys :)
    Already updated the post in order to reflect the changes you guys pointed out, and also gave you credits for that.

    Regarding the “truly” custom views, I think that this feature was implemented in the latest versions, from what I recall at least in SugarCRM 5.2.X it didn’t exist, and one was forced to copy all their logic to the ones on custom folder.

  • http://larryreese.yolasite.com/ Joe Hines

    The information in your post about SugarCRM Development is very interesting and very helpful for the Developers for the SugarCRM Development.