function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc dev 2264sfdc dev 2264 

masking a credit card number from description

Hi,

I want to mask a credit card number form case description if the case is having a credt card number to it , Please let me know the code to acheive this .

Thanks in advance
NagendraNagendra (Salesforce Developers) 
Hi,

Please find the sample code below which should help.

Trigger:
trigger testMaskCC on Case(before insert){
 if(trigger.isBefore && trigger.isInsert){ 
        CreditCard_No_Masking_OnCase NewCase= new CreditCard_No_Masking_OnCase();
        NewCase.newCaseInsert(Trigger.new);
    }      
}
Trigger Class:
public class CreditCard_No_Masking_OnCase{

    public void newCaseInsert(list<Case> newList){
    
    String reGexVisa='4[0-9]{12}(?:[0-9]{3})';
    Pattern regexPattern = Pattern.compile(reGexVisa);
        for(case cs : newList){
           String withOutSpec=(cs.description).replaceAll('[-, +]','');
           Matcher regexMatcher = regexPattern.matcher(withOutSpec);
            system.debug('-------------group count----'+regexMatcher.groupCount());
           while(regexMatcher.find()){
              Integer countGroup=0;
              String  matchValue=regexMatcher.group(countGroup);
              cs.description= String.valueOf(cs.description).replaceAll('[0-9]', 'X');
              countGroup++;
            }
        }
    }
}
Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra