Table of Contents

Formulas Examples (v2)

you are here (click to zoom) - discover the stack

Here we publish various examples of formulas, for you to use/optimize to your needs. The samples should show you

  1. Debug Formulas
  2. Start Formulas
  3. Simple String Formulas TODO
  4. Group Information Formulas TODO
  5. Date Formulas TODO
  6. Promise TODO

Debug Formulas

Just Dump Everything

This formula will dump all the information you have to the console, to better inspect the objects.

v2((data, context) => {
  // Don't do anything, just put a bunch of information in the debug on each cycle
  console.log('debug data', data);
  console.log('debug context', context);
  // don't return anything, so no changes actually happen (commented out)
  // return data.value;
});

Formula Target Information

Formula targets let the formula know what it is running for.

v2((data, context) => {
  console.log('formula target', context.target);
  // the target type
  console.log('formula target type', context.target.type);
  // the target name
  console.log('formula target name', context.target.name);
});

Set Value On Start

Basics or Run-On-Start Only

If a formula should only run once, it can do it's work and return stop : true. Here's an example:

v2((data, context) => {
  console.log('this will run on start only');
  // If it already contains a value, don't change it and stop the formula.
  if (data.value) return { stop: true };
});

This example also sets a value, and then stops the formula:

v2((data, context) => {
  console.log('this will run on start only');
  // If it already contains a value, don't change it and stop the formula.
  if (data.value) return {
    value: 42,
    stop: true,
  };
});

Fill an Empty Field on Start Only

This formula will set a specific value when the form is opened. For example, it might pre-fill a new field on existing data which didn't exist before (so the default value from the configuration won't apply, since it's not new)

Assume that this is on a field Status which should always get the word NEW.

v2((data, context) => {
  // If it already contains a value, don't change it and stop the formula.
  if (data.value) return { stop: true };

  // If not yet, set it to `NEW` and also stop the formula.
  // Otherwise editing won't work, since it will continue to copy the value.
  return { value: 'NEW', stop: true };
});

Fill an Empty Field on Start from Another Field

This formula will set a specific value when the form is opened. For example, it might pre-fill a new field on existing data which didn't exist before (so the default value from the configuration won't apply, since it's not new)

Assume that this is on a field NewName which should be copied once from the Name field, but later allow editing freely.

v2((data, context) => {
  // If it already contains a value, don't change it and stop the formula.
  if (data.value) return { stop: true };

  // If not yet, copy the value from Name and also stop the formula.
  // Otherwise editing won't work, since it will continue to copy the value.
  return { value: data.Name, stop: true };
});

Reset a string-url field so it will update as the title changes

Imagine a UrlKey field of type string-url fields often have a field mask like [Title]. This results in the url-key being generated from the Title field. But the mechanics only apply the first time the entity is created. This is because future changes of the Title should usually not modify the link any more.

But now imagine if you copy an entity which has such a key. In that case, the title should usually be modified and the key should change again. So we want to flush the field, but only if a) the item is a copy (detected by EntityId being 0).

v2((data, context) => {
  // On first run, if it's edit (already has an id) then stop / ignore this formula
  if (context.target.entity.id != 0)
    return { stop: true };

  // Info to developer, as he might be confused
  if (data.value != '')
    console.log('Formula on ' + 
      context.target.name + 
      'will reset the value because it assumes copy. ' +
      'The field may still have a value, since it should be auto-generated by other values' +
      " so don't be surprised. " + 
      "But if it is auto-generated, it should continue to update as the master-value changes."
    );

  // It's new because ID == 0. On new, the url is already '' but on 'copy' we must reset it
  return {
    value: '',  // force it to '' (only has effect on copy)
    stop: true, // stop processing this formula
  };
});

History

  • Examples started 2sxc 20.00