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

How to mask credit card number using apex

Hi All,

I want to mask credit card number in Case Description field. Whenever case get created in sfdc if case description field contains any credit card number, I just wanted to mask credit card number. For example if anyone enter credit card number like 4346-7810-4356-6678 in case description, After saving the case Credit card number should be look like as xxxx-xxxx-xxxx-xxxx. I have also tried to write trigger on Case object. But it is not working properly. It is making all number as cross(x) in case description field. Please find below code:
trigger testMaskCC on Case(before insert){
 if(trigger.isBefore && trigger.isInsert){ 
        CreditCard_No_Masking_OnCase NewCase= new CreditCard_No_Masking_OnCase();

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());
              Integer countGroup=0;
              cs.description= String.valueOf(cs.description).replaceAll('[0-9]', 'X');

And also I am using regex for idetifying different companies's credit card number like Visa Card: ^4[0-9]{12}(?:[0-9]{3})?$  Mastercard: ^5[1-5][0-9]{14}$.

Please help me to generalized Regex(I wanted to use one Regex to identify different comany's credit card instead of using different company's regex) and Mask the Credit Card number only.
Any Help in this regard would be much appriciated.

Sean FieldingSean Fielding
We built an application for this
Matt Parker 1Matt Parker 1
We built a (slightly cheaper) application for this called PCIFY:

It has a 14-day free trial so give it a shot and see if it works for you
Sean FieldingSean Fielding
For anyone who doesn't want to worry about custom triggers & REGEX, we recently released DataMask 2.2, which also includes Einstein Bot support and advanced masking logic (including look behind inclusions).  Check us out at:
Hi Pammy, Did you find solution for this issue, please let me know.

creditonelogin creditonelogincreditonelogin creditonelogin
Much like their Android using opposite numbers, those with iOS devices can without difficulty revel in a exceptional mobile banking revel in through the use of the TIAA-CREF software.  credit one credit card This is available underneath the App Store if you look for “TIAA” under the quest tab. You can quickly down load this unfastened app and directly log in for a complete mobile banking enjoy. This makes it clean that allows you to experience all of the benefits of the internet site in a easy and organized app that securely logs you in.