SugarCRM SupportProduct GuidesSugar DeveloperSugar Developer Guide 11.0ArchitectureSugar LogicUsing Sugar Logic DirectlyCreating a Custom Dependency for a View

Creating a Custom Dependency for a View

Dependencies can also be created and executed outside of the built in features. For example, if you wanted to have the description field of the Calls module become required when the subject contains a specific value, you could extend the calls edit view to include that dependency.

./custom/modules/Calls/views/view.edit.php

<?php

require_once "include/MVC/View/views/view.edit.php";
require_once "include/Expressions/Dependency.php";
require_once "include/Expressions/Trigger.php";
require_once "include/Expressions/Expression/Parser/Parser.php";
require_once "include/Expressions/Actions/ActionFactory.php";

class CallsViewEdit extends ViewEdit
{

    function CallsViewEdit()
    {
        parent::ViewEdit();
    }

    function display()
    {
        parent::display();
        $dep = new Dependency("description_required_dep");
        $triggerExp = 'contains($name, "important")';
        //will be array('name')

        $triggerFields = Parser::getFieldsFromExpression($triggerExp);
        $dep->setTrigger(new Trigger($triggerExp, $triggerFields));

        //Set the description field to be required if "important" is in the call subject
        $dep->addAction(ActionFactory::getNewAction('SetRequired', array(
            'target' => 'description',
            'label' => 'description_label',
            'value' => 'true'
        )));

        //Set the description field to NOT be required if "important" is NOT in the call subject
        $dep->addFalseAction(ActionFactory::getNewAction('SetRequired', array(
            'target' => 'description',
            'label' => 'description_label',
            'value' => 'false'
        )));

        //Evaluate the trigger immediatly when the page loads
        $dep->setFireOnLoad(true);
        $javascript = $dep->getJavascript();
        echo 

        SUGAR.forms.AssignmentHandler.registerView('EditView');

        {$javascript}

        
EOQ;
    }
}

?>

The above code creates a new Dependency object with a trigger based on the 'name' (Subject) field in of the Calls module. It then adds two actions. The first will set the description field to be required when the trigger formula evaluates to true (when the subject contains "important"). The second will fire when the trigger is false and removes the required property on the description field. Finally, the javascript version of the Dependency is generated and echoed onto the page.

Last modified: 2021-02-17 02:44:13