I have created a custom entity in MS CRM 4. I have a couple of lookups (one for product, one for currency), an integer field (quantity) and a couple of money fields (price per unit and total). I wanted to be able to lookup the product and have it populate the price per unit and total fields. All of the code is entered into the OnSave event on the form.
What I did:
First,
//this is to get the proudctid guid that was selected
var product = document.crmForm.all.new_productid;
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
var pl = “{H5637A92-ZGQB-DE91-9J43-001543E82HL4}”;
if (productID!=null)
{
//put an alert message here.
}
// Prepare variables to retrieve Authentication
var authenticationHeader = GenerateAuthenticationHeader();
// Prepare the SOAP message. This is getting the information from the sever
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’>”+
//this is what entity I want to read
“<q1:EntityName>productpricelevel</q1:EntityName>”+
“<q1:ColumnSet xsi:type=’q1:ColumnSet’>”+
“<q1:Attributes>”+
//this is what fields from the entity I want to read
“<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>”+
//This is where I say what pricelevel I want to use
“<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>”+
//this is where I pass the product
“<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 product was found.”;
alert(msg);
return;
}
else
{
for (i=0;i < results.length;i++)
{
//this is where I get the amount and pass it to the field I want
var amount = results[i].selectSingleNode(‘./q1:amount’).nodeTypedValue;
crmForm.all.priceper.value = amount;
}
}
}
//this is where I calculate the total price
var pri = crmForm.all.priceper.value;
var qt = crmForm.all.quantity.value;
var total = (pri * qt);
crmForm.all.amount.value = total;
Keep moving along now.

Comments