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
krishna casukhela 7krishna casukhela 7 

How to remove asterisk from emailaddress

Hello
I have a piece of code where as soon as the visual force page loads I mask the email id .
for ex: if emailID is jose@gmail.com, it is masked as  j***@g****.com

But before storing the field value in sobject, I need to unmask it to jose@gmail.com

any help will be greatly appreciated.
private String maskEmail (String sEmail){

         String sMaskedEmail;

         String[] aEmail = sEmail.split('@');
         if(aEmail.size() == 2){

             sMaskedEmail = aEmail[0].left(1);

             for(integer i=0; i < aEmail[0].length() - 1; i++){

                 sMaskedEmail += '*';

             }
              

             String[] aEmail2 = aEmail[1].split('\\.', 2);

             sMaskedEmail += '@' + aEmail2[0].left(1);

             for(integer i=0; i < aEmail2[0].length() - 1; i++){

                 sMaskedEmail += '*';

             }

             sMaskedEmail += '.' + aEmail2[1];
         }

         return sMaskedEmail;  

    }

thanks
krishna
 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Krishna,
There are many ways to do it and may use whichever is best applicable in your case.
- If your code still has access to the original value (variable), you may use that variable 
- Can use hidden text field where you may store the original value before masking it and you can use that hidden field data while saving the data in sObject. 
- If it is in list, you may use Map where Id can be the unmasked email id (Assume it is unique) and value can be the masked email id. 

Hope this helps!
Please mark it as Solved it you find this helpful.
 
krishna casukhela 7krishna casukhela 7
Hi Maini
when the constrcutor is called I write an soql and will capture the unmasked email address value into an string variable (say fan_email)

if the user doesn't make any changes and its SAVE button then I can send value in fan_email to sobject field. this part is fine.

I have like this: public String FanEmailMasked{get;set;}
                       public String FanEmailUnmasked{get;set;}

<apex:inputText value="{!FanEmailMasked}"/>

Now if the user has changed his email then this value will be available in the FanEmailMasked.
Now how do I determine if there is a change in the email address because while saving to object I am saving like this

sobject.email__c=fanEmailUnmasked
Upsert sobject;

so if there is a change in email then how to pass to sobject field.

I hope I am clear.

thanks
krishna
 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Krishna,
Can you please clarify this.

"Now if the user has changed his email then this value will be available in the FanEmailMasked." - Here when the user edits the data, is that still  masked


 
krishna casukhela 7krishna casukhela 7
Yes Maini
If the user has changed his email then this value is available in the FanEmailMasked.
so if I pass this value to sobject it works.

when page loads it will fetch the values, so email is masked. Now if I try to save thin it throws error.

Here's where I am having a small confusion.

thanks
krishna
krishna casukhela 7krishna casukhela 7
Hi Maini
if the user has changed his email then this value is available in the FanEmailMasked, so it means the new email is not masked ,so
a successful save occurs to the sobject.

The prblm comes when the user hasn't made any changes to the fields and directly hits save button.

thanks
krishna
Malni Chandrasekaran 2Malni Chandrasekaran 2
Krishna,
Just to confirm my understanding,
Lets say initial email id - abc@gmail.com
while loading you are masking it to a**@g****.com

now, 
FanEmailMasked is - a**@g****.com
FanEmailUnmasked is - abc@gmail.com

Now if the user edits, abc@gmail.com to xyz@gmail.com
FanEmailMasked is xyz@gmail.com
FanEmailMasked is abc@gmail.com

And as per your current coding saves abc@gmail.com

if my understanding is correct, Pls try the logic given below,

if (FanEmailMasked.equals(maskEmail(FanEmailMasked)) // Invoke the method to mask the value in FanEmailMasked and compare
{                                                                                             
sobject.email__c=fanEmailUnmasked

}
else
{
sobject.email__c=fanEmailmasked

}
Upsert sobject;


Eg to show how it works,
Scenario :1 User has not edited the email id ; FanEmailMasked is - a**@g****.com and   FanEmailUnmasked is - abc@gmail.com
        
if (a**@g****.com equals a**@g****.com) //which is true
hence it executes , sobject.email__c=fanEmailUnmasked  // abc@gmail.com is saved

Scenario :2 User has edited the email id ; FanEmailMasked is - xyz@gmail.com and   FanEmailUnmasked is - abc@gmail.com

if (xyz@gmail.com equals x**@g****.com) // Condition fails and goes to else part

sobject.email__c=fanEmailmasked // xyz@gmail.com is saved.


Hope this helps.

Please mark it solved if this helps solving your problem
Malni Chandrasekaran 2Malni Chandrasekaran 2
You may also set some flag (boolean variable like emailIdChanged) to true on Input field's (Email Id field) 'OnChange' event.

if (emailIdChanged)
{                                                                                             
sobject.email__c=fanEmailmasked
}
else
{
sobject.email__c=fanEmailUnmasked
}
Upsert sobject;
 
krishna casukhela 7krishna casukhela 7
Hi
if I give like this  FanEmailMasked.equals(maskEmail(FanEmailMasked))   it worked.
Now in constructor I am calling the maskEmail method and also the same method being called in save changes button.

Is there a way I can avoid this so call to method is only once?

thanks
krishna
 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Krishna,
I am not very sure abt the logic behind the maskEmail method call in save changes button.

Please try if it helps,
 - define a flag (boolean) variable, something like, toCallMask and by default set to false.

if (FanEmailMasked.equals(maskEmail(FanEmailMasked)) 
{                                                                                             
sobject.email__c=fanEmailUnmasked;
toCallMask = true;

}
else
{
sobject.email__c=fanEmailmasked
toCallMask = false;
}
Upsert sobject;

In your Save changes method, before calling maskEmail method you can check for the flag variable

if (toCallMask)
{
Call the maskEmail method.

}


                                                                  (OR)


You may also try as I have given in previous reply. In the below option, you dont have to call maskEmail method to check if the email id is changed.
You may also set some flag (boolean variable like emailIdChanged) to true on Input field's (Email Id field) 'OnChange' event.

if (emailIdChanged)
{                                                                                             
sobject.email__c=fanEmailmasked
}
else
{
sobject.email__c=fanEmailUnmasked
}
Upsert sobject;


Hope this helps.
Aaryan RanaAaryan Rana
Can anyone please unmask this email
r******5@gmail.com
It would be a great help