Archive

Archive for May, 2012

Retrieve multiple records synchronously using OData & JQuery in CRM 2011

Hi,

Below is the Jscript function to retrieve multiple records synchronously using OData & JQuery.

function retrieveMultipleSync(odataSetName, select, filter) {

var serverUrl = Xrm.Page.context.getServerUrl();

var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;

var odataUri = serverUrl + ODATA_ENDPOINT + “/” + odataSetName + “?”;

if (select) {

odataUri += “$select=” + select ;

}

if (filter) {

odataUri += “&” + “$filter=” + filter;

}

var service = GetRequestObject();

if (service != null) {

service.open(“GET”, odataUri, false);

service.setRequestHeader(“X-Requested-Width”, “XMLHttpRequest”);

service.setRequestHeader(“Accept”, “application/json,text/javascript, */*”);

service.send(null);

var requestResults = eval(‘(‘ + service.responseText + ‘)’).d;

return requestResults;

}

}

function GetRequestObject() {

var req = false;

if (window.XMLHttpRequest) {

req = new XMLHttpRequest();

} else if (window.ActiveXObject) {

req = new ActiveXObject(“Microsoft.XMLHTTP”);

}

return req;

}

  • Copy & Paste above method to a new .jscript file
  • Replace double quotes(“) symbols with your keyboard double quotes
  • Add the new .jscript file as web resource to CRM application
  • Add “Jquery” & json” helper classes as web resources

How do I call this method :-

  • In my sample below, I will show you how to read “Contacts” of an “Account”

function retrieveContactsByAccountId(accountId) {

// Pass ‘Contact’ set name since we are reading Contacts

var oDataSetName = “ContactSet”;

// Column names of ‘Contact’ (Pass * to read all columns)

var columns = “FirstName,LastName”;

// Prepare filter

var filter = “AccountId/Id eq guid'” + accountId + “‘”;

var requestResults  = retrieveMultipleSync(oDataSetName, columns, filter);

if (requestResults != null) {

if (requestResults.results != null && requestResults.results.length > 0) {

for (var indxContacts = 0; indxContacts < requestResults.results.length; indxContacts++) {

var contact = requestResults.results[indxContacts];

if (contact.FirstName) {

alert(“Contact’s First Name – ” + contact.FirstName);

}

}

}

}

else {

alert(“Error while retrieving; Error – ” + service.responseText);

}

}

Get OData URI conventions from here

🙂

Loading dependent jscript libraries in ribbon button’s execution CRM 2011

Hi,

I have a ribbon button on my “Account” main grid .

I am calling function “sayHello” which is in my web resource “account_ribbon” by defining custom action like below.

<Actions>

<JavaScriptFunction Library=”$webresource:account_ribbon” FunctionName=”sayHello”>

</JavaScriptFunction>

</Actions>

Now the problem is, I need to call another method from my “sayHello” function, which is in other web resource “common”.

Solution :-

The solution is simple. We can add all the required libraries to <Actions> node like below

 <JavaScriptFunction Library=”$webresource:common” FunctionName=”isNaN” />

Note – Since <JavaScriptFunction> requires a function name, pass “isNaN”.

Now my <Actions> node look like below

<Actions>

<JavaScriptFunction Library=”$webresource:account_ribbon” FunctionName=”sayHello”>

</JavaScriptFunction>

<JavaScriptFunction Library=”$webresource:common” FunctionName=”isNaN”>

</JavaScriptFunction>

</Actions>

🙂

Hide/Show sections using JScript in CRM 2011

Hi,

We can’t directly hide or show the section using

  • Xrm.Page.ui.controls.get(“{section_name}”).setVisible(true/false)

You have to first get the “Tab” control of your “Section”.

Below is the script to Hide/Show sections

    // Get the Tab which contains your Section

var tabCtrl = Xrm.Page.ui.tabs.get(“{tab_name}”);

if (tabCtrl && tabCtrl.sections.get(“{section_name}”)) {

tabCtrl.sections.get(“{section_name}”).setVisible(true/false);

}

You can get the name of Tab or Section from customization form.

🙂

System.InvalidOperationException while running Device Registration tool – CRM 2011 online

Hi,

When I was trying to generate Device “ID” and “Password” using the command “deviceregistration.exe /operation:Register”, I got below error

Unhandled Exception: System.InvalidOperationException: There is an error in XML

document (5, 5). —> System.Security.Cryptography.CryptographicException: The parameter is incorrect

Reason :-

  • When you run the “deviceresistration.exe”, it creates new “LiveDevice.xml” file under “C:\Users\user_name\LiveDeviceID\” folder
  • Error occurs when the tool try to parse the generated “LiveDevice.xml” and show the Id & Password on console

Fix :-

  • Delete “LiveDevice.xml” file from ”C:\Users\user_name\LiveDeviceID” folder
  • Run the command “deviceregistration.exe /operation:Register”

🙂

Report cannot be displayed – rsProcessingAborted

Hi,

This is the most common error we get when you run a custom report from CRM.

Report error - rsProcessingAborted

Report error – rsProcessingAborted

Though there could be many reasons below are the ways to know the reason for issue

To debug:-

  • Connect to the machine where your SSRS server installed and open the event viewer
    • Run –> eventvwr –> Windows –> Application
Report error - Event Viewer

Report error – Event Viewer

  • For detailed error description, open the SQL Reporting logs from below path
    • C:\Program Files\Microsoft SQL Server\MSRS10.MSSQLSERVER\Reporting Services\LogFiles

🙂

Limitation of OData End Point – Setting Activity Parties

Hi,

In one of my requirement, I had to create an “Appointment” record programmatically using Jscript by populating field ‘required attendees’ (i.e., ActivityParty) to it.

I followed below steps to complete the requirement

  • Created the “Appointment” record first
  • Added 2 contacts as “Required Attendees” to Appointment, for which I used below code and called it 2 times since I need to create 2 activity parties

var activityParty = new Object();

activityParty.PartyId = {

Id: participentId,

LogicalName: participentLogicalName

};

activityParty.ActivityId = {

Id: activityId,

LogicalName: “appointment”

};

activityParty.ParticipationTypeMask = { Value: 5 }; //5 = Required Attendees

var activityParty = createRecord (activityParty, “ActivityPartySet”);

Issue I faced :-

  • The problem I faced was,
    • only one “Contact” added as “Required Attendees” (i.e., Only 1 Activity Party is creating) instead of 2
    • When I debug the script and DB, I observed that 1st activity party is getting created initially but once after the 2nd activity party created, the first one is getting deleted

Reason & Workaround :-

  • Later I came to know that, this is the limitation of OData end point.
  • As a workaround, I registered a Plugin on post create of “Appointment” and added “Activity Parties”

🙂