Left to Right tabbing in MSCRM 2013

The default tab behaviour in Microsoft Dynamics CRM is top to bottom, rather than left to right.

The following will change the tab order from left to right on your form in IE, Firefox and Chrome. However, please note that it is unsupported.


function TabOrderLefttoRight() {
for (var i = 0; i < Xrm.Page.ui.controls.getLength() ; i++) {
var control = Xrm.Page.ui.controls.get(i);
var element = document.getElementById(control.getName());

if (element.tabIndex && element.tabIndex != "0") {
if (element.className == 'ms-crm-Hidden-NoBehavior')
continue;
if (element.tagName == 'A') {
if (element.className != 'ms-crm-InlineTabHeaderText')
continue;
}
element.tabIndex = 1000 + (i * 10);
}
}

Advertisements

Using JavaScript to assign a record

A while back, a user on the Microsoft Dynamics forums asked how to assign a record using JavaScript. Here’s the code, remember to update the userid and recordid variables.

var header = GenerateAuthenticationHeader();

var userid = "838975f4-029c-de11-9693-0003ffc4c746";
var recordid = "9721c859-d185-dc11-ac34-000c291d8da3";
var target = "TargetOwnedAccount";

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\">" +
header +
" <soap:Body>" +
" <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <Request xsi:type=\"AssignRequest\">" +
" <Target xsi:type=\""+ target +"\">" +
" <EntityId>"+ recordid +"</EntityId>" +
" </Target>" +
" <Assignee>" +
" <PrincipalId xmlns=\"http://schemas.microsoft.com/crm/2006/CoreTypes\">"+ userid +"</PrincipalId>" +
" <Type xmlns=\"http://schemas.microsoft.com/crm/2006/CoreTypes\">User</Type>" +
" </Assignee>" +
" </Request>" +
" </Execute>" +
" </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/Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);

var resultXml = xmlHttpRequest.responseXML;

(I pasted it here so that I can find it easily in future).

Setting a lookup value to null

To set a lookup field to null you’d do the following in your c# code:

vehicle.new_accountid = new Lookup();
vehicle.new_accountid.IsNull = true;
vehicle.new_accountid.IsNullSpecified = true;

To do the same using JavaScript, you’d do the following:

crmForm.all.new_accountid.DataValue = null;

Performing a Javascript Web Service call to the Metadata Service

About a week ago a colleague of mine told me that he’d been able to use my JS script to retrieve fields from a related record upon the change of a lookup value- which worked exactly as he wanted. However, one of the fields he retrieved was a Picklist value- which was obviously a numeric value and pretty pointless since the field he was displaying the value in was nvarchar.

He had three options;

1) Hard code the text values for each option value in his code (yeah, right!),

2) Create a picklist with the same values (text and numeric) as the source picklist and ensure that his documentation clearly outlines that the two picklists will forever have to kept in sync (umm, no thanks),

3) Perform a web service call to the Metadata service and retrieve the options for the source picklist (!).

So I decided to put together the JavaScript code to achieve (3). In the example below, I’m retrieving the accountcategorycode field from the Account entity.

var request = "" +
"<?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>" +
" <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <Request xsi:type=\"RetrieveAttributeRequest\">" +
" <MetadataId>00000000-0000-0000-0000-000000000000</MetadataId>" +
" <EntityLogicalName>account</EntityLogicalName>" +
" <LogicalName>accountcategorycode</LogicalName>" +
" <RetrieveAsIfPublished>false</RetrieveAsIfPublished>" +
" </Request>" +
" </Execute>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/MetadataService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction",
"http://schemas.microsoft.com/crm/2007/WebServices/Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", request.length);
xmlHttpRequest.send(request);
var result = xmlHttpRequest.responseXML;
alert(result.xml);

Hope that helps somebody out there!