Copado‎ > ‎

Global Find and Replace Rules

The Global Find and Replace Rules feature allows you to define custom find and replace rules that Copado applies whenever you commit files and deploy files Org-to-Org or Git-to-Org.

The rules must be written in a .YML file and then uploaded to Copado, as an attachment, to an Environment record and/or a Deployment Flow record. If the Copado.yml file is uploaded in a Deployment Flow, the rules will apply to any deployments and commits of the Environments and the branches of the Deployment Flow. If the Copado.yml file is uploaded to an Environment, the rules will apply to Metadata/Git Metadata deployments.

Note that if your Salesforce instance doesn't have Attachments, and instead is using Files, you can still upload the attachment using this URL: https://{instance}.salesforce.com/p/attach/NoteAttach?pid={parentId}

A common use case for this feature is to remove offending XML tags that will make a file not deployable. Like for example:
  • remove all ManageSandbox user permissions from a profile, since this user permission only exists on production environments and will fail when trying to be deployed to sandboxes.
  • remove FLS of deprecated fields, etc.


You can test the regular expressions here and validate your file before uploading it from here.

The .YML file structure must have two sections: regex_lib and rules. These sections are configured as follows:

regex_lib

The "regex_lib" section contains the 'find' regex expressions. These regex expressions are saved in a variable name so that they can be referenced in the rules section. 

In the following example, the 'find' regex expression for a field is assigned to a variable name called "field":

field: '(?s)(.[^\n]+<fields>(?:(?!<fields>).)*?<fullName>___REPLACEVALUE___</fullName>(?:(?!<fields>).)*?</fields>)'


rules

The "rules" section contains one or more rules to be executed. A rule consists of a rule name and several rule parameters. The rule name cannot have spaces or special characters and the rule parameters are as follows:
  • file_names
    • A list of the exact names of the metadata files where the find and replace will be executed (e.g. "Account.object", "MyCustomLayout.layout", etc.)
    • The rule will execute once for every file name listed
    • Leaving this parameter empty means the rule applies to all files
  • extensions
    • A list of the metadata file extensions where the find and replace will be executed (e.g. "object", "layout", etc.)
    • The rule will execute once for every file that has one of the extensions listed
    • Leaving this parameter empty means the rule applies to all extensions
  • regex_name
    • The variable name of the regex expression defined in the regex_lib section
  • replace_values
    • A list of values to replace in the regex expression (___REPLACEVALUE___)
  • replace_with
    • The value that will replace the text that is found by the regex expression
    • Leaving this parameter blank means the matching regex will be deleted
  • branches
    • The list of Git branches where the rule applies. If left blank, the rule applies to all Git branches.
  • exclusion_branches
    • (As of Copado v8.5) The list of Git branches where the rule does not apply. This parameter overrides the branches parameter in case both are included in the .YML file. If left blank, the rule applies to all Git branches.

In the following example, the "invalid_fields" rule will be applied to the Account and Contact object. The rule's regex expression is "field" and the values to be replaced in the regex expression are "History__c" and "History_Date__c". The value to replace with is blank since the purpose of the rule is to delete the fields. If using the Git Integration, the branches where the rule applies are dev1, uat and master.

    invalid_fields: #this is the rule name
file_names:
- 'Account.object'
- 'Contact.object'
regex_name: 'field'
replace_values:
- History__c
- History_Date__c
branches:
- dev1
- uat
- master
replace_with:
Comments