fm:assign

Used for changing the value of an element in a DOM.

SYNTAX

There are two ways of using the fm:assign function. The first one initialises the element you are referring to if it doesn’t already exist:

<fm:assign initTarget=":{root}/A" textValue="PRIMARY_DATA"/>

The other only assigns the value to an existing target element:

<fm:assign setTarget=":{root}/A" textValue="PRIMARY_DATA"/>

As you can see you must supply a target (that is initialised or just set) and a value. This value can be a text value, in the above example the target will be set to “PRIMARY_DATA”. You can also use an XPath expression in the following way:

<fm:assign initTarget=":{root}/A" expr=":{root}/B"/>

NB: <fm:assign initTarget=”xPath”/> only works for a single element. To use fm:assign with a repeating element structure (including list items, check boxes, radio groups, etc.), you would first need to init the elements using <fm:init>, followed by <fm:assign setTarget=”XPath”/>.

CONCEPTS

Initialising a tree structure

Whenever you are initialising data, using fm:init or fm:assign, if you target a node in a tree structure that does not exist, FOX will initialise all its ancestors at the same time. See Example 1 below.

NB: You cannot use an fm:assign to initialise repeating elements. In these cases, you must first initialise the elements and then assign values to them.

The :{assignee} context

When you are assigning a value using a setTarget and an XPath, the :{assignee} context refers to the original value of the element. This means you can use it in your assignment. See Example 2 below.

EXAMPLES

Example 1

The following assignment is used in a module to create a CONTEXT structure. By using this assignment the CONTEXT element is created, as is the SUBJECT sub element that in turn has a sub element called REF_ID1. Contexts are covered in other worksheets.

<fm:init target=”/*/CONTEXT/SUBJECT/REF_ID1”/>
<fm:assign setTarget=”/*/CONTEXT/SUBJECT/REF_ID1”
expr=”sring(concat(:{params}/P_ID), ‘OU’)”/>

The REF_ID1 element will get the value of the XPath which is concatenating the P_ID element in the :{params} DOM with the letters ‘OU’.

Example 2

You can use the :{assignee} context to compute the new value when you assign a value to an element:

<fm:assign initTarget=”/*/ITEM/PRICE”
expr=”round(:{assignee} div 100 * 105 * 100) div 100”/>

Example 3

You can use :{assignee} multiple times within an fm:assign statement:

<fm:init target=”/*/EMPLOYEE_LIST/EMPLOYEE/FULLNAME”/>
<fm:assign setTarget=”/*/EMPLOYEE_LIST/EMPLOYEE/FULLNAME”
expr=”string(concat(:{assignee}/../FORENAME, ‘ ’,
:{assignee}/..SURNAME))”/>

EXERCISES

Please use your XX_ORDERMODULE (where XX are your initials) file for the following exercises.

Exercise 1

Create an action named “action-change” that is a button. This action should use an fm:assign to increase the UNIT_PRICE and PRICE elements of all items in your Order List by 15 percent.

Exercise 2

In the entry-theme, for each item in your Order List, initialise an element named AVAILABLE that has the value ‘true’.

Exercise 3

Add an assign to action-change that will set the AVAILABLE element to ‘false’ for all items where the ordered quantity is higher than five.