You are here: Scripting Reference > Functions > Program Control > ProcessMasterRecord

ProcessMasterRecord

Declaration

function ProcessMasterRecord : boolean;

Example

procedure OnBeforeEvent(var Value:Variant);

begin

If Global1 <> KeyField.Value then

begin

ProcessMasterRecord

Exit; //call Exit to stop this script now and start on the header immediately

end;

 

//otherwise other code here

end;

Description

ProcessMasterRecord is only available in the OnBeforeMap event of a dataset. By calling this function processing will revert to the parent dataset. The See "Program Flow" loop is broken and returned to the next level up.

 

Normally this function is used when processing source definition that is a single dataset of lines needing to be split into a destination definition that has header and line datasets. The source dataset probably has a key field that is the same across lines in the same group eg if the source is order lines each line may have an order number field. Each time the order number changes you need to create a new record in the destination header dataset.

 

You would need to configure you map in the following way;

//LINKEDDATA EVENT OF CHILD DATASET

procedure LinkedDataEvent(var LinkedData:TdaQueryDataView); //ORDER_LINE

begin

LinkedData := ORD_LINE;

end;

Firstly you need to set the LinkedData of the child dataset - this should be set to the source dataset. Essentially you are linking the source lines to the destination lines.

 

//ONBEFOREMAP EVENT OF PARENT DATASET

procedure OnBeforeEvent(var Value:Variant);

begin

Global1 := KeyField.Value;

end;

The above script resides on the OnBeforeMap of the parent dataset. It stores the key field's value in a global variable.

//ONBEFOREMAP EVENT OF CHILD DATASET

procedure OnBeforeEvent(var Value:Variant);

begin

If Global1 <> KeyField.Value then;

begin

ProcessMasterRecord

Exit; //call Exit to stop this script now and start on the header immediately

end;

end;

The above script resides on the OnBeforeMap event of the child dataset. Each time a line is processed it checks to see if the key field is still the same value. If it has changed value you need to call ProcessMasterRecord to start a new header record.

 

How does this work?

When the mapping first starts a header record is processed in the destination (remember it is pull based mapping running off the destination dataset). The header has no See "LinkedData" therefore one record will be processed. During processing of this record any reference to source fields will read values from the first record in the source. The current value of the key field is stored in a global for later comparison.

 

After the header it will start to process the child dataset. Since the LinkedData is set to the source dataset it will continue processing the lines either until there are no more, or until the ProcessMasterRecord is called (due to change in key field value).

 

ProcessMasterRecord causes the script engine to start processing the header record again. This time any reference to source fields will be reading values from the current record in the source (The first record with the new key field). Again after the header is processed it will continue to process the lines.