Dynamics CRM 2011 Unit Test Part 11: QUnit with client side JavaScript

Dynamics CRM 2011 Unit Test Part 1: Introduction and Series Contents

The complete sample code using QUnit can be downloaded from MSDN sample gallery: http://code.msdn.microsoft.com/Dynamics-CRM-2011-client-9f189663

This post describe how to use QUnit and Xrm.Page Script Library Template (Microsoft Visual Studio extension) to unit test Dynamics CRM 2011 client side jQuery or JavaScript code.

More information on Xrm.Page Script Library Templatecan be found: http://msdn.microsoft.com/en-us/library/gg328261.aspx#BKMK_DevelopmentTools

More information on QUnit can be found: http://qunitjs.com/

Suppose we have one JavaScript web resource which will run when account form load. The client side JavaScript code will update the “Account Name” label to “Organisation Name” and set Preferred User look up field value to the Preferred User of the Primary Contact of the current account.

Code Under Test


/// <reference path="XrmPageTemplate.js" />

//This Library uses a namespace naming strategy to help prevent duplication function names
if (typeof (Zzhou) == "undefined")
{ Zzhou = { __namespace: true }; }
if (typeof (Zzhou.Qunit) == "undefined")
{ Zzhou.Qunit = { __namespace: true }; }
// Namespace container for functions in this library.
Zzhou.Qunit.Account = {
 onload: function () {
 var attrName = 'name',
 label = 'Organisation Name';
 this._changeControlLabel(attrName, label);
 this._updatePreferredUser();
 },
 _changeControlLabel: function (attrName, label) {
 var control = Xrm.Page.getControl(attrName);
 if (control) {
 control.setLabel(label);
 }
 },
 _updatePreferredUser: function () {
 // primary contact look up field name
 var attrName = 'primarycontactid';
 // get primary contact attribute
 var attribute = Xrm.Page.getAttribute(attrName);
 if (attribute) {
 // get primary contact attribute value
 var attrValue = attribute.getValue();
 if (attrValue && attrValue[0] && attrValue[0].entityType === 'contact') {
 // get primary contact id
 var primaryContactId = attrValue[0].id;
 // retrieve primary contact.PreferredSystemUserId
 SDK.JQuery.retrieveRecord(
 primaryContactId,
 "Contact",
 "PreferredSystemUserId",
 null,
 // success call back
 function (user) {
 if (user && user.PreferredSystemUserId) {
 var attrName = 'preferredsystemuserid';
 var attribute = Xrm.Page.getAttribute(attrName);
 if (attribute) {
 var lookupReference = [];
 lookupReference[0] = {};
 lookupReference[0].id = user.PreferredSystemUserId.Id;
 lookupReference[0].entityType = user.PreferredSystemUserId.LogicalName;
 lookupReference[0].name = user.PreferredSystemUserId.Name;

attribute.setValue(lookupReference);
 }
 }
 },
 function (error) {
 alert(error.message);
 }
 );
 }
 }
 },
 __namespace: true
};

Unit Test


/// <reference path="XrmPageTemplate.js" />
module(
 'Account Form Library Test',
 {
 setup: function () {
 this.savedAjax = $.ajax;
 },
 teardown: function () {
 $.ajax = this.savedAjax;
 }
 }
 );
test("Zzhou.Qunit.Account._changeControlLabel", function () {
 //
 // Arrange
 //
 var attrName = 'name',
 expectedLabel = 'Organisation Name';
 //
 // Act
 //
 Zzhou.Qunit.Account._changeControlLabel(attrName, expectedLabel);

//
 // Assert
 //
 var actualLabel, control = Xrm.Page.getControl(attrName);
 if (control) {
 actualLabel = control.getLabel();
 }
 strictEqual(actualLabel, expectedLabel, "actualLabel and expectedLabel are the same value and type");
});
test("Zzhou.Qunit.Account._updatePreferredUser", function () {
 expect(3);

//
 // Arrange
 //
 var data = {
 d: {
 PreferredSystemUserId: {
 Id: '5bcf7e47-9993-e111-88fa-00155d4c5b01',
 LogicalName: 'systemuser',
 Name: 'First name Last name'
 }
 }
 };
 $.ajax = function (options) {
 options.success(data, "success", { responseText: JSON.stringify(data) });
 };

//
 // Act
 //
 Zzhou.Qunit.Account._updatePreferredUser();

//
 // Assert
 //
 var attrName = 'preferredsystemuserid';
 var attribute = Xrm.Page.getAttribute(attrName);
 if (attribute) {
 var attrValue = attribute.getValue();
 if (attrValue && attrValue[0]) {
 strictEqual(attrValue[0].entityType, data.d.PreferredSystemUserId.LogicalName);
 strictEqual(attrValue[0].id, data.d.PreferredSystemUserId.Id);
 strictEqual(attrValue[0].name, data.d.PreferredSystemUserId.Name);
 }
 }
});

Advertisements

Dynamics CRM 2011 Rollup Update 11 IFD not working Failed to get priv user group information

Update:

 

A fix can be found from the following link

http://community.dynamics.com/product/crm/f/117/t/93178.aspx

Microsoft just provided me with a work around while they work on the hot fix.

• Change the Anonymous user identity of IIS Anonymous Authentication Credentials to Application pool Identity option:

1. On the CRM server, open the Internet Information Services (IIS) Manager;

2. In IIS Manager, click the CRM site;

3. In the Features View, double-click Authentication;

4. Select Anonymous Authentication , and then click Edit in the Actions pane;

5. In the Edit Anonymous Authentication Credentials dialog box, click the Application pool Identity , and then click Ok;

6. Do an IISRESET on CRM and ADFS server.

 

 

I have on premise IFD deployment, after install Rollup Update 11, it is not working, i keep get the following errors


Microsoft CRM Error Report:
Error Description:
Failed to get priv user group information. k = 1b813725-c917-4291-bcea-277fca8e799e privUserGroupId: 5140579c-a508-40a3-943b-fed25dac4842, localSystemAdGuid: 00000000-0000-0000-0000-000000000000, Exception: Microsoft.Crm.CrmSecurityException: Could not find GUID for server: VMCRM$ With SearchFilter:samAccountName at Microsoft.Crm.SecurityUtils.GetGuid(String searchItem, String searchFilter, String searchItemLogInfo, Boolean exceptionIfNotfound) at Microsoft.Crm.SecurityUtils.GetLocalSystemGuid() at Microsoft.Crm.Caching.OrganizationSettingsCacheLoader.LoadCacheData(Guid key, ExecutionContext context).

Error Details:
Failed to get priv user group information. k = 1b813725-c917-4291-bcea-277fca8e799e privUserGroupId: 5140579c-a508-40a3-943b-fed25dac4842, localSystemAdGuid: 00000000-0000-0000-0000-000000000000, Exception: Microsoft.Crm.CrmSecurityException: Could not find GUID for server: VMCRM$ With SearchFilter:samAccountName
 at Microsoft.Crm.SecurityUtils.GetGuid(String searchItem, String searchFilter, String searchItemLogInfo, Boolean exceptionIfNotfound)
 at Microsoft.Crm.SecurityUtils.GetLocalSystemGuid()
 at Microsoft.Crm.Caching.OrganizationSettingsCacheLoader.LoadCacheData(Guid key, ExecutionContext context).

Full Stack:
[CrmSecurityException: Could not find GUID for server: VMCRM$ With SearchFilter:samAccountName]
 at Microsoft.Crm.SecurityUtils.GetGuid(String searchItem, String searchFilter, String searchItemLogInfo, Boolean exceptionIfNotfound)
 at Microsoft.Crm.SecurityUtils.GetLocalSystemGuid()
 at Microsoft.Crm.Caching.OrganizationSettingsCacheLoader.LoadCacheData(Guid key, ExecutionContext context)

[CrmException: Failed to get priv user group information. k = 1b813725-c917-4291-bcea-277fca8e799e privUserGroupId: 5140579c-a508-40a3-943b-fed25dac4842, localSystemAdGuid: 00000000-0000-0000-0000-000000000000, Exception: Microsoft.Crm.CrmSecurityException: Could not find GUID for server: VMCRM$ With SearchFilter:samAccountName
 at Microsoft.Crm.SecurityUtils.GetGuid(String searchItem, String searchFilter, String searchItemLogInfo, Boolean exceptionIfNotfound)
 at Microsoft.Crm.SecurityUtils.GetLocalSystemGuid()
 at Microsoft.Crm.Caching.OrganizationSettingsCacheLoader.LoadCacheData(Guid key, ExecutionContext context).]
 at Microsoft.Crm.Caching.OrganizationSettingsCacheLoader.LoadCacheData(Guid key, ExecutionContext context)
 at Microsoft.Crm.Caching.ObjectModelCacheLoader`2.LoadCacheData(TKey key, IOrganizationContext context)
 at Microsoft.Crm.Caching.CrmMultiOrgCache`2.CreateEntry(TKey key, IOrganizationContext context)
 at Microsoft.Crm.Caching.CrmMultiOrgCache`2.LookupEntry(TKey key, IOrganizationContext context)
 at Microsoft.Crm.Authentication.Claims.ClaimsUtility.GetAuthenticationMethod(Guid organizationId, String method)
 at Microsoft.Crm.Authentication.Claims.CrmFederatedAuthenticationModule.SendToIdentityProvider(RedirectLocation redirectLocation)
 at Microsoft.Crm.Authentication.Claims.CrmFederatedAuthenticationModule.OnEndRequestHandler(Object sender, EventArgs e)
 at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
 at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Other Message:

Error Number:
0x80040216

Source File:
Not available

Line Number:
Not available

Error Trace:

&nbsp;

Date: 10-30-2012

Time: 20:09:56

Server: default.zhongchenzhou.com
Request URL:
https://default.zhongchenzhou.com/default.aspx

Dynamics CRM 2011 IE10 cannot import solution script error

When use IE10 together with Dynamics CRM 2011 (Rollup Update 10) and try to import a solution, there is a error window popup

This is caused by a script error in file

~\Tools\Solution\import\SolutionImportProcess.aspx
function window.onload() {

After change the code to

window.onload = function () {

Another error comes

 

If click on cancel, another error comes

 

Anyone know how to fix this?