Answered by:
CRM 2011 Early Bound Entities : How to development questions

Question
-
Hi,
I have not the answer of these questions and require help from developers who know how to:
- Remove an attribute from an attributes collection of an Early Bound Entity. The code for the Late Bound Entities is :
((Entity)myEntity).Remove("attributelogicalname");
- Check if an Early Bound Entity contains a specific attribute. The code for the Late Bound Entities is:
((Entity)myEntity).Contains("attributelogicalname");
My blog : http://mscrmtools.blogspot.com
Did you try the new CrmDiagTool for Microsoft Dynamics CRM 2011 ? If not, follow me
Upgraded tools for Dynamics CRM 2011!
View Layout Replicator | Searchable Property Updater | Ribbon Browser | SiteMap Editor | JavaScript Web Resource Manager | Role updaterTuesday, May 15, 2012 8:11 AMModerator
Answers
-
A slightly better way to do this is by using this helper class.
public static class ReflectionUtility { public static string GetPropertyName<T>(Expression<Func<T>> expression) { MemberExpression body = (MemberExpression)expression.Body; return body.Member.Name.ToLower(); } }
To use it you can do something like this
var target = ((Entity) context.InputParameters["Target"]).ToEntity<Contact>(); if (target.Attributes.Contains(ReflectionUtility.GetPropertyName(() => target.ParentCustomerId))) { if (target.ParentCustomerId == null) { // ParentCustomerId updated to null } else { // ParentCustomerId updated to some other value } } else { // ParentCustomerId wasn't updated }
This way, you don't hardcode any strings.
Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits
- Proposed as answer by Dimaz Pramudya (www.xrmbits.com) Tuesday, May 15, 2012 11:21 AM
- Marked as answer by Tanguy T [MVP CRM]MVP, Moderator Tuesday, May 15, 2012 11:59 AM
Tuesday, May 15, 2012 11:17 AM
All replies
-
Hi Tanguy,
What was your questions again?
Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits
Tuesday, May 15, 2012 9:38 AM -
Simply that I know how to check presence of attribute in Late Bound Entities and how to remove a reference to an attribute in this same Late Bound Entities
But I don't know how to do it for Early Bound Entities.
Let's take an example:
Imagine I want to detect in a Lookup attribute has been emptyed in a plugin.
For Late Bound Entities, it's easy to write:
if(((Entity)myEntity).Contains("mylookup") && ((Entity)myEntity["myLookup"] == null) { // It has been emptyed }
But I don't know how to write the same thing for Early Bound entities
var myAccount = (Account)context.InputParameters["Target"]; if(myAccount.myLookup == null) { }
myAccount.myLookup could be null because attribute is not contained in the object or because the value has been emptyed...
I know that it is possible to do something with Lambda expression but I don't know how...
My blog : http://mscrmtools.blogspot.com
Did you try the new CrmDiagTool for Microsoft Dynamics CRM 2011 ? If not, follow me
Upgraded tools for Dynamics CRM 2011!
View Layout Replicator | Searchable Property Updater | Ribbon Browser | SiteMap Editor | JavaScript Web Resource Manager | Role updaterTuesday, May 15, 2012 10:17 AMModerator -
What I normally do for plugin is to combine both late bound and early bound.
I use the late bound technique to see whether the myLookup field is changed. If it is, I normally convert the late bound entity into Early Bound one for a better coding experience.
var target = ((Entity) context.InputParameters["Target"]); if (target.Contains("myLookup")) { // Convert the Late Bound into Account Early Bound var account = target.ToEntity<Account>(); // Do something with the Account here }
When you convert the late bound into early bound using the ToEntity method, any changes you make to the early bound attributes will get propagated to the Late Bound one. This is good when you do stuff on PreCreate.
I don't think there is a way to determine whether the field is updated just from Early Bound class alone.
Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits
Tuesday, May 15, 2012 10:40 AM -
I know this way to code but I'm really trying to have a unified methodology for developping plugins only with Early Bound Entities and I know this should be possible (another MVP has some info but he is really busy these days) using Lambda expression...
My blog : http://mscrmtools.blogspot.com
Did you try the new CrmDiagTool for Microsoft Dynamics CRM 2011 ? If not, follow me
Upgraded tools for Dynamics CRM 2011!
View Layout Replicator | Searchable Property Updater | Ribbon Browser | SiteMap Editor | JavaScript Web Resource Manager | Role updaterTuesday, May 15, 2012 10:44 AMModerator -
Hi Tanguy,
Just did further digging into this. You can actually do this from the Early Bound classes.
var myAccount = (Account)context.InputParameters["Target"]; if (myAccount.Attributes.Contains("myLookup")) { if (myAccount.myLookup == null) { // myLookup value was updated to NULL } else { // myLookup value was updated to some other value } } else { // myLookup value wasn't changed }
Hope this helps. You can put some lambda around this if you want. But this should do the job.
Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits
- Edited by Dimaz Pramudya (www.xrmbits.com) Tuesday, May 15, 2012 11:03 AM
Tuesday, May 15, 2012 11:03 AM -
A slightly better way to do this is by using this helper class.
public static class ReflectionUtility { public static string GetPropertyName<T>(Expression<Func<T>> expression) { MemberExpression body = (MemberExpression)expression.Body; return body.Member.Name.ToLower(); } }
To use it you can do something like this
var target = ((Entity) context.InputParameters["Target"]).ToEntity<Contact>(); if (target.Attributes.Contains(ReflectionUtility.GetPropertyName(() => target.ParentCustomerId))) { if (target.ParentCustomerId == null) { // ParentCustomerId updated to null } else { // ParentCustomerId updated to some other value } } else { // ParentCustomerId wasn't updated }
This way, you don't hardcode any strings.
Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits
- Proposed as answer by Dimaz Pramudya (www.xrmbits.com) Tuesday, May 15, 2012 11:21 AM
- Marked as answer by Tanguy T [MVP CRM]MVP, Moderator Tuesday, May 15, 2012 11:59 AM
Tuesday, May 15, 2012 11:17 AM -
Great! This seems really interesting!
I will try your method
Thanks
My blog : http://mscrmtools.blogspot.com
Did you try the new CrmDiagTool for Microsoft Dynamics CRM 2011 ? If not, follow me
Upgraded tools for Dynamics CRM 2011!
View Layout Replicator | Searchable Property Updater | Ribbon Browser | SiteMap Editor | JavaScript Web Resource Manager | Role updaterTuesday, May 15, 2012 11:58 AMModerator -
No worries Tanguy.
Glad I can help you.
Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits
Wednesday, May 16, 2012 1:10 AM