Monday, February 7, 2011

Hiding System User Views

System user view are stored in SavedQueryBase table.
If you want to hide a view there is a field names IsPrivate and if you set the value to 1 it will be hidden.
Please note that this is an unsupported customization.

update SavedQueryBase
set IsPrivate = 1
where SavedQueryId =''

Sunday, February 6, 2011

Sharing Records in CRM Using DB Scripts

When you share a record with a team or a user do you know where it is stored in the database? The entity name is PrincipalObjectAccess. This is the entity which stores the shared information.

I have stated below the columns which will have to be populated in this table.

PrincipalId
This field stores information regarding Team/User guid that the record will be shared with.

PrincipalTypeCode
This field stores information regarding whether the record is shared with a team or a user.
Team – Value is 9
User – Value is 8

ObjectId
This field stores information regarding the record that is being shared with a user/team

ObjectTypeCode
This field stores information regarding the shared record entity type code.
e.g.:  account – 1
Contact – 2..etc…..

AccessRightsMask
Type of share
e.g.: - read, write

These are the important fields to remember.

SQL script to do this is given below.


INSERT into PrincipalObjectAccess ( PrincipalId, PrincipalTypeCode, ObjectId, ObjectTypeCode, AccessRightsMask,
InheritedAccessRightsMask, ChangedOn)

Values (guid, 9,  @aID, (select objecttypecode from entity where logicalname ='account'), 786455, 0, GETDATE()

When Saving an Account Record You get a Type Cast Error


There are times that after a data migration to CRM accounts table and you try to do a change to the account record and save it you get an error. Mostly you get specified cast is not valid exception.

If you try to save another record you might not get this error. This gets you thinking. What have I included in the error record and not in the corrected saved account record? 

Put the dialog trace that is given by CRM and enable the trace. You will not get a detail view of the error, but an important thing you will see. Right before the exception there has been method fired to update the address.

This is the place to start working with this. You should know when you save an account record there will 2 another entity that will get updated, which is customer address base entity. For each account record there will be 2 address records in the customer address base. However you will be displayed in the associated view only one record. If you don’t have these records and if you try to save the accounts record you will get an error.

This will most probably occur with migrated data as we will not migrate records for customer address base if there are not addresses. However you at least will have to add some dummy records to get his working. By doing that you will be able to save the accounts records.

Thursday, February 3, 2011

Javascript Window.Open and Window.ShowModelDialogBox

There are difference between these methods when used. One cannot state this is the best method as these are suited for different scenarios.


For an example if you want your child window to be on top of the parent window and you cannot do anything to do the parent window till you close the child window, you will probably use Window.ShowModelDialogBox method.


If you want to access the parent window information from child window you will have use Window.Open method as Window.ShowModelDialogBox will not keep tabs with the parent window once it is executed.


However Window.ShowModelDialogBox can be used to pass a parameter value to the parent window from the child window using a return value. 


I will post some more information about these that i have experienced later.

Reading Uncommited data In SQL Server

Most of the time when we want to do an update, delete or insert to the database we use begin, rollback SQL statements.
If only you are very sure that you will be using commit tran.

Why we use rollback tran is to make sure that what ever that we do has not caused the data in the DB to be in inconsistent state. Lets take a look at the below example.

1. begin tran
2. update HumanResources.Employee
3. set NationalIDNumber = 60
4. where EmployeeID = 257
5. rollback tran

If your transaction is not completed then you will not be able to check the changes in the data in the DB as the changes have not been committed yet.

During such times you can run the below statement and get this job done.

set transaction isolation level read uncommitted

Tuesday, February 1, 2011

Example of adding 'Or' and 'And' Operator in a CRM Query

We most of the time need one operator, either 'and' Or 'or.

But there are some queries we need to use both these in collaboration. Following example displays how this can be done.


ConditionExpression conditionExpression1 = new ConditionExpression("a", ConditionOperator.Equal, Val);
ConditionExpression conditionExpression2 = new ConditionExpression("b", ConditionOperator.Equal, Val);
ConditionExpression conditionExpression3 = new ConditionExpression("statuscode", ConditionOperator.Equal, 1);

//Adds the or operator conditions
FilterExpression filterExpression = new FilterExpression();
filterExpression.FilterOperator = LogicalOperator.Or;
filterExpression.Conditions = new ConditionExpression[] { conditionExpression1, conditionExpression2 };

//Adds the or operator condition with and Operator condition
FilterExpression filterExpression2 = new FilterExpression();
filterExpression2.FilterOperator = LogicalOperator.And;
filterExpression2.Conditions = new ConditionExpression[] { conditionExpression3 };
filterExpression2.Filters = new FilterExpression[] { filterExpression };

Converting a string Array to a Guid Array with one line of C# Code

This is no big deal. We can get this done in using looping through an array also. But I checked the google and found out about how to get this done form one line of code.
The Code line is below for your reference.

GuidArray = Array.ConvertAll(StringArray, delegate(string stringID) { return new Guid(GuidID); });

StringArray = string array
stringID = string variable
GuidID = Guid variable
GuidArray = Final Guid Array

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...