CRM 2013 – JavaScript – Unsaved Message due to a jQuery masking Plug-In

CRM Unsaved Message due to a JQuery masking plugin

We have a requirement to have telephone number displayed with the carrier code. It can be implemented by using a JQuery masking plugin. The details can be found in this blog: http://slalomdotcom.wordpress.com/2011/06/03/crm-field-masking-with-jquery/

However the record status will be changed to ‘unsaved changes’ after page loading. CRM pops up message ‘Your changes have not been saved. To stay on the page so that you can save your changes, click Cancel’ when user leave page without any changes. This is very frustrating and it can’t be resolved directly by setting Xrm.Page.data.setFormDirty(false)

crmunsaved

Finally we decided to use JS to Mask the telephone fields instead of JQuery plugin. Following is the sample code:

On Form Load:

Function formatTel()

{

var sTmp = Xrm.Page.getAttribute(“telephone1”).getValue();

sTmp = sTmp.replace(/[^0-9]/g, “”);

switch (sTmp.length) {

case 10:

sTmp = “(” + sTmp.substr(0, 3) + “) ” + sTmp.substr(3, 3) + “-” + sTmp.substr(6, 4);

break;

default:

sTmp = “(” + sTmp.substr(0, 3) + “) ” + sTmp.substr(3, 3) + “-” + sTmp.substr(6, 4) + ” x” + sTmp.substr(10, sTmp.length – 10)

break;

}

Xrm.Page.getAttribute(“telephone1”).setValue(sTmp);

Xrm.Page.getAttribute(“telephone1”).setSubmitMode(“never”);

}

//On Field change reset submit mode to always:

function changeSubmitMode(mode) {

// mode = “always”

if (Xrm.Page.getAttribute(“telephone1”).getSubmitMode() != mode ){

Xrm.Page.getAttribute(“telephone1”).setSubmitMode(mode);

}

}

 

 

// Remove phone masking before saving back to CRM

function removePhoneMasking(text) {

text = text.replace(“(“, “”);

text = text.replace(“)”, “”);

text = text.replace(“-“, “”);

text = text.replace(/ /g, “”);

return text;

}


 

I hope you find it useful!

Author: Zhe Chen
Title: Lead Dynamics CRM Consultant @ Adisys
Email: zhechen@adisys.co