Monday, December 27, 2010

Client-side scripting example for RetrieveMultiple

Goal:
When a new case is created generate a case number, instead of using the "auto-numbering", so that you can put it in any format you want. I wanted our case number to be all numeric, and be sequential. In other words, I wanted to create new cases with case number = last case number + 1.

Client-side code
In the Case form OnLoad event (under form properties)  add the following javascript:
Note: This client-side code is the almost equivalent to the SQL statement: SELECT TOP 1 ticketnumber + 1 FROM IncidentBase ORDER BY createdon DESC


var CRM_FORM_TYPE_CREATE = 1;
if(crmForm.FormType == CRM_FORM_TYPE_CREATE)
{
// Prepare variables to retrieve the contacts.
var msg = "";
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>incident</q1:EntityName>"+
"<q1:ColumnSet xsi:type='q1:ColumnSet'>"+
"<q1:Attributes>"+
"<q1:Attribute>ticketnumber</q1:Attribute>"+
"</q1:Attributes>"+
"</q1:ColumnSet>"+
"<q1:Orders>" +
"<q1:Order>" +
"<q1:AttributeName>createdon</q1:AttributeName>" +
"<q1:OrderType>Descending</q1:OrderType>" +
"</q1:Order>" +
"</q1:Orders>" +
"<q1:PageInfo>" +
"<q1:PageNumber>1</q1:PageNumber>"+
"<q1:Count>1</q1:Count>"+
"</q1:PageInfo>"+
"</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 = "Unable to generate case number, please fill in the Case Number field manually";
crmForm.all.ticketnumber.disabled = false;
alert(msg);
return;
}
else
{
var ticketnumber= results[0].selectSingleNode('./q1:ticketnumber').nodeTypedValue;
var strArr = ticketnumber.split('-');
var index = 0;
if(strArr.length > 1)
index = 1;
var tickNum = new Number(strArr[index]);
if(!isNaN(tickNum))
{
crmForm.all.ticketnumber.value = new Number(tickNum + 1);
}
else
{
msg = "Case number found was not a number, please fill in the Case Number field manually";
crmForm.all.ticketnumber.disabled = false;
alert(msg);
return;
}
}
}
}


References:
1. Server-side translation into client-side JavaScript
http://community.dynamics.com/product/crm/crmtechnical/b/crminogic/archive/2009/08/28/how-to-convert-a-retrievemultiple-query-written-server-side-to-soap-message-to-be-used-in-javascripts.aspx
2. Example of attributes for an entity (incident): http://msdn.microsoft.com/en-us/library/dd903985.aspx

Tuesday, December 21, 2010

Unable to publish workflow - Dynamics CRM 4.0

Problem:
You cannot publish Workflows in Dynamics CRM 4.0, and you get the following message in the event viewer: "Exception has been thrown by the target of an invocation."

Solution:
1. Open the CRM website's web.config. (typically located in C:\Inetpub\wwwroot)

2. Locate section <authorizedTypes>

3. Add the following to this section:
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Globalization" TypeName="CultureInfo" Authorized="True"/>

Friday, December 10, 2010

Two very useful tools for rebuilding a computer

DriverMax - Allows you to quickly see all of the drivers on your system, and then export them to a folder. This is useful because you can then slipstream the drivers into the OS installation, instead of having to download them all and add them from scratch.


nLite - Use it to build slipstreamed OS bootable ISO with customizations, such as adding drivers, creating an unattended installation, and more.

Boot and Nuke - This is used to completely erase data from hard drives.
There was an error in this gadget