---
title: "Lusid.CleardownModule.Rule.Writer"
slug: "lusidcleardownmodulerulewriter"
updated: 2024-08-22T09:16:16Z
published: 2024-08-22T09:16:16Z
canonical: "support.lusid.com/lusidcleardownmodulerulewriter"
---

> ## Documentation Index
> Fetch the complete documentation index at: https://support.lusid.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lusid.CleardownModule.Rule.Writer

| **Type** | **Read/write** | **Author** | **Availability** |
| --- | --- | --- | --- |
| [Data provider](/v1/docs/what-are-a-data-provider-and-a-direct-provider) | Write | FINBOURNE | Provided with LUSID |

Providing you have sufficient [access control permissions](/v1/docs/creating-policies-to-control-access-to-providers-for-different-luminesce-end-users), the `Lusid.CleardownModule.Rule.Writer` provider enables you to write a [Luminesce SQL query](/v1/docs/understanding-the-luminesce-sql-query-syntax) that sets [cleardown rules](/v1/docs/how-do-i-add-a-cleardown-module-with-cleardown-rules-to-a-chart-of-accounts) for an existing cleardown module. [See how to create a cleardown module](/v1/docs/lusidcleardownmodulewriter).

You must construct a valid table of data to write, one cleardown rule per record. `Lusid.CleardownModule.Rule.Writer` lists the fields (columns) available to populate with values for each record, and has a set of parameters to help you construct a valid table.

Note the following:

- Your query replaces any existing cleardown rules in the cleardown module. To retain existing cleardown rules, include them in your query.
- The order in which cleardown rules are specified in a cleardown module is significant; LUSID uses **the first matching cleardown rule found**. You can use the `RulePriority` field to set the order of cleardown rules.

**See also:** [Lusid.CleardownModule.Rule](/v1/docs/lusidcleardownmodulerule)

## Basic usage

```sql
@table_of_data = <select-statement>;
select * from Lusid.CleardownModule.Rule.Writer where ToWrite = @table_of_data;
```

## Query parameters

`Lusid.CleardownModule.Rule.Writer` has parameters that help you construct a valid table of data to write.

> **Note:** The `ToWrite` parameter is mandatory and used to actually write the table of data into LUSID.

To list available parameters, their data types, default values, and an explanation for each, run the following query using a [suitable tool](/v1/docs/what-tools-are-available-to-write-luminesce-queries):

```sql
select FieldName, DataType, ParamDefaultValue, Description from Sys.Field where TableName = 'Lusid.CleardownModule.Rule.Writer' and FieldType = 'Parameter';
```

## Data fields

`Lusid.CleardownModule.Rule.Writer` lists the fields you can populate in your table of data to write.

The following fields are mandatory to include in the table of data:

| **Mandatory fields** | **Notes** |
| --- | --- |
| `ChartOfAccountsScope` `ChartOfAccountsCode` `CleardownModuleCode` `RuleId` `GeneralLedgerAccountCode` `RuleFilter` | `RuleId` must be unique within the cleardown module. `GeneralLedgerAccountCode` must be a general ledger account registered with the parent chart of accounts (CoA). The syntax of and allowed values for a `RuleFilter` expression are [documented here](/v1/docs/how-do-i-add-a-cleardown-module-with-cleardown-rules-to-a-chart-of-accounts#syntax-of-a-cleardown-rule). Note each `'` character encapsulating a string value must be escaped by an additional `'` character in Luminesce, so for example `select 'HoldType eq ''P''' as RuleFilter`. `RulePriority` defaults to 0.00 but if your cleardown module has more than one cleardown rule you should specify a different positive whole number for each. Cleardown rules with lower numbers take precedence and are evaluated earlier. |

To list all available fields, their data types, whether fields are considered 'main', and an explanation for each, run the following query using a [suitable tool](/v1/docs/what-tools-are-available-to-write-luminesce-queries):

```sql
select FieldName, DataType, IsMain, IsPrimaryKey, SampleValues, Description from Sys.Field where TableName = 'Lusid.CleardownModule.Rule.Writer' and FieldType = 'Column';
```

## Write errors

We recommend examining the results of every write query using one or more of the `WriteError`, `WriteErrorCode` and `WriteErrorDetail` fields.

For each record in the table of data to write, `Lusid.CleardownModule.Rule.Writer` returns an error code. If the operation is successful, the error code is `0`. If unsuccessful, a positive error code and explanation help you discover why LUSID considers the operation invalid.

## Examples

> **Note:** For more example Luminesce SQL queries, visit our [Github repo](https://github.com/finbourne/luminesce-examples/tree/master/examples/lusid/abor).

### Example 1: Add a single cleardown rule

Any existing rules in the cleardown module are replaced.

```sql
@data_to_write = select 'DailyNAV' as CleardownModuleCode, 'Abor' as ChartOfAccountsScope, 'Standard' as ChartOfAccountsCode,
'rule_01' as RuleId, '1-Investments' as AccountCode, 'Account.Code startswith ''200''' as RuleFilter;

select * from Lusid.CleardownModule.Rule.Writer where ToWrite = @data_to_write;
```

### Example 2: Add multiple cleardown rules in a particular order

The `RulePriority` field determines the order in which cleardown rules are evaluated; LUSID uses the first matching cleardown rule found. Lower numbers take precedence and are evaluated earlier.

```sql
@cleardown_rules = values
('rule_01', '1-Investments', 'Account.Code startswith ''200''', 1),
('rule_02', '2-Cash', 'Account.Type in ''Income'',''Expense'',''Revenue''', 2),
('rule_03', '3-Subscriptions', 'Properties[Account/MyScope/Cleardown] exists and Properties[Abor/MyScope/Region] eq ''EMEA''', 3)
;
@data_to_write = select 'DailyNAV' as CleardownModuleCode, 'Abor' as ChartOfAccountsScope, 'Standard' as ChartOfAccountsCode,
column1 as RuleId, column2 as AccountCode, column3 as RuleFilter, column4 as RulePriority from @cleardown_rules;

select * from Lusid.CleardownModule.Rule.Writer where ToWrite = @data_to_write;
```
