I have been working on a custom entity inside of CRM. Basically, I was adding product to it and wanted to get the price per unit for the correct price list, multiply it by the quantity and apply any discount. This functionality works great in the quote section, and I needed to duplicate it. Well, I did and I did it all in JavaScript. I did not have to create a plug-in, which could also be done. Here is how I did it:
//this is to get the proudctid guid
var product = document.crmForm.all.new_productid;
//this is to determine if I have chosen to set my own price per unit
var work = crmForm.all.new_overrideprice.value;
var product2 = new Array();
product2 = product.DataValue;
var productID = product2[0].id;
var product4 = productID;
//this is where I stop getting the productid guid
//this is me setting the default pricelist to a var,
//if you want to dynamicly set the pricelist you would need to do a search on it like I do for the product
var pl = “{C5987A92-DADB-DC11-9F47-001143E82CB3}”;
if (productID!=null)
{
//set your own error check
}
if (work == ’1′)
{
// Prepare variables to retrieve the figures.
var authenticationHeader = GenerateAuthenticationHeader();
// Prepare the SOAP message.
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’>”+
authenticationHeader+
“<soap:Body>”+
“<RetrieveMultiple xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>”+
“<query xmlns:q1=’http://schemas.microsoft.com/crm/2006/Query’”+
” xsi:type=’q1:QueryExpression’>”+
“<q1:EntityName>productpricelevel</q1:EntityName>”+
“<q1:ColumnSet xsi:type=’q1:ColumnSet’>”+
“<q1:Attributes>”+
“<q1:Attribute>amount</q1:Attribute>”+
“</q1:Attributes>”+
“</q1:ColumnSet>”+
“<q1:Distinct>false</q1:Distinct>”+
“<q1:Criteria>”+
“<q1:FilterOperator>And</q1:FilterOperator>”+
“<q1:Conditions>”+
“<q1:Condition>”+
“<q1:AttributeName>pricelevelid</q1:AttributeName>”+
“<q1:Operator>Equal</q1:Operator>”+
“<q1:Values>”+
“<q1:Value xsi:type=’xsd:string’>”+pl+”</q1:Value>”+
“</q1:Values>”+
“</q1:Condition>”+
“</q1:Conditions>”+
“<q1:FilterOperator>And</q1:FilterOperator>”+
“<q1:Conditions>”+
“<q1:Condition>”+
“<q1:AttributeName>productid</q1:AttributeName>”+
“<q1:Operator>Equal</q1:Operator>”+
“<q1:Values>”+
“<q1:Value xsi:type=’xsd:string’>”+product4+”</q1:Value>”+
“</q1:Values>”+
“</q1:Condition>”+
“</q1:Conditions>”+
“</q1:Criteria>”+
“</query>”+
“</RetrieveMultiple>”+
“</soap:Body>”+
“</soap:Envelope>”;
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject(“Msxml2.XMLHTTP”);
xHReq.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);
xHReq.setRequestHeader(“SOAPAction”,”http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple”);
xHReq.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8″);
xHReq.setRequestHeader(“Content-Length”, xml.length);
xHReq.send(xml);
// Capture the result.
var resultXml = xHReq.responseXML;
// Check for errors.
var errorCount = resultXml.selectNodes(‘//error’).length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode(‘//description’).nodeTypedValue;
alert(msg);
}
// Parse and display the results.
else
{
var results = resultXml.getElementsByTagName(‘BusinessEntity’);
var msg = “”;
if (results.length == 0)
{
msg = “No item was found.”;
alert(msg);
return;
}
else
{
for (i=0;i < results.length;i++)
{
var amount = results[i].selectSingleNode(‘./q1:amount’).nodeTypedValue;
crmForm.all.new_priceperunit.value = amount;
}
}
}
//this is where I take my 3 fields and add them together if I have not chosen to override the system price
var pri = parseInt(crmForm.all.new_priceperunit.value.replace(/,/g, ”));
var qt = parseInt(crmForm.all.new_quantity.value.replace(/,/g, ”));
var di = parseInt(crmForm.all.new_discount.value.replace(/,/g, ”));
var total = ((pri * qt) – di);
crmForm.all.new_amount.value = total;
}
else
{
//this is where I take my 3 fields and add them together if I have chosen to override the system price
var price = (crmForm.all.new_priceperunit.value.replace(/,/g, ”));
var qut = (crmForm.all.new_quantity.value.replace(/,/g, ”));
var dis = (crmForm.all.new_discount.value.replace(/,/g, ”));
//set the total field amount
var tot = ((price * qut) – dis);
crmForm.all.new_amount.value = tot;
}
I hope this helps some of you.
l8r