When setting up a new product in the product catalog, Default Unit is a system required field.
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!
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.
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!
In addition to this:
ReplyDeleteI just wanted The Unit Group and Default Unit to be filled in automatically, because they have to be filled. When I tried to hide the fields or make them business recommended, I got the an error when I saved the product.
So I searched on the internet and Jim Wang posted an example from de SDK and I changed it to what I needed:
//Create an array to set as the DataValue for the lookup control.
var lookupData = new Array();
//Create an Object add to the array.
var lookupItem= new Object();
//Set the id, typename, and name properties to the object.
lookupItem.id = '{047461C6-08E4-4553-B2BE-3DD8BBDCD467}';
lookupItem.typename = 'uomschedule';
lookupItem.name = 'Default Unit';
// Add the object to the array.
lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
crmForm.all.defaultuomscheduleid.DataValue = lookupData
//Create an array to set as the DataValue for the lookup control.
var lookupData1 = new Array();
//Create an Object add to the array.
var lookupItem1= new Object();
//Set the id, typename, and name properties to the object.
lookupItem1.id = '{8B480589-CBD5-4A80-8801-23BA41C65D22}';
lookupItem1.typename = 'uom';
lookupItem1.name = 'Primary Unit';
// Add the object to the array.
lookupData1[0] = lookupItem1;
// Set the value of the lookup field to the value of the array.
crmForm.all.defaultuomid.DataValue = lookupData1
Hi Bertil,
ReplyDeleteThis worked great for me. Was trying out a longer route which I was writing an OnLoad event on the Opportunity Product Form to default the Unit value as soon as the form is opening but was not working for some reason.
Thanks a ton. I really appreciate it.