I am facing a problem with crm forms as when i change the field "imc_totalweight" value from 50 to any other value the calculation of freight is done and after saving the calculations -the number changes back to 50 but the freight calulations are accurate according to the another inserted values.
here is my javascript code:
function launchQuoteDetailsReport() {
var rdlName = "QuoteSalesDetailsParentReport.rdl";
var reportGuid = "828227c6-c644-e211-8f68-000c295e2404";
var entityGuid = Xrm.Page.data.entity.getId();
var entityType = "1084";
var serverUrl = Xrm.Page.context.getClientUrl();
var organizationName = Xrm.Page.context.getOrgUniqueName();
var link = serverUrl + "/crmreports/viewer/viewer.aspx?action=run&context=records&helpID=" + rdlName + "&id={" + reportGuid + "}&records=" + entityGuid + "&recordstype=" + entityType;
openStdDlg(link, null, 800, 600, true, false, null);
}
function Form_onload() {
if (Xrm.Page.ui.getFormType() == 1) {
Xrm.Page.getAttribute("effectivefrom").setValue(new Date);
Xrm.Page.getAttribute("effectivefrom").fireOnChange();
}
Xrm.Page.ui.tabs.get(4).setVisible(false);
if (Xrm.Page.ui.getFormType() == 1 || Xrm.Page.ui.getFormType() == 2) {
var mydate = new Date;
Xrm.Page.getAttribute("imc_timestamp").setValue(mydate);
}
}
function Form_onsave() {
if (Xrm.Page.ui.getFormType() == 1 || Xrm.Page.ui.getFormType() == 2) {
Xrm.Page.getAttribute("imc_calculatefreight").setSubmitMode("always");
Xrm.Page.getAttribute("imc_timestamp").setSubmitMode("always");
}
}
function imc_calculatefreight_onchange() {
var calcFreight = Xrm.Page.getAttribute("imc_calculatefreight").getValue();
if (calcFreight == true) {
var URL = "http://www.example.com/";
var zipcode = Xrm.Page.getAttribute("shipto_postalcode").getValue();
var weight = Xrm.Page.getAttribute("imc_totalweight").getValue();
var origin = "63044";
var p = Xrm.Page.getAttribute("imc_shippingfrom").getValue();
if (p == "3") {
origin = "62035";
}
if (p == "2") {
origin = "44146";
}
if (p == "4") {
origin = "92553";
}
if (p == "5") {
origin = "21230";
}
if (p == "100000000") {
origin = "32060";
}
if (p == "100000001") {
origin = "84401";
}
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(URL + "&origin=" + origin + "&dest=" + zipcode + "&weight1=" + weight);
var error = xmlDoc.getElementsByTagName("error").item(0).text;
var strfreight = xmlDoc.getElementsByTagName("netcharges").item(0).text;
var intdays = xmlDoc.getElementsByTagName("days").item(0).text;
if (error != "") {
alert("Error ocurred retrieving freight cost:\n\n" + error);
Xrm.Page.getAttribute("freightamount").setValue(null);
Xrm.Page.getAttribute("imc_days").setValue(null);
} else {
if (freight != "") {
var freight = strfreight.replace(/\$|,/g, "");
freight = freight * 1.2 + 70.00;
Xrm.Page.getAttribute("freightamount").setValue(parseFloat(freight));
Xrm.Page.getAttribute("imc_days").setValue(parseInt(intdays));
}
}
}
}
function effectivefrom_onchange() {
var start_date = Xrm.Page.getAttribute("effectivefrom").getValue();
var end_date = Xrm.Page.getAttribute("effectiveto");
if (start_date != null && start_date != "") {
var sd_yy = start_date.getYear();
var sd_mm = start_date.getMonth();
var sd_dd = start_date.getDate();
var new_date = start_date.setMonth(start_date.getMonth() + 1);
end_date.setValue(new_date);
}
}
function billto_telephone_onchange() {
var oPhone = Xrm.Page.getAttribute("billto_telephone");
var isError = "false";
var isExtension = "false";
var vPhone = oPhone.getValue();
var rString = /[\(\)\-\. ]/g;
vPhone = vPhone.replace(rString, "");
vPhone = vPhone.replace("ext", "x");
vPhone = vPhone.replace("EXT", "x");
vPhone = vPhone.substring(0, 1) == "1" ? vPhone.substring(1, vPhone.length) : vPhone;
if (vPhone.length < 10) {
isError = "true";
} else {
var vPhone1 = vPhone.substr(0, 3);
var vPhone2 = vPhone.substr(3, 3);
var vPhone3 = vPhone.substr(6);
if (vPhone3.indexOf("x") > 0) {
isExtension = "true";
var vPhone4 = vPhone3.substr(vPhone3.indexOf("x") + 1);
vPhone3 = vPhone3.substr(0, vPhone3.indexOf("x"));
}
}
if (isError == "true" || isNaN(Math.abs(vPhone1)) || isNaN(Math.abs(vPhone2)) || (isNaN(Math.abs(vPhone3)) || vPhone3.length != 4) || isExtension == "true" && (isNaN(Math.abs(vPhone4)) || vPhone4.length > 5)) {
alert("The Phone number entered is not a valid phone number, the phone number must be 10 digits." + "\n\nThe valid format for phone numbers is (234) 567-8901 or with an extension" + "\nthe valid format is (234) 567-8901 x1234." + "\n\nExamples of valid phone numbers, which will get automatically formatted, are:" + "\n\t2345678901 \t2345678901 x1234" + "\n\t234.567.8901 \t234.567.8901 ext 1234" + "\n\t(234)567-8901\t(234)567-8901 EXT1234" + "\n\nPlease reenter the phone number in one of the above formats.\n\n");
oPhone.SetFocus();
event.returnValue = false;
return;
}
oPhone.setValue("(" + vPhone1 + ") " + vPhone2 + "-" + vPhone3 + (isExtension == "true" ? " x" + vPhone4 : ""));
}
function billto_fax_onchange() {
var oPhone = Xrm.Page.getAttribute("billto_fax");
var isError = "false";
var isExtension = "false";
var vPhone = oPhone.getValue();
var rString = /[\(\)\-\. ]/g;
vPhone = vPhone.replace(rString, "");
vPhone = vPhone.replace("ext", "x");
vPhone = vPhone.replace("EXT", "x");
vPhone = vPhone.substring(0, 1) == "1" ? vPhone.substring(1, vPhone.length) : vPhone;
if (vPhone.length < 10) {
isError = "true";
} else {
var vPhone1 = vPhone.substr(0, 3);
var vPhone2 = vPhone.substr(3, 3);
var vPhone3 = vPhone.substr(6);
if (vPhone3.indexOf("x") > 0) {
isExtension = "true";
var vPhone4 = vPhone3.substr(vPhone3.indexOf("x") + 1);
vPhone3 = vPhone3.substr(0, vPhone3.indexOf("x"));
}
}
if (isError == "true" || isNaN(Math.abs(vPhone1)) || isNaN(Math.abs(vPhone2)) || (isNaN(Math.abs(vPhone3)) || vPhone3.length != 4) || isExtension == "true" && (isNaN(Math.abs(vPhone4)) || vPhone4.length > 5)) {
alert("The Fax number entered is not a valid phone number, the phone number must be 10 digits." + "\n\nThe valid format for phone numbers is (234) 567-8901 or with an extension" + "\nthe valid format is (234) 567-8901 x1234." + "\n\nExamples of valid phone numbers, which will get automatically formatted, are:" + "\n\t2345678901 \t2345678901 x1234" + "\n\t234.567.8901 \t234.567.8901 ext 1234" + "\n\t(234)567-8901\t(234)567-8901 EXT1234" + "\n\nPlease reenter the phone number in one of the above formats.\n\n");
oPhone.SetFocus();
event.returnValue = false;
return;
}
oPhone.setValue("(" + vPhone1 + ") " + vPhone2 + "-" + vPhone3 + (isExtension == "true" ? " x" + vPhone4 : ""));
}
function shipto_telephone_onchange() {
var oPhone = Xrm.Page.getAttribute("shipto_telephone");
var isError = "false";
var isExtension = "false";
var vPhone = oPhone.getValue();
var rString = /[\(\)\-\. ]/g;
vPhone = vPhone.replace(rString, "");
vPhone = vPhone.replace("ext", "x");
vPhone = vPhone.replace("EXT", "x");
vPhone = vPhone.substring(0, 1) == "1" ? vPhone.substring(1, vPhone.length) : vPhone;
if (vPhone.length < 10) {
isError = "true";
} else {
var vPhone1 = vPhone.substr(0, 3);
var vPhone2 = vPhone.substr(3, 3);
var vPhone3 = vPhone.substr(6);
if (vPhone3.indexOf("x") > 0) {
isExtension = "true";
var vPhone4 = vPhone3.substr(vPhone3.indexOf("x") + 1);
vPhone3 = vPhone3.substr(0, vPhone3.indexOf("x"));
}
}
if (isError == "true" || isNaN(Math.abs(vPhone1)) || isNaN(Math.abs(vPhone2)) || (isNaN(Math.abs(vPhone3)) || vPhone3.length != 4) || isExtension == "true" && (isNaN(Math.abs(vPhone4)) || vPhone4.length > 5)) {
alert("The Phone number entered is not a valid phone number, the phone number must be 10 digits." + "\n\nThe valid format for phone numbers is (234) 567-8901 or with an extension" + "\nthe valid format is (234) 567-8901 x1234." + "\n\nExamples of valid phone numbers, which will get automatically formatted, are:" + "\n\t2345678901 \t2345678901 x1234" + "\n\t234.567.8901 \t234.567.8901 ext 1234" + "\n\t(234)567-8901\t(234)567-8901 EXT1234" + "\n\nPlease reenter the phone number in one of the above formats.\n\n");
oPhone.SetFocus();
event.returnValue = false;
return;
}
oPhone.setValue("(" + vPhone1 + ") " + vPhone2 + "-" + vPhone3 + (isExtension == "true" ? " x" + vPhone4 : ""));
}
function shipto_fax_onchange() {
var oPhone = Xrm.Page.getAttribute("shipto_fax");
var isError = "false";
var isExtension = "false";
var vPhone = oPhone.getValue();
var rString = /[\(\)\-\. ]/g;
vPhone = vPhone.replace(rString, "");
vPhone = vPhone.replace("ext", "x");
vPhone = vPhone.replace("EXT", "x");
vPhone = vPhone.substring(0, 1) == "1" ? vPhone.substring(1, vPhone.length) : vPhone;
if (vPhone.length < 10) {
isError = "true";
} else {
var vPhone1 = vPhone.substr(0, 3);
var vPhone2 = vPhone.substr(3, 3);
var vPhone3 = vPhone.substr(6);
if (vPhone3.indexOf("x") > 0) {
isExtension = "true";
var vPhone4 = vPhone3.substr(vPhone3.indexOf("x") + 1);
vPhone3 = vPhone3.substr(0, vPhone3.indexOf("x"));
}
}
if (isError == "true" || isNaN(Math.abs(vPhone1)) || isNaN(Math.abs(vPhone2)) || (isNaN(Math.abs(vPhone3)) || vPhone3.length != 4) || isExtension == "true" && (isNaN(Math.abs(vPhone4)) || vPhone4.length > 5)) {
alert("The Fax number entered is not a valid phone number, the phone number must be 10 digits." + "\n\nThe valid format for phone numbers is (234) 567-8901 or with an extension" + "\nthe valid format is (234) 567-8901 x1234." + "\n\nExamples of valid phone numbers, which will get automatically formatted, are:" + "\n\t2345678901 \t2345678901 x1234" + "\n\t234.567.8901 \t234.567.8901 ext 1234" + "\n\t(234)567-8901\t(234)567-8901 EXT1234" + "\n\nPlease reenter the phone number in one of the above formats.\n\n");
oPhone.SetFocus();
event.returnValue = false;
return;
}
oPhone.setValue("(" + vPhone1 + ") " + vPhone2 + "-" + vPhone3 + (isExtension == "true" ? " x" + vPhone4 : ""));
}
;