Tags

Hello Everyone,

I’ve seen many request regarding usage of LineItemNo while adding products to Quote or Order. Eventually my recent client too asked for this same functionality on CRM 4.0, So i though of finally writing my First blog on this. [Had to start from somewhere so why not with this and that too at a peculiar date Today~Friday the 13th !!!  😉 ]

By default, there is a system attribute LineItemNo(int) at Quote/Order Product forms(which can be added during form customization) but without having any properties like auto-populating Item nos while adding products. We can use the same attribute for this Customization instead of creating a new one unnecessarily. In my example here i’m using the Order form with field LineItemNo added to it.

In order to achieve this, the best way is to serially update the LineItemNo field in all the respective Order Product forms at last by running the code while updating the Order form (i.e. after finally adding or deleting the products).

Now, just like RetrieveMultiple we don’t have any CrmService through which we can update multiple forms at a single call. So, i thought of a workaround where i’m using CrmService.RetrieveMultiple method to filter all the Order Product forms with respective of the particular Order. And using the GUID of those filtered forms again  i’m updating each LineItemNo field one by one in a loop using CrmService.Update method.

The Code goes like this :-
var conid=crmForm.ObjectId;
var n = 0;
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>salesorderdetail</q1:EntityName>”+
“<q1:ColumnSet xsi:type=’q1:ColumnSet’>”+
“<q1:Attributes>”+
“<q1:Attribute>salesorderdetailid</q1:Attribute>”+
“</q1:Attributes>”+
“</q1:ColumnSet>”+
“<q1:Distinct>false</q1:Distinct>”+
“<q1:Criteria>”+
“<q1:FilterOperator>And</q1:FilterOperator>”+
“<q1:Conditions>”+
“<q1:Condition>”+
“<q1:AttributeName>salesorderid</q1:AttributeName>”+
“<q1:Operator>Like</q1:Operator>”+
“<q1:Values>”+
“<q1:Value xsi:type=’xsd:string’>”+conid+”</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&#8221;);
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;
var totamt=0;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode(‘//description’).nodeTypedValue;
alert(msg);
}
// Parse and display the results.
else
{
var results = resultXml.getElementsByTagName(‘BusinessEntity’);

for (i=0;i < results.length;i++)
{
if(results[i].selectSingleNode(‘./q1:salesorderdetailid’) !=null)
{
var ids= results[i].selectSingleNode(‘./q1:salesorderdetailid’).nodeTypedValue;

n = n + 1 ;
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>”+
“<Update xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>”+
“<entity xsi:type=’salesorderdetail’>”+
“<lineitemnumber>”+n+”</lineitemnumber>”+
“<salesorderdetailid>”+ids+”</salesorderdetailid>”+
“</entity>”+
“</Update>”+
“</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/Update&#8221;);
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
{
alert(“LineItemNo Values to id = “+ids+” successfully updated.”);
}
// end of CrmService.Update
}
}
// end of form ids loop
}
// end of CrmService.RetrieveMultiple

This works for numbering both Adding Existing-products as well as Write in products based on the priority of creation of the products.

Hope this Helps !!! 🙂

Ashwani.

Advertisements