Sunday, December 6, 2009

Setting the default unit of product

Hello,
Our team is continually getting the request from users that setting the default unit is »a click to far«
So there is a simple solution with using the web service …
I'm posting JavaScript code which is useful to setting the default unit on a product detail part (Opportunity, Quote, Sales Order or Invoice) enter the code bellow in the on change part of the product lookup.



// Setting the default unit***Tomaz Vodusek
var buid;
var productid = crmForm.all.productid.DataValue[0].id;
var authenticationHeader = GenerateAuthenticationHeader();
// Prepare the SOAP message.
var xml = "" +
"" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
authenticationHeader +
"" +
"" +
"product" +
"" + productid + "" +
"" +
"" +
"defaultuomid" +
"
" +
"
" +
"
" +
"
" +
"
";
// 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/Retrieve");
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);
}
// Display the retrieved value.
else {
buid = resultXml.selectSingleNode("//q1:defaultuomid").nodeTypedValue;
}
var ime
// Prepare the SOAP message.
xml = "" +
"" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
authenticationHeader +
"" +
"" +
"uom" +
"" + buid + "" +
"" +
"" +
"name" +
"
" +
"
" +
"
" +
"
" +
"
";
// Prepare the xmlHttpObject and send the request.
xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
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.
errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
// Display the retrieved value.
else {
ime = resultXml.selectSingleNode("//q1:name").nodeTypedValue;
}
// Create an array to set as the DataValue for the lookup control.
var lookupData = new Array();
// Create an object to add to the array.
var lookupItem = new Object();
// Set the id, typename, and name properties to the object.
lookupItem.id = buid;
lookupItem.typename = 'uom';
lookupItem.name = ime;
// Add the object to the array.
lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
crmForm.all.uomid.DataValue = lookupData;
//Konec Vnosa

No comments: