# Can I Add a New Masking Rule?

![](/files/bV66cgZUHAhsDBXbIqYV)

## How to Create A New Masking Rule

CS:Govern Masking Rules are implemented as custom database functions and are persisted in the *GovernMaskingRule* table in a CopyStorm database.

The procedure to create a new Masking Rule is:

* Review the code of an existing Masking Rule in your database.
* Write and test SQL code for a new Masking Rule.
* Insert a new record into the CS:Govern Masking Rule control table.

## Example: Create Reverse Masking Rule

In this example, create a new masking rule which masks data by reversing its characters (e.g. ABC become CBA).

The follow code for SQL/Server was developed by looking at an existing masking rule.

```
CREATE OR ALTER  FUNCTION [dbo].[guard_mask_to_reverse]( @original NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX)
AS
BEGIN
    IF @original IS NULL RETURN NULL;
    IF @original = '' RETURN '';
    RETURN REVERSE(@original)
END;
```

After installing the new function into the database, the next step is to teach CS:Govern about the new Masking Rule.

```
BEGIN
	DECLARE @id INT;
	DECLARE @templateRuleId INT;
	DECLARE @now DATETIME;

--
-- Insert a top level masking rule.
--
	INSERT INTO GuardianMaskingRule(name,functionName,isActive,createdDate,modifiedDate)
		VALUES( 'Reverse', 'guard_mask_to_reverse([[VALUE]])', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
	;

--
-- Pick an existing rule which works on the same field types as the new rule.
-- Find the unique id assigned to the new rule.
--
	SELECT @templateRuleId=id FROM GuardianMaskingRule WHERE name='Asterisks';
	SELECT @id=id, @now=createdDate FROM GuardianMaskingRule WHERE name='Reverse';

--
-- Register the type of Salesforce fields supported by the masking rule.
--
	INSERT INTO GuardianMaskingRuleFieldType(fieldTypeName, maskingRuleId
		, createdDate, modifiedDate)
		SELECT fieldTypeName, @id, @now, @now 
          FROM GuardianMaskingRuleFieldType
          WHERE maskingRuleId = @templateRuleId;

END;
```

Restart the CS:Govern GUI and the new *Reverse* masking rule will be available.

![](/files/K8FMUFeaE4qnOuQrpU9m)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.capstorm.com/cs-govern/frequently-asked-questions/can-i-add-a-new-masking-rule.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
