Back to all How-tos

Output Sections conditionally

The case we deal with here is the one where Print sections are output according to a data field value condition.

For example, we have three sections named respectively Letter-Volley, Letter-Promo and Invoice. The data field Preference allows us to control what section should be executing based upon its value. So, if the data field Preference value is equal to Volleyball then the Letter-Volley and the Invoice sections are outputted, else the Letter-Promo and Invoice sections are outputted.

Steps

  1. In the Resources pane, Context > Print, right-click Letter-Volley.
  2. Select Make conditional, to open the Wizard for conditional print sections.
  3. Select the data field Preference.
  4. Set the condition to: Equal to.
  5. In the text field, type Volleyball.
  6. Repeat this for Letter-Promo section, with the only difference that you set the condition to: Not equal to.

As of OL Connect version 2021.1 the Wizard for conditional print sections has been dramatically improved, allowing very complex rules to be created without any need for scripting.

The same can be achieved by writing a Control Script. In older OL Connect versions that don’t have a Wizard for conditional print sections, this is the solution:

  1. In the Scripts pane, add a Control Script.
  2. Double-click the Control Script to open the script window and write the following lines:
    var lettertype = record.fields.Preference;
    if (lettertype == 'Volleyball'){
    merge.template.contexts.PRINT.sections['Letter-Promo'].enabled = false;
    }
    else {
    merge.template.contexts.PRINT.sections['Letter-Volleyball'].enabled = false;
    }

In order to test this concept, you can download and open these resources which are taken from the ‘Connect Foundations’ course.

To see the above conditions in action, toggle to the Preview tab in the template view and go through the different records. Notice that a red indicator appears next to the letter/document (in the Resources pane, under Contexts > Print) that is disabled by the script depending on the value of the Preference field.

API Features used in this how-to

Leave a Reply

Your email address will not be published. Required fields are marked *