Please find the code below:
"setFormatAccount" Method is invoking on form save event
var fieldValue ="";
function setFormatAccount(executionContext) {
var eventArgs = executionContext.getEventArgs();
var isSaved = false;
if (eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 1) {
isSaved = setFormat("telephone1");
if (isSaved) {
//Set the value to "telephone1" field after save completes
Xrm.Page.getAttribute("telephone1").setValue(fieldValue);
}
}
}
function setFormat(field) {
fieldValue = Xrm.Page.getAttribute(field).getValue();
if (fieldValue != null) {
var countrycode2 = fieldValue;
if (fieldValue.startsWith('+')) {
var countrycode1 = fieldValue.substr(0, fieldValue.indexOf(' '));
if (countrycode1.length > 0)
countrycode2 = fieldValue.substr(countrycode1.length + 1);
else
countrycode2 = countrycode1;
if (countrycode2.startsWith('(')) {
countrycode2 = countrycode2.substr(countrycode2.indexOf(')') + 1);
}
}
countrycode2 = countrycode2.replace('(', '');
countrycode2 = countrycode2.replace(')', '');
countrycode2 = countrycode2.replace('-', '');
countrycode2 = countrycode2.replace('x', '');
countrycode2 = countrycode2.replace(/_/g, '');
countrycode2 = countrycode2.replace(/\s/g, '');
Xrm.Page.getAttribute(field).setValue(countrycode2);
return true;
}
}