doo.model

Scripting enables you to many advanced scenarios. During the edit, before the form is open, you can change the record and form behaviors.

Scripting enables you to many advanced scenarios. During the edit, before the form is open, you can change the record and form behaviors. In this case, you use Scripting in the Table menu:

Scripting in the table definition menu

Actions/script triggers

You can use these (lifecycle) events:

  • On model load - runs once, when the form for Add/Edit is open
  • On model change - runs every type the form changes. And once when the form is opened.
  • Before model save - before the record is saved.
  • After model save - after the record is saved.

Properties/Methods

doo.model contains all of the table fields and some other predefined properties:

  • isValid - when you set the whole model as invalid, it is not possible to save the form
  • isReadOnly - makes the whole form read-only
  • infoText - assign a message to this property in case you want to show something in the form header
  • hideCategory('categoryName') - it hides the whole category (tab) on the form
  • showCategory('categoryName') - and this shows it again

The properties have a format <[Header] (internalName)]> In case you change the header, the script works without any change. In case you change the internal property of the field, you need to project this into the script.

Every field of the record has these properties/methods:

  • value - the current property value
  • setValue(value) - make it easier to assign a value to linked fields, and dates ... See examples for specific data types.
  • originalValue - the value in the moment the edit started
  • currentlyChanged - if the value has been changed since the last run
  • isVisible - if the input is visible
  • isRequired - if the input is required
  • isEnabled - if the input is enabled
  • filterForDropdown - it is used to filter dropdowns, lookups
  • label - for changing the label/header of the table definition

Scripting Tips

In case, you are not sure, how the model object looks like or what the value of a variable is, check the value in the developer console of the browser

console.log(doo.model);

Or you can debug your JavaScript step by step and check values of variables and so on in the developer console of the browser using debugger command in your code (be sure to delete it when done :))

debugger;

You can open the developer console by pressing F12 in Chrome/Edge browser or CTRL+SHIFT+I in Opera browser and similarly in other browsers.


Examples

Enable/Disable a field

The field "Url" should not be enabled in case the type is not "Digital":

doo.model.<[Url (asewx)]>.isEnabled = doo.model.<[Type (keyc5)]>.value === 'Digital';

Visibility of a field

The field "Url" should not be visible in case the type is not "Digital":

doo.model.<[Url (asewx)]>.isVisible = doo.model.<[Type (keyc5)]>.value === 'Digital';

Setting a Dropdown value

Prefill the State field according to the user branch (we added the field branch to the Users table).

doo.model.<[State (stawer)]>.value = doo.currentUser.branch === '550' ? 'New' : 'Active';

Form (in)validation

The form is not valid in case ...

doo.model.isValid = doo.model.<[Url (url)]>.value.indexOf('http://') === -1;

Dropdown filtering by roles

Filter values for roles. The filtered field State is a Dropdown type

// Admin role can choose from two states, others only one
if (doo.currentUser.roles.indexOf('Admin (system)') != -1) {
   doo.model.<[State (state)]>.filterForDropdown = ['Active', 'Closed'];
} else {
   doo.model.<[State (state)]>.filterForDropdown = ['New'];
}

Lookup filtering

Filtering of linked lookup (this type of filter enables only a few simple conditions!)

doo.model.<[Members (x21sf9ws40)]>.filterForDropdown = "Branch === B01";
doo.model.<[Members (x21sf9ws40)]>.filterForDropdown = "Branch === A01 or Branch === B01";
doo.model.<[Members (x21sf9ws40)]>.filterForDropdown = "Branch contains B01";

Setting the lookup value

The value must be loaded from another table, so you do not enter the exact value, but search criteria. For links to many records, the value can be an array or a single value. For links to one, the value/result should be a single value. 
You can find more tips about filters in this chapter

// full text search
doo.model.<[Members (members)]>.setValue('MyFulltextSearch');
// in case we kow an exact value id
doo.model.<[Members (members)]>.setValue(['865f8bac-ed8a-4979-972f-3f1706fb1bb1']);
// simplified filter
doo.model.<[Members (members)]>.setValue({ filter: "surname(in)(Black)" });