hi i have written a simple java script function which which will mask all credit card nos digits except the last four digits for agent login and for any other login(admin/manager) it will display the credit card
digits(12) as it is or without masking , i am facing two issues here one the it is masking for everyone irrespective of agent or manager login, another the masking is such that the last four digits shows at front and the remaining is shown a complete blank space , for better understanding i am providing both code and screenshot of my implementation.
// JavaScript source code
function RetrieveLoggedInD365UserSecurityRoles(executionContext) {
var roleName = "Agent";
//will retrieve the credit Card Number from the form
var formContext = executionContext.getFormContext();
var creditCardNum = formContext.getAttribute("cts_creditcardnumber").getValue();
var duplicatecreditCardNum = creditCardNum;
//
//to retrive the global user context from the d365 security profile
var globalContext = Xrm.Utility.getGlobalContext();
var userRoles = globalContext.userSettings.roles;
//using regex it will mask the digits of credit card Numbers
userRoles.forEach(function hasRoleName(item) {
if (item.name == roleName) {
alert("i m in!!")
var replacedCreditCardNum = creditCardNum.replace(/\d(?=\d{4})/g, "x");
formContext.getAttribute("cts_creditcardnumber").setValue(replacedCreditCardNum);
};
if (item.name != rolename) {
formContext.getAttribute("cts_creditcardnumber").setValue(duplicatecreditCardNum);
};
}
);
}

it should have shown as "xxxxx-xxxx-0123" and its masking for all
Thank you for helping me out, i tried certain related community post for my implementation and yet i am not getting the desired result . thank you for your time to consider this.