I'm looking to basically redact some information in our Salesforce environment, across various fields and using different REGEX patterns.

For example, in Case.Description, I have the following details:

CMP 40 Casual + Samsung A5 (Stock) 

username: jyi
Email: [email protected]
Password: Kjr

username: 4nudf
Email: [email protected]
Password: Kjr2438

* Need setup

And the outcome I am hoping to get to is:

CMP 40 Casual + Samsung A5 (Stock) 

*Information redacted*

*Information redacted*

* Need setup

I've had a look at a similar question previously posted which has suggested using Apex instead of Flows to achieve this due to the Boolean nature of the REGEX function in Flows.

As I lack developer experience, I'm struggling to understand Apex basics to formulate a query.

Could someone help breakdown the steps required to perform the desired operation please? This is what I've gotten to so far, but am still stuck with multiple error messages:

  1. create new public apex class (to save for future reference) Checking that this is using Apex class and not Apex trigger (as per this article). The data already exists in saved records.
  2. how do I work with standard objects to access Case.Description field? Do I need to create a duplicated variable that is only visible in Apex?
  3. I want to be able to use the pattern.compile function to name various REGEX patterns and run them systematically.
  4. To use system.debug to first find the records to check the REGEX is correct, count the number of records that qualify and create a variable to quarantine those records.
  5. Run a secondary query to replace values (i.e. username, email, password) with new value (i.e. information redacted)

Thank you in advance for your help!!

  • 1
    Are you trying to achieve this? You don't want to show the username and password to those who are looking at the Description field?
    – prem22
    Commented Jul 7, 2023 at 6:41
  • hi @prem22, yes thats correct. i am hoping to replace the username and password with a generic note in the Description field.
    – Nat
    Commented Jul 10, 2023 at 3:05

1 Answer 1


Hi depending on the number of Cases you might have to create a batch or execute it through the anonymous window.

This is a high level snippet of a code that might help you out:

List<Case> cases = new List<Case>([SELECT Description FROM Case where <YOUR_FILTERS>]);
        List<Case> updatedCases = new List<Case>();
        for (Case c : Cases) {
            String longText = c.Description;
            List<String> lstRows = longText.split('\n');
            List<String> wordsToWatchOutFor = new List<String>{ 'UserName', 'password', 'email' };
            String toUpdate;
            for (String x : wordsToWatchOutFor) {
                for (String y : lstRows) {
                    if (y.contains(x)) {
                        toUpdate = toUpdate + '*redact*'+ '/n';
                    } else {
                        toUpdate = toUpdate + '/n' + y;

            c.Description = toUpdate;
        update updatedCases;

  • thanks NedlaV! ill give that a go to start with.
    – Nat
    Commented Jul 16, 2023 at 22:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .