Showing posts with label Product Catalog. Show all posts
Showing posts with label Product Catalog. Show all posts

Friday, November 6, 2009

Unit set automatically with Default Unit

When setting up a new product in the product catalog, Default Unit is a system required field.

DefUnit

Great! So when you choose the product on the the opportunity/quote/order/invoice-product screen the Default Unit is automatically filled in in the Unit field… NOT!

UnitNotSet

As most users will want to choose the default unit, let’s set it automatically. To do so, add the next script to the OnChange event of the product field:

GetAttributeValueFromID= function(sEntityName, sGUID, sAttributeName,sOrganizationName)
{
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>" +
" <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>" + sEntityName +"</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>" + sAttributeName + "</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>"+ sEntityName + "id</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>"+
"<q1:Value xsi:type=\"xsd:string\">" + sGUID + "</q1:Value>" +
"</q1:Values>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </RetrieveMultiple>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","
http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
if (resultXml != null)
{
var names = resultXml.selectNodes("//BusinessEntity/q1:" + sAttributeName);
if (names != null)
{
if (names.length > 0)
{
return names[0].text;
}
}
//return "*Error*";
}
}

//Set unit with default unit of product using CRM webservice
if(crmForm.all.productid.DataValue != null)
{
    var ProductSelected = new Array;
    ProductSelected = crmForm.all.productid.DataValue;

    var UnitId = "";
    var UnitDescription = "";
    UnitId = GetAttributeValueFromID("product",ProductSelected[0].id,"defaultuomid");
    UnitDescription = GetAttributeValueFromID("uom",UnitId,"name");

    var InputUnitId = new Array();
    InputUnitId[0] = new LookupControlItem (UnitId, 1055, UnitDescription);
    crmForm.all.uomid.DataValue = InputUnitId;
}
else
{
    crmForm.all.uomid.DataValue = null;
}

 

This will retrieve the default unit from the product and fill out the unit field as the product is set.

UnitSetAuto

In this way, the end user still has the posibillity to choose another unit if necessary. But if not, is saves a couple of clicks!