Tuesday, November 13, 2012

Overriding the CreatedOn Attribute On Entities in Microsoft Dynamics CRM 2011

I wanted to write a quick blogpost because I think there are a lot of people out there that have never used the overriddencreatedon field that is on all CRM 2011 entities.  If you have ever tried to manipulate the createdon field manually using the CRM sdk you will find that it is basically read-only.

There is a workaround for this though.
If you populate the overriddencreatedon field when you insert your entities you can specify your own datetime for the createdon field.  Now when the field is saved the overriddencreatedon field datetime is put in the createdon field of the entity and the REAL createdon field is still saved in the overriddencreatedon field.

So all you have to do is populate the overriddencreatedon field and it will in turn populate the createdon field and the real date will then be saved in the overriddencreatedon field.

There is a limitation to this.
You cannot insert a datetime in the future.  This will throw an error.  If you are using an application on another server and using datetime.now you might want to also use .addminutes(-5) or something to that effect because if the server times are out of sync by even a little bit and the application server tries to insert a time that is in the future for the CRM server, it will fail.

-Happy Tuesday

No comments:

Post a Comment