How do I create a label field that shows a warning or not?

I have an API that returns true or false based on if the id is associated with a course that has been billed.  I needed to create a API, because the billed value is in a  parent module.

How do a create a custom field that is a label that displays a warning message based on a true value?

Thank you

  • Hi

    Perhaps you may prefer to apply the custom Dependency SetColor.

    Save the code bellow in the file custom/include/Expressions/Actions/SetColorAction.php

    <?php
    /*
     * Your installation or use of this SugarCRM file is subject to the applicable
     * terms available at
     * http://support.sugarcrm.com/Resources/Master_Subscription_Agreements/.
     * If you do not agree to all of the applicable terms or do not have the
     * authority to bind the entity as an authorized representative, then do not
     * install or use this SugarCRM file.
     *
     * Copyright (C) SugarCRM Inc. All rights reserved.
     */
    
    /**
     * Class SetColorAction
     *
     * SugarLogic Action to set a color to a field
     */
    
    require_once("include/Expressions/Actions/AbstractAction.php");
    
    class SetColorAction extends AbstractAction
    {
    	/**
    	 * Store the expression
    	 *
    	 * @var mixed|string
    	 */
    	protected $expression = '';
    
    	/**
    	 * The field we are targeting
    	 *
    	 * @var string
    	 */
    	protected $targetField = '';
    
    	/**
    	 * All colors which may be returned by given expression
    	 *
    	 * @var string
    	 */
    	protected $colors = '';
    
    
    	/**
    	 * Constructor
    	 *
    	 * @param array $params
    	 */
    	public function __construct($params)
    	{
    		$this->targetField = $params['target'];
    		$this->expression = str_replace("\n", '', $params['value']);
    		$this->colors = str_replace("\n", '', $params['colors']);
    	}
    
    	/**
    	 * Returns the javascript class equivalent to this php class
    	 *
    	 * @return string javascript.
    	 */
    	public static function getJavascriptClass()
    	{
    		return <<<JS
    		SUGAR.forms.SetColorAction = function(target, valExpr, colorsExpr) {
    			if(_.isObject(target)) {
    				this.expr = target.value;
    				this.target = target.target;
    				this.colors = target.colors;
    			} else {
    				this.expr = valExpr;
    				this.target = target;
    				this.colors = colorsExpr;
    			}
    		};
    		SUGAR.util.extend(SUGAR.forms.SetColorAction, SUGAR.forms.AbstractAction, {
    			exec : function(context) {
    				if(typeof(context) == 'undefined') context = this.context;
    
    				context.target = this.target;
    
    				var value = this.evalExpression(this.expr, context), colors = this.evalExpression(this.colors, context);
    
    				_.each(colors, function(color) {
    					context.removeClass(this.target, color, false);
    				}, this);
    
    				if(!_.isUndefined(value) && !_.isEmpty(value)) context.addClass(this.target, value, false);
    			}
    		});
    JS;
    	}
    
    	/**
    	 * Returns the javascript code to generate this actions equivalent.
    	 *
    	 * This is used for BWC modules only
    	 *
    	 * @return string javascript.
    	 */
    	public function getJavascriptFire()
    	{
    		return "new SUGAR.forms.SetColorAction('{$this->targetField}','" . addslashes($this->expression) . "', '{$this->colors}')";
    	}
    
    	/**
    	 * Applies the Action to the target.
    	 *
    	 * @param SugarBean $target
    	 */
    	public function fire(&$target)
    	{
    		//This is a no-op under PHP
    	}
    
    	/**
    	 * Returns the definition of this action in array format.
    	 *
    	 * @return array
    	 */
    	public function getDefinition()
    	{
    		return array(
    			'action' => $this->getActionName(),
    			'params' => array(
    				'target' => $this->targetField,
    				'value' => $this->expression,
    				'colors' => $this->colors
    			)
    		);
    	}
    
    	/**
    	 * The Action Name
    	 *
    	 * @return string
    	 */
    	public static function getActionName()
    	{
    		return 'SetColor';
    	}
    }
    

    Create the file custom/Extension/<module>/Ext/Dependencies/SetColor.php with content bellow, obviously update it accordingly:

    <?php
    $dependencies['Products']['setcolor'] = [
    	'hooks' => ["edit", "view"],
    	'trigger' => 'equal($the_field_c, true)',
    	'triggerFields' => ['the_field_c'],
    	'onload' => true,
    	'actions' => [
    		[
    			'name' => 'SetColor',
    			'params' => [
    				'target' => 'the_field_c',
    				'value' => '"label red"',
    				'colors' => 'createList("label red")',
    				'errorValue' => '"label red"',
    			],
    		],
    	],
    ];
    
    ?>
    

    Run the following repair actions:

    • Quick Repair and Rebuild
    • Rebuild Sugar Logic Functions
    • Rebuild Javascript Languages
    • Rebuild JS Grouping Files
    • Clear Additional Cache

    Finally clear browser cache and enjoy it.

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • You can use the "related" Sugar Logic function in the trigger line of André's code to get the value of the field in the parent module.

  • I just want it to return a message to the screen saying this course has been billed.

    Sorry, I am having trouble understanding what the code is doing in order to translate it to what I need.

    Where would you add the Action to a field?

  • What about having e checkox field for the true and false value of "billed"?

    Then you could create a message text field which is calculated and dependent.

    The formula for the calculated value of the text could be: ifElse($billed_c,"BILLED","")

    The dependency formula for visibility could be: $billed_c

    Now if the billed flag is set the textfield will be shown with the text BILLED.

    Everything can be done in Studio resp. Modulebuilder

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH

  • The answer to whether the course has been billed or not is on a parent module.  The problem is I can't access that from studio, so I need to write code to do it.

  • "I needed to create a api because the billed value is on a parent module" 
    Is there a direct relationship between the two? Which you could retrieve with load_relationship() on either side of the relationship? If so you can work with a computed value using the related property where you pick up the 'billed' property from the parent (if that is available). And if that is the case you can create addvalidationtask on the child that does the check and shows an warning (https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.0/Cookbook/Adding_Field_Validation_to_the_Record_View/)

    Another aproach would be to have an validationtask on the child that loads data using your api and depending on that throws the error or not. Here is some information you can use to start with: https://sugarclub.sugarcrm.com/dev-club/f/questions-answers/1340/record-js---addvalidationtask---fetch-data

    Be aware that the save function of create and record are a bit different. I don't know if this is still the case for 10.x branch but in previous branches the difference would cause validationtasks to be ignored on the create view becuase the way save waterfall function was implemented.

  • In the formular editor of the studio you can address related fields too.

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH