Update field value

When making these changes be sure to make them in the custom directory, ie: 'public/legacy/custom/modules/<module>/…​'

1. Introduction

UpdateValue logic allows you to define rules to set a value on a field when other fields' value change

At the moment this does not work for relate fields

1.1 Example scenario

In this example we will be updating the value of Account’s employees field when the value of industry field changes.

Update Employees Value

2. Logic Metadata definition

The first thing to define is the logic entry in the metadata. This is where we are going to define the triggers for our value update.

The configuration for the logic can be added to the vardefs.php or the detailviewdefs.php.

In the following example we are going to add them to the Accounts module field vardefs.

2.1 Steps to add the logic on the custom vardefs

  1. Create a new field in custom extensions for Accounts module:

    1. Example: public/legacy/custom/Extension/modules/Accounts/Ext/Vardefs/employees.php

  2. Copy the 'employees' definition from public/legacy/include/SugarObjects/templates/company/vardefs.php

  3. Replace the logic entry to the employees definition with the code on the snippet below

    • Re-set permissions if needed (will depend on your setup)

  4. Run Repair and Rebuild on Admin menu

<?php

$dictionary['Account']['fields']['employees'] = array(
    'name' => 'employees',
    'vname' => 'LBL_EMPLOYEES',
    'type' => 'varchar',
    'len' => 10,
    'comment' => 'Number of employees, varchar to accomodate for both number (100) or range (50-100)',
    'logic' => [
        'update-value' => [
            'key' => 'updateValue',
            'modes' => ['detail', 'edit', 'create'],
            'params' => [
                'fieldDependencies' => [
                    'industry',
                ],
                'targetValue' => '450',
                'activeOnFields' => [
                    'industry' => ['Banking'],
                ]
            ]
        ],
    ]
);

3. Update Field Value Logic

Within logic for specific fields you can set multiple definitions for update-value. Each definition will work like an OR. For example:

    'name' => 'employees',
    'comment' => 'Number of employees, varchar to accomodate for both number (100) or range (50-100)',
    'label' => 'LBL_EMPLOYEES',
    'logic' => [
        'update-value' => [
            'key' => 'updateValue',
            'modes' => ['detail', 'edit', 'create'],
            'params' => [
                'fieldDependencies' => [
                    'industry',
                ],
                'targetValue' => '450',
                'activeOnFields' => [
                    'industry' => ['Banking'],
                ]
            ]
        ],
        // OR
        'update-value2' => [
            'key' => 'updateValue',
            'modes' => ['detail', 'edit', 'create'],
            'params' => [
                'fieldDependencies' => [
                    'industry',
                ],
                'targetValue' => '5000',
                'activeOnFields' => [
                    'industry' => ['Media'],
                ]
            ]
        ],
    ]

Now, Employees will update to 450 if the Industry is Banking OR 5000 if Industry is Media.

Update Employees Value Media

4. Properties description

  • Key

    • The key within the named logic array is stating which logic type will be used for the following. In this case it’s updateValue.

  • Modes

    • Modes are views you would like your updateValue logic to take effect on, as shown above it will be detail, edit and create. Another example of a mode that could be selected could be list for example.

4.1 Params

Field Dependencies

fieldDependencies is where you declare the field(s) that you would like your logic to depend on.

'fieldDependencies' => [
    'industry',
]

Active on Fields

activeOnFields is where you declare the field/values that trigger the change of the field to a required field.

In the example above we have the field employees set to 450 when Industry is Banking.

If we wanted it to set it to 450 if it was either Banking or another value such as Media, then a new value would be added like so:

'activeOnFields' => [
  'industry' => ['Banking', 'Media'],
],
Multiple Fields

Within the activeonFields you can add more than one field such as:

'activeOnFields' => [
    'industry' => ['Banking'],
    'name' => ['Example'],
],

This works like an AND. If Name is Example AND Industry is Banking.

When adding more fields to activeOnFields be sure to also add them to fieldDependencies.

Name Filled Example

Updated Employees Field

Target Value

targetValue will be the value you want the field to have if criteria is met.

In the example above it was 450.

For more information on different field logic see here.

Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.