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

Parents
  • 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
  • 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?

Reply Children
No Data