Using the EntityFactory to retrieve entity objects in SalesLogix 7.5

As Ryan briefly mentioned in this post, the EntityFactory allows you to retrieve entity objects from within the SalesLogix entity model.    The EntityFactory allows you to retrieve an existing entity collection or to create a new record within a collection, which you can then manipulate and then save. The EntityFactory also allows you to delete a specific entity from the collection.

Note that the EntityFactory is a member of the Sage.Platform class.  In my code samples that follow I provide samples that already include a using reference to this class.  If you do not have this type of using statement you would need to explicitly call the full class:  Sage.Platform.EntityFactory.

There are a couple of different ways to get an specific entity using the EntityFactory.

EntityFactory.GetRepository

One approach to use the EntityFactory to return a collection is with EntityFactory.GetRepository.  This method returns the entity collection you specify.  It also allows you to pass in a particular ID using the Get method, which will result in a specific record within the entity collection being returned.  Lets take a look at how to do this.  Lets say I want to get a reference to a particular Product entity using its IProduct collection.  I can use the following line of code to create a new IProduct reference called fxproduct which refers to the IProduct result returned from the EntityFactory.GetRepository method:

IProduct fxproduct = EntityFactory.GetRepository<IProduct>().Get(productID) as IProduct;

Now with my reference established I can use my fxproduct entity reference, such as:

IProduct fxproduct = EntityFactory.GetRepository<IProduct>().Get(productID) as IProduct;

double? myProductsPrice = fxproduct.Price;

Conversely, you could combine it into one line like this:

double? myProductsPrice = EntityFactory.GetRepository<IProduct>().Get(productID).Price;

If you wanted you could just return an entire entity collection (in my example, all products in the system) using the code above and omitting the Get method:

IProduct fxproduct = EntityFactory.GetRepository<IProduct>() as IProduct

EntityFactory.GetByID

Another very similar way of using the EntityFactory to return a entity collection to to use GetByID.  This is almost identical to the GetRepository method shown above, when used in conjunction with the Get method.  This call is specifically designed to return just one record in the entity collection.  To get my reference to the Product entity as I did before it would look something like:

IProduct fxproduct = Sage.Platform.EntityFactory.GetById<IProduct>(productID) as IProduct

 

EntityFactory.Create

This method allows you to create a new entity within the collection you pass in.  To use this you can just specify which entity you wish to create a new item within:

IProduct newProduct = EntityFactory.Create<IProduct>();

This method simply creates an empty shell.  You are responsible for then filling in the entity, either in code on the page or by using the entity’s business rules.  to fill it in you can use code such as:

IProduct newProduct = EntityFactory.Create<IProduct>();

newProduct.Price = 12.00;

newProduct.Name = "A new product";

newProduct.Save();

EntityFactory.Delete

This method allows you to delete a specific record with the entity collection.  To use this you pass in the entity item to delete:

IProduct fxproduct = Sage.Platform.EntityFactory.GetById<IProduct>(productID) as IProduct

EntityFactory.Delete(fxproduct);

ABOUT THE AUTHOR

Kris Halsrud

Kris Halsrud is a Senior Analyst / Developer for Customer FX Corporation.

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) and Creatio (bpm'online) news and product updates!

You have Successfully Subscribed!