> For the complete documentation index, see [llms.txt](https://learn.capstorm.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.capstorm.com/cs-govern/frequently-asked-questions/can-i-add-a-new-masking-rule.md).

# Can I Add a New Masking Rule?

![](https://869259762-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyE2QoOSmKpot88Q3kHGR%2Fuploads%2Fgit-blob-3236c0be7548c6fa1a53dd74f8584140f5e6ccbb%2F5fcf6d03-Govern-Banner.png?alt=media)

## 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.

![](https://869259762-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyE2QoOSmKpot88Q3kHGR%2Fuploads%2Fgit-blob-da863de96b49e8df89152342699feabf575ab3ad%2F189e4c25-CustomMaskingRule.png?alt=media)
