Code


The code tag is available in every field type and offers the option of implementing the output of a field as required using JavaScript.

You can find more examples of fields or an introduction to JavaScript in the Layouter manual.

Calls in code

Within the <Code> element, two variants are possible for executing code.

Shortened call

  • If the code consists of only one statement, it can be written directly into the block.

  • The return statement is implicit.

<Text Name="Footer"> <Code>$.getText('Profile.User.FirstName')</Code> </Text>

Detailed call

  • If the code consists of several statements and/or contains variables, it must be written within the function main().

  • Within the same code block, other functions can also be defined for use in main.

  • The return statement in main must be set explicitly.

<Text Name="Footer"> <Code> function main() { let firstName = $.getString('Profile.User.FirstName'); if (firstName == 'Foo') { firstName += 'Bar'; } return firstName; } </Code> </Text>

API description

In a code element $ initiates the use of the primedocs API.

Access to fields

Forms fields, fields and profile fields can be accessed with $('[field]'). Below is an example for each field type:

Forms field

<Code>$("Forms.Subject")</Code>

Field

<Code>$("Header")</Code>

Profile field: User

<Code>$("Profile.User.FirstName")</Code>

Profile field: Organisation

<Code>$("Profile.Org.Unit")</Code>

Function calls

Furthermore, $. initiates the call of a primedocs function: $.myFunction()

It is possible to use general JavaScript functions (e.g. foreach() on arrays (ObjectCollections) or replace() on strings). Consult Mozilla's JavaScript documentation for more information.

Return value of field types

The return value of main() in code must always correspond to the field in terms of data type.
For example, it is not possible to output a date forms field in a text field. Conversely, a date field cannot output string/text but only date forms fields or date fields.

Here is an overview:

Field type

Return type

Field type

Return type

Text

String

FormattedText

FormattedText

WordContent

WordContent

Date

Date

YesNo

Boolean

Picture

Image (z. B. Profile.Org.Logo)

Native functions

All native functions can be viewed in the official Mozilla documentation and tried out via a Playground. According to this list, these can also be used in primedoc's code.

primedocs' own functions

The following is a sortable table with all primedocs functions: which API they belong to and the respective return type. A function is then always composed of [API].[Function].

API

Function

Return type

API

Function

Return type

$

getDateAsText()

String

$

joinNonEmpty()

String

$

formatDate()

String

$

getDate()

Date

$.formattedText

fromText()

FormattedText

$.formattedText

parse()

FormattedText

$.wordContent

fromFormattedText()

WordContent

$.wordContent

fromText()

WordContent

$.translations

getText()

String

$.translations

getFormattedText()

FormattedText

$.snippets

getFormattedText()

FormattedText

$.snippets

getWordContent()

WordContent

$.formattedText

getBuilder()

FormattedText-Builder

$.wordContent

getBuilder()

WordContent-Builder

Getters

API

Function

Return type

API

Function

Return type

 

$()

Feldtyp (je nach Id)

$

get()

Feldtyp (je nach Id)

$

getText()

String

$

getDate()

Date

$

getFormattedText()

FormattedText

$

getWordContent()

WordContent

$

getYesNo()

YesNo

$

getObject()

Object

$

getObjectCollection()

ObjectCollection

Functions with named parameters

Some functions contain an object with a comma-separated list of key-value pairs as the second parameter. We call the key-value pairs ‘named parameters’.

The values of the key-value pairs can be any type of string. This includes

  1. Calling profile data / Forms fields / Fields, see below Key name

  2. Fixed text, see below Key info

  3. References to variables, see below Key function

In the following example, a global translation of type FormattedText is returned in a field header. The function call to getFormattedText requires the ID of the global translation and, as a second parameter, a list of three key-value pairs with the keys name, info and function.

<FormattedText Name="Header"> <Code> function main(){ let myVariable = $("Forms.SignerMain.User.Function"); return $.translations.getFormattedText("FormattedTexts.FooterBoldWithParams", { name: $("Forms.SignerMain.User.FirstName") + " " + $("SignerMainLastName"), info: "Fixtext ist auch möglich", function: myVariable }); } </Code> </FormattedText>

Functions in detail per API

Im Folgenden wird jede Funktion detailliert beschrieben:

Function

Purpose

Function

Purpose

getText(String id)

  • Fetches unformatted text explicitly via id. Alternatively, an implicit call can also be made (see example).

  • Parameter id: Field id. The field after id must output a string.

  • Return type: String

getDateAsString(String id)

  • Fetches a date field using id and converts it to unformatted text.

  • Parameter id: Field id. The field behind id must output a date.

  • Return type: String

joinNonEmpty(String separator, String item1, String item2, […])

  • Combines the items of type String in the list and then separates them with the separator.

  • Parameter separator: String. Character that acts as a separator between all list elements.

  • Parameters item1, item2 and ff.: all parameters after separator form a list of strings that are output one after the other.

  • Return type: String

$.joinNonEmpty(' / ", $("Profile.Org.Title'), $('Profile.Org.Unit')) Result: 'Example company / example department'

More information here: https://primesoft-group.atlassian.net/wiki/pages/resumedraft.action?draftId=513114113&draftShareId=80b57d2b-c81a-47ab-b7c1-c74d27363d16

formatDate(Date date, String format)

$.formatDate($("Forms.Date").Value, "yyyy-MM-dd")

formattedText API

Function

Purpose

Function

Purpose

formattedText.fromText(String text)

  • Converts a string to a FormattedText.

  • Parameter text: String or field that returns a string.

  • Return type: String

Example with a string:

$.formattedText.fromText('A funny sentence.')

Example with reference to a field:

$.formattedText.fromText($('Forms.Subject'))

formattedText.parse(String html, Object parameters)

  • Builds a FormattedText from the arguments.

  • Parameter html: String containing the HTML definition of the FormattedText and any placeholders in the format {{placeholderName}}.

  • Parameter parameters: Description of all parameters that occur as placeholders in html.

  • Placeholder names within a FormattedText must be unique.

  • Return type: FormattedText

Example without named parameters:
$.formattedText.parse('<p data-word-style-id='Quote'>paragraph in built-in "Quote" style</p>');

Example with named parameters:
$.formattedText.parse('<p data-word-style-id='Quote'>{{something}}</p>', { something: 'Paragraph in built-in "Quote" Style' });

formattedText.getBuilder()

See chapter Code | Builder API

Creates a builder object in order to append FormattedText or Text to it using the append() function.

$.formattedText.getBuilder()

wordContent API

Function

Purpose

Function

Purpose

wordContent.fromFormattedText(FormattedText ft)

  • Converts a FormattedText to a WordContent.

  • Parameter ft: Parameter type: FormattedText, mandatory.

  • Return type: WordContent

Example with FormattedText from translation:

$.wordContent.fromFormattedText($.translations.getFormattedText("FormattedTexts.CopyTo"))

Example with FormattedText from snippets

$.wordContent.fromFormattedText($.snippets.getFormattedText("Introduction"))

Example with reference to field:

$.wordContent.fromFormattedText($("IntroductionFT"))

wordContent.fromText(String text)

  • Converts a text to a WordContent.

  • Parameter text: Parameter type: String, mandatory.

  • Return type: WordContent

Example with a string:

$.wordContent.fromText("A funny sentence.")

Example with reference to a field:

$.wordContent.fromText($("Forms.Subject"))

wordContent.getBuilder()

See chapter Code | Builder API

Creates a builder object in order to append WordContent, FormattedText or Text to it using the append() function.

$.wordContent.getBuilder()

translations API

The following functions are available in the translations group:

Function

Purpose

Function

Purpose

translations.getText(String id)

  • Retrieves an unformatted translation from the global translations.

  • Parameter id: Parameter type: String, mandatory, specification of the ID of the translation entry in the global translations

  • Return type: String

$.translations.getText("Texts.Subject")

translations.getFormattedText(String id, Object parameters)

Retrieves a formatted translation from the global translations.

  • Parameter id: Parameter type: String, mandatory, specification of the ID of the translation entry in the global translations

  • Parameter parameters: Parameter type: Object, if the translation has parameters, mandatory:
    List of key-value pairs with values according to the translation (types in handlebars: strings, arrays or booleans).

  • Return type: FormattedText

Example without parameters:

$.translations.getFormattedText("ContractTitle")

Example with parameters:

$.translations.getFormattedText("FormattedTexts.Paragraphs.BoldNormal", { bold: $("Forms.Subject"), normal: "Untertitel-Text" } )

snippets API

The following functions are available in the snippets group:

Function

Purpose

Function

Purpose

snippets.getWordContent(String key, Object placeholders)

  • Retrieves a WordContent text module with a specific key.

  • Parameter key: mandatory, parameter type: String, specification of the snippet key

  • Parameter placeholders: Parameter type: Object, there are SnippetPlaceholders, mandatory,
    List of key-value pairs with string values

  • Return type: WordContent

SnippetPlaceholder can only be filled with strings.

Example without placeholders:

$.snippets.getWordContent("Introduction")

Example with placeholders:

$.snippets.getWordContent("Introduction", { dateToday: $.getDateAsString("Forms.Date"), guests: $("Guests") } )

snippets.getFormattedText(String key)

  • Retrieves a formatted text as a text module with a specific key.

  • Parameter key: Parameter type: String, mandatory, specification of the snippet key

  • Return type: FormattedText

$.snippets.getFormattedText("FooterFTSnippet")

Builder API

The Builder API can be used in a FormattedText field or a WordContent field and enables the assembly of Texts, FormattedTextss and WordContents in the sense of a modular system, in which each text, regardless of type, is strung together.

Use

The use of the Builder only makes sense if several text paragraphs have to be strung together conditionally.

Functions

Function

Purpose

Function

Purpose

formattedText.getBuilder()

  • Creates a FormattedText builder object to append FormattedText or text with the append() function.

  • Parameters: none

  • Return type: FormattedText builder object

  • Call in a FormattedText field: $.formattedText.getBuilder()

wordContent.getBuilder()

  • Creates a WordContent builder object to append WordContent, FormattedText or Text with the append() function.

  • Parameters: none

  • Return type: WordContent builder object

  • Call in a WordContent field: $.wordContent.getBuilder()

append(WordContent/FormattedText content)

  • Adds the content of parameter content to the builder pipeline.

  • Parameter content: mandatory, parameter type WordContent or FormattedText, depending on which field type you are in.

  • Return type: WordContent or FormattedText

  • Call on the builder object, e.g. in a WordContent field:

builder
.append($.snippets.getWordContent("Introduction"))
.append($.wordContent.fromText(" - mit Builder"))

See detailed example further below

build()

  • Last mandatory function call in a series of builder functions. Triggers the build pipeline and then assembles all 'appended' parts from left to right.

  • Parameters: none

  • Return type: FormattedText/WordContent

  • Call on the builder object: builder.build()

Application

The following rules apply when using the Builder:

  • A FormattedText field can only output FormattedText and Text.

  • A WordContent field can output WordContent, FormattedText and Text.

The non-field type objects must first be converted to the target field type. For example, in a WordContent field, a FormattedText must be called from the conversion function fromFormattedText():

.append($.wordContent.fromFormattedText($.translations.getFormattedText('FormattedTexts.CopyTo')))

The same for WordContent fields or FormattedText fields and the output of text:

.append($.formattedText.fromText(' - with Builder'))

Example: Builder for WordContent field and FormattedText field

Getters

Funktion

Zweck

Funktion

Zweck

$(String name) / $.get(String name)

  • Gets a field according to the argument.

  • Parameter name: Parameter type: String, mandatory, specification of the field name

  • Return type: corresponding to the retrieved field

  • see also Code | Access to fields

$("Profile.User.FirstName") / $.get("Forms.Date")

$.getText(String name)

  • Retrieves a text.

  • Parameter name: Parameter type: String, mandatory, specification of the field name

  • Return type: Text

$.getText("Forms.Subject")

$.getDate(String name)

  • Retrieves a date.

    Parameter name: Parameter type: String, mandatory, specification of the field name

    Return type: Text

$.getDate("DateWrittenOut")

$.getFormattedText(String name)

  • Retrieves a FormattedText.

  • Parameter name: Parameter type: String, mandatory, specification of the field name

  • Return type: FormattedText

$.getFormattedText("Forms.Subject")

$.getWordContent(String name)

  • Retrieves a WordContent.

  • Parameter name: Parameter type: String, mandatory, specification of the field name

  • Return type: WordContent

$.getWordContentText("Forms.Subject")

$.getYesNo(String name)

  • Retrieves a YesNo.

  • Parameter name: Parameter type: String, mandatory, specification of the field name

  • Return type: YesNo

$.getYesNo("Confidentiality")

$.getObject(String name)

  • Retrieves an object.

  • Parameter name: Parameter type: String, mandatory, specification of the name of a field

  • Return type: Object

$.getObject("SingleRecipient")

$.getObjectCollection(String name)

  • Retrieves an ObjectCollection.

  • Parameter name: Parameter type: String, mandatory, specification of the field name

  • Return type: ObjectCollection

$.getObjectCollection("Participants")


Use customised functions

With GlobalCode, specially defined functions can also be used in a code definition. For more information, read here: Global Configurations | GlobalCode .


CDATA-Tag

The CDATA tag (<![CDATA[My text]]>) ensures that everything between the start and end tag does not go through the parser. This is called ‘escaping’.

Without a CDATA tag, for example, the logical AND operator must be output as an HTML entity: &amp;. By using the CDATA tag, you can only use &, which results in more readable code.

We recommend the use of CDATA tags if the code in the field contains the following:

  • logical AND operators &

  • Comparison operators (<, <=, >, >=), e.g. in loops

  • Arrow expressions (=>)

Example with CDATA tag and & instead of &amp;

PrimeSoft AG, Bahnhofstrasse 4, 8360 Eschlikon, Switzerland