Code
//Get Reference Unit Price
function getRefUnitPrice(oproductId)
{
var aryRet=new Array(3);
var discountNodes=getDiscountNodes(oproductId);
if (discountNodes!=null)
{
var stdAmount;
var maxCnt=(discountNodes.length>2?2:discountNodes.length);
if (maxCnt>0)
{
//std unit price
aryRet[0]=new Object();
var lowquantity=discountNodes[0].selectSingleNode("./discounttypeid.lowquantity").text;
aryRet[0].caption=(parseInt(lowquantity)-1).toString()+"PCS";
aryRet[0].value=parseFloat(discountNodes[0].selectSingleNode("./amount").text);
stdAmount=parseFloat(aryRet[0].value);
aryRet[0].value=aryRet[0].value.toFixed(2);
setControl("esp_ref_perunitprice1",aryRet[0]);
}
for ( var i = 0; i < maxCnt; i++ )
{
var discount;
if (discountNodes[i].selectSingleNode("./discounttypeid.isamounttype").text=="1")
{
discount=discountNodes[i].selectSingleNode("./discounttypeid.amount").text;
aryRet[i+1]=new Object();
aryRet[i+1].caption=discountNodes[i].selectSingleNode("./discounttypeid.lowquantity").text;
aryRet[i+1].caption+="PCS~";
aryRet[i+1].caption+=discountNodes[i].selectSingleNode("./discounttypeid.highquantity").text;
aryRet[i+1].caption+="PCS"
aryRet[i+1].value=stdAmount-parseFloat(discount);
aryRet[i+1].value= aryRet[i+1].value.toFixed(2);
setControl("esp_ref_perunitprice"+(i+2).toString(),aryRet[i+1]);
}
else
{
aryRet[i+1]=new Object();
discount=discountNodes[i].selectSingleNode("./discounttypeid.percentage").text;
aryRet[i+1].caption=discountNodes[i].selectSingleNode("./discounttypeid.lowquantity").text;
aryRet[i+1].caption+="PCS~";
aryRet[i+1].caption+=discountNodes[i].selectSingleNode("./discounttypeid.highquantity").text;
aryRet[i+1].caption+="PCS";
aryRet[i+1].value=stdAmount* (1-parseFloat(discount)/100);
aryRet[i+1].value.toFixed(2);
setControl("esp_ref_perunitprice"+(i+2).toString(),aryRet[i+1]);
}
}
}
}
//Han Jianxing
//set control caption and text for Text Control
//fieldValue.value =Text
//fieldValue.caption=Display Name
function setControl(fieldName, fieldValue)
{
var fieldObjData = crmForm.all[fieldName];
var fieldObjTcControl = document.getElementById( fieldName + "_c" );
fieldObjData.innerText=fieldValue.value;
fieldObjTcControl.innerText=fieldValue.caption;
}
function getDiscountNodes(oproductId)
{
var fetchXml=
[
"<fetch mapping='logical'>",
"<entity name='productpricelevel'>",
"<attribute name='amount'/>",
"<attribute name='transactioncurrencyid'/>",
"<filter type='and'>",
"<condition attribute='pricelevelid' operator='eq' value='{9439FF4D-8613-DE11-B063-001EC9B6EAFD}'/>",
"<condition attribute='productid' operator='eq' value='"+oproductId+"'/>",
"</filter>",
"<link-entity name='discount' from='discounttypeid' to='discounttypeid' link-type='outer'>",
"<attribute name='amount'/>",
"<attribute name='highquantity'/>",
"<attribute name='isamounttype'/>",
"<attribute name='lowquantity'/>",
"<attribute name='percentage'/>",
"<attribute name='transactioncurrencyid'/>",
"<order attribute='lowquantity'/>",
"</link-entity>",
"</entity>",
"</fetch>"
].join("");
fetchXml=fetchXml.replace(/</g,'<')
fetchXml=fetchXml.replace(/>/g,'>')
var xml =
[
"<?xml version='1.0' encoding='utf-8'?>",
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" ",
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ",
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">",
GenerateAuthenticationHeader(),
"<soap:Body>",
" <Fetch xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" ,
" <fetchXml>",
fetchXml,
" </fetchXml>",
" </Fetch>",
"</soap:Body>",
"</soap:Envelope>"
].join("");
var resultXml = executeSoapRequest("Fetch",xml);
var node=resultXml.selectSingleNode("//error");
if (node!=null)
{
alert(node.xml);
}
else
{
var fetchResult= resultXml.selectSingleNode("//FetchResult");
if (fetchResult!=null)
{
fetchResult=fetchResult.text;
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(fetchResult);
return xmlDoc.selectNodes("//result");
}
}
return null;
}
// Helper method to execute a SOAP request
function executeSoapRequest(action, xml)
{
var actionUrl = "http://schemas.microsoft.com/crm/2007/WebServices/";
actionUrl += action;
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction",actionUrl);
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
return resultXml;
}
// Helper method to return a single node value from XML
function getNodeValue(tree, el)
{
var retVal = null;
var e = null;
e = tree.getElementsByTagName(el);
if (e != null && e[0] != null) {
retVal = e[0].firstChild.nodeValue;
}
return retVal;
}
// Helper method to return a multiple node value from XML
function getMultipleNodeValues(tree, el)
{
var retVal = new Array();
var e = null;
e = tree.getElementsByTagName(el);
for(i = 0; i < e.length; i++) {
retVal[i] = e[i].firstChild.nodeValue;
}
return retVal;
}
//Get Reference Unit Price
function getRefUnitPrice(oproductId)
{
var aryRet=new Array(3);
var discountNodes=getDiscountNodes(oproductId);
if (discountNodes!=null)
{
var stdAmount;
var maxCnt=(discountNodes.length>2?2:discountNodes.length);
if (maxCnt>0)
{
//std unit price
aryRet[0]=new Object();
var lowquantity=discountNodes[0].selectSingleNode("./discounttypeid.lowquantity").text;
aryRet[0].caption=(parseInt(lowquantity)-1).toString()+"PCS";
aryRet[0].value=parseFloat(discountNodes[0].selectSingleNode("./amount").text);
stdAmount=parseFloat(aryRet[0].value);
aryRet[0].value=aryRet[0].value.toFixed(2);
setControl("esp_ref_perunitprice1",aryRet[0]);
}
for ( var i = 0; i < maxCnt; i++ )
{
var discount;
if (discountNodes[i].selectSingleNode("./discounttypeid.isamounttype").text=="1")
{
discount=discountNodes[i].selectSingleNode("./discounttypeid.amount").text;
aryRet[i+1]=new Object();
aryRet[i+1].caption=discountNodes[i].selectSingleNode("./discounttypeid.lowquantity").text;
aryRet[i+1].caption+="PCS~";
aryRet[i+1].caption+=discountNodes[i].selectSingleNode("./discounttypeid.highquantity").text;
aryRet[i+1].caption+="PCS"
aryRet[i+1].value=stdAmount-parseFloat(discount);
aryRet[i+1].value= aryRet[i+1].value.toFixed(2);
setControl("esp_ref_perunitprice"+(i+2).toString(),aryRet[i+1]);
}
else
{
aryRet[i+1]=new Object();
discount=discountNodes[i].selectSingleNode("./discounttypeid.percentage").text;
aryRet[i+1].caption=discountNodes[i].selectSingleNode("./discounttypeid.lowquantity").text;
aryRet[i+1].caption+="PCS~";
aryRet[i+1].caption+=discountNodes[i].selectSingleNode("./discounttypeid.highquantity").text;
aryRet[i+1].caption+="PCS";
aryRet[i+1].value=stdAmount* (1-parseFloat(discount)/100);
aryRet[i+1].value.toFixed(2);
setControl("esp_ref_perunitprice"+(i+2).toString(),aryRet[i+1]);
}
}
}
}
//Han Jianxing
//set control caption and text for Text Control
//fieldValue.value =Text
//fieldValue.caption=Display Name
function setControl(fieldName, fieldValue)
{
var fieldObjData = crmForm.all[fieldName];
var fieldObjTcControl = document.getElementById( fieldName + "_c" );
fieldObjData.innerText=fieldValue.value;
fieldObjTcControl.innerText=fieldValue.caption;
}
function getDiscountNodes(oproductId)
{
var fetchXml=
[
"<fetch mapping='logical'>",
"<entity name='productpricelevel'>",
"<attribute name='amount'/>",
"<attribute name='transactioncurrencyid'/>",
"<filter type='and'>",
"<condition attribute='pricelevelid' operator='eq' value='{9439FF4D-8613-DE11-B063-001EC9B6EAFD}'/>",
"<condition attribute='productid' operator='eq' value='"+oproductId+"'/>",
"</filter>",
"<link-entity name='discount' from='discounttypeid' to='discounttypeid' link-type='outer'>",
"<attribute name='amount'/>",
"<attribute name='highquantity'/>",
"<attribute name='isamounttype'/>",
"<attribute name='lowquantity'/>",
"<attribute name='percentage'/>",
"<attribute name='transactioncurrencyid'/>",
"<order attribute='lowquantity'/>",
"</link-entity>",
"</entity>",
"</fetch>"
].join("");
fetchXml=fetchXml.replace(/</g,'<')
fetchXml=fetchXml.replace(/>/g,'>')
var xml =
[
"<?xml version='1.0' encoding='utf-8'?>",
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" ",
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ",
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">",
GenerateAuthenticationHeader(),
"<soap:Body>",
" <Fetch xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" ,
" <fetchXml>",
fetchXml,
" </fetchXml>",
" </Fetch>",
"</soap:Body>",
"</soap:Envelope>"
].join("");
var resultXml = executeSoapRequest("Fetch",xml);
var node=resultXml.selectSingleNode("//error");
if (node!=null)
{
alert(node.xml);
}
else
{
var fetchResult= resultXml.selectSingleNode("//FetchResult");
if (fetchResult!=null)
{
fetchResult=fetchResult.text;
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(fetchResult);
return xmlDoc.selectNodes("//result");
}
}
return null;
}
// Helper method to execute a SOAP request
function executeSoapRequest(action, xml)
{
var actionUrl = "http://schemas.microsoft.com/crm/2007/WebServices/";
actionUrl += action;
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction",actionUrl);
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
return resultXml;
}
// Helper method to return a single node value from XML
function getNodeValue(tree, el)
{
var retVal = null;
var e = null;
e = tree.getElementsByTagName(el);
if (e != null && e[0] != null) {
retVal = e[0].firstChild.nodeValue;
}
return retVal;
}
// Helper method to return a multiple node value from XML
function getMultipleNodeValues(tree, el)
{
var retVal = new Array();
var e = null;
e = tree.getElementsByTagName(el);
for(i = 0; i < e.length; i++) {
retVal[i] = e[i].firstChild.nodeValue;
}
return retVal;
}