Dynamically Setting Entity Properties by Name at Runtime in C# Code in Infor CRM

There might be times when you need to set an entity property value by the property name as a string at runtime. Sure, you could use reflection for this and use the PropertyInfo class, but there is a built-in way that is much easier to use. All entities implement the IDynamicEntity interface, which provides this functionality out of the box.

// IDynamicEntity is located in
Sage.Platform.Orm.Interfaces.IDynamicEntity

When you use an entity object via it’s IDynamicEntity interface, it allows to to use dynamic properties instead of static.

Static/Normal Code Example:

var account = Sage.Platform.EntityFactory.Create<IAccount>();
account.AccountName = "Some Account";
account.Type = "Customer";
account.Save();

Dynamic Code Example using IDynamicEntity:

var account (Sage.Platform.Orm.Interfaces.IDynamicEntity)Sage.Platform.EntityFactory.Create<IAccount>();
account["AccountName"] = "Some Account";
account["Type"] = "Customer";
account.Save();

Here’s another example:

var phoneField = "MainPhone";

var account = this.BindingSource.Current as Sage.Platform.Orm.Interfaces.IDynamicEntity;
account[phoneField] = "8005551212";
account["Type"] = "Customer";

// it works for relationship properties as well
account["Owner"] = EntityFactory.GetById<IOwner>("SYST00000001");

account.Save();

Granted, there’s likely very limited scenarios where you’d need to access the entity properties by name in this way, but it does allow you to dynamically set entity properties without the use of (and overhead of) reflection.

ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix since 2001 and believes in sharing with the community. He loves C#, Javascript, Python, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) news and product updates!

You have Successfully Subscribed!