Custom validation SetRequired using Dependency

Hello Everyone,

I have custom requirement:

1) I have a custom field call_rating

2) It is required when Meeting status is "Held" and user having role "Sales".

3) In either condition it is not mandatory.

I am able to achieve this requirement using overriding create.js and record.js. But then it is not working in mobile view.

After reading this link, I come to know that I can achieve that using dependency. but still it is not working.

Also I have a doubt regarding the javascript block we have in code block.

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_8.3/Architecture/Sugar_Logic/Using_Suga… 

My code:

<?php
global $current_user;
include_once('modules/ACLRoles/ACLRole.php');
$roles = getUserRoleNames($current_user->id);

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 MeetingsViewEdit extends ViewEdit
{

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

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

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

// if(in_array("Sales", $roles)){

//Set the description field to be required if "important" is in the call subject
$dep->addAction(ActionFactory::getNewAction('SetRequired', array(
'target' => 'call_rating_c',
'label' => 'Call Rating',
'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' => 'call_rating_c',
'label' => 'Call Rating',
'value' => 'false'
)));

//Evaluate the trigger immediatly when the page loads
$dep->setFireOnLoad(true);
$javascript = $dep->getJavascript();
echo
SUGAR.forms.AssignmentHandler.registerView('EditView');
{$javascript}
EOQ;
}
}

?>

Help is highly appreciated.

  • Hi @Kishor Mali

    I think you can do this by adding extenstion, If you do this only in javascript it will never work on mobile.

    For example I created the following for my Opportunities module

    <sugar_root>\custom\Extension\modules\Opportunities\Ext\Dependencies

    for you I think it would be :

    <sugar_root>\custom\Extension\modules\Meetings\Ext\Dependencies


    - create a file named after your field for easier reading example: call_rating_required_php

    <?php
    $dependencies['Meetings']['status'] = array(
    'hooks' => array("edit"), // this is where you want it to fire
    'trigger' => 'true', // to fire when fields change
    'triggerFields' => array('status'), // field that will trigger this when changed
    'onload' => true, // fire when page is loaded
    //Actions is a list of actions to fire when the trigger is true
    //Actions we want to run, you can set multiple dependency action here
    'actions' => array(
    array(
    'name' => 'SetRequired', // function to trigger
    //The parameters passed in will depend on the action type set in 'name'
    'params' => array(
    'target' => 'call_rating',
    'label' => 'call_rating_c_label',
    'value' => 'equal($status, "Held")', // the Logic for it to trigger if the field is required or not
    ),
    ),
    ),
    );

    *However , I am not sure I can help you with the AND USER HAS ROLE Sales , as I would not know how to include that part.

  • Peter Chimienti : Thanks for the implementation. But in my case, I have custom requirements which are not possible just by using pre-defined set of Dependencies. That's why I am thinking of developing custom function which I can utilize in dependency. But the code provided in the documentation look like buggy.

  • have you found the solution? i have a similar situation. please provide the solution if found. Thanks

  • Mobile app doesn't support custom dependencies, so a custom dependency which evaluates the User's roles will only work on client base.

    Save the attached file into folder custom/include/Expressions/Expression/Boolean/

    Repair sugarLogic functions (twice) and finally quick Repair and Rebuild.

    You will be able to create a dependency like that:

    and(equal($status, "Held"), isUserInRoleList(createList("Sales")))

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada