Login / Register  search  syndication  about

          Ryan Farley's Blog

Ryan Farley on .NET Development with a focus on CRM Development for SalesLogix

Extending Sublogix Entities

If you've followed my posts on Sublogix, or even better, given Sublogix a spin, you'll know that if you're doing SalesLogix development Sublogix can save you a ton of time and give you a much easier to use data access and entity model. This post will show you how to extend the Sublogix entities to give you even easier access and save even more time.

The Sublogix Entity Model

Before we look at extending Sublogix entities, let's take a look at the standard generated Sublogix entities. The Sublogix library has a built-in entity model for the standard out of the box SalesLogix database. There are T4 templates you can add to your project to generate the entity model based on your own custom SalesLogix database schema. Either way, you have and entity class for each table in the database. If you've used teh T4 templates, you can find a file in your project called SublogixEntityObjects.cs which contains all of these classes and you can take a look. There's nothing special about these classes. The important part of these objects are the attributes they're decorated with that tells Sublogix how the classes and properties relate to the SalesLogix database tables and fields.

Extending A SubLogix Entity

Let's look at a sample of extending the Account entity. Let's add a property to get the related Contacts for the account. To do this, we simply need to inherit the Account entity as our base class, then add soem new custom properties.

First, let's create a new class, we'll call it AccountExtended. Then we'll have it inherit from account. At this point we have the following:

using System;
using System.Collections.Generic;
using Sublogix.Model;
using Sublogix.Entities;
namespace SublogixDemo.Entities { public class AccountExtended : Sublogix.Entities.Account { } }

There is one crucial thing missing from this class. The attributes that normally decorate a Sublogix entity. You can see these by looking at the Account class in the SublogixEntityObjects.cs file that was generated by the T4 templates. Once we add that our class will look like this:

using System;
using System.Collections.Generic;
using Sublogix.Model;
using Sublogix.Entities;
namespace SublogixDemo.Entities { [EntityElement(Name="ACCOUNT", PrimaryKey="ACCOUNTID")] public class AccountExtended : Sublogix.Entities.Account { } }

Now we can add whatever custom properties to the class we need. One thing to note, the base Account class has a built-in repository. So, to add the property to get the related child Contacts, we just need to add the following:

using System;
using System.Collections.Generic;
using Sublogix.Model;
using Sublogix.Entities;
namespace SublogixDemo.Entities { [EntityElement(Name="ACCOUNT", PrimaryKey="ACCOUNTID")] public class AccountExtended : Sublogix.Entities.Account { public IList<Contact> Contacts { get { return this.Repository.Find<Contact>(x => x.AccountId == this.AccountId); } } } }

Pretty simple. Now we could just use account.Contacts to get a list of the Contacts related to the account. Often, when I do this sort of thing, I store the retrieved items locally in a private variable to avoid going back to the database to retrieve them. For example, this sample extends the OpportunityProduct entity to provide the related Product entity built-in. The difference here is that I only retrieve it once, then store the reference so next time I ask for it I don't make a trip back to the database for it.

using System;
using Sublogix.Model;
using Sublogix.Entities;
namespace SublogixDemo.Entities { [EntityElement(Name="OPPORTUNITY_PRODUCT", PrimaryKey="OPPPRODUCTID")] public class OpportunityProductExtended : Sublogix.Entities.OpportunityProduct { private Product _product; public Product Product { get { if (_product == null) _product = this.Repository.GetById<Product>(this.ProductId); return _product; } } } }

One of the cool things about doing this is that if you're binding the Sublogix entity to a datagrid, you can create properties for related items so you can easily bind as you normally would since the values from related tables are all built in. Also, it goes without saying that you can certainly do a lot more than just retrieving related entities. You could do any sort of calculations or complex things there too, of course. Here's one that extends the Opportunity entity to add a calculated property to get the total price of all related opportunity products.

using System;
using System.Linq;
using System.Linq.Expressions;
using Sublogix.Model;
using Sublogix.Entities;
namespace SublogixDemo.Entities { [EntityElement(Name="OPPORTUNITY", PrimaryKey="OPPORTUNITYID")] public class OpportunityExtended : Sublogix.Entities.Opportunity { public double? OpportunityProductTotal { get { return this.Repository.Find<OpportunityProduct>(x => x.OpportunityId == this.OpportunityId).Sum(x => x.Price); } } } }

The sky is the limit. If you need to add any writable properties, that is, a property with a "set" as well as a "get", you'll need to tell Sublogix to ignore that property, otherwise it will try to match that up with a SalesLogix database field with the same name. To tell Sublogix to ignore that property, simply add the EntityElement attribute with an Ignore flag.

[EntityElement(Ignore = true)]
public string MyCustomProperty { get; set; }

Using Extended Sublogix Entities

To use an extended Sublogix entity, you just replace the standard entity name with your extended one.

So, instead of doing this:

var accountList = repo.Find<Account>(x => x.AccountName.StartsWith('Ab'));

You just do this to use your extended entity (Notice the change from Account to AccountExtended):

var accountList = repo.Find<AccountExtended>(x => x.AccountName.StartsWith('Ab'));

Pretty easy. Are you drinking the Sublogix koolaid yet?

What's This?
Bookmark and Share

About 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.

View the SalesLogix Mobile Seveloper Series
View the SalesLogix SData Developer Series
View the Git for the SalesLogix Developer series

Related Content
   Mobile 3.0.3 for Saleslogix v7.5.4, v8.0.0 & v8.1.0 is now available!
Saleslogix Mobile v3.0.3 is now available in the customer portals. This update addresses all issues int
Posted on Apr 14, 2014 by Scott Weber to SalesLogix Product Blog
   Editing Filters in Saleslogix Web Client 8.0
Is it possible to add or remove Saleslogix filters from the List view in Accounts?
Posted on Apr 08, 2014 by SalesLogix Support to SalesLogix Questions & Answers
   Creating a Unique Saleslogix table ID in a SQL Function
A couple of months ago Ryan Farley wrote an article about how to use a SQL stored procedure to create a S
Posted on Mar 25, 2014 by Kris Halsrud to Kris Halsrud's Blog
   Saleslogix Integration with Google - WOW!
Saleslogix will be releasing their Google Integration this month and from what I have seen, it is fantast
Posted on Mar 18, 2014 by Scott Weber to SalesLogix Product Blog
   Using a Test Plan
So you have approved the Statement of Work (SOW) for some modifications and are awaiting the delivery of
Posted on Mar 13, 2014 by George Jensen to George Jensen's Blog


RJ Samp said:

Just got back into SubLogix for a project and downloaded the latest version. Cool stuff and thank you!

December 8, 2011 4:38 PM

Ryan Farley said:

Good to hear RJ. Thanks.

December 8, 2011 5:09 PM

Steve Meyers said:


   This tool is awesome and the T4 tamplates work great in VS2010. Unfortunately the templates are not functioning in 2012. Do you know what I need to do to make this work in 2012? The SubLogix DLL works great in 2012 but it is just the baseline structures.

Thanks in advance,


January 1, 2014 9:57 AM

Steve Meyers said:


   On further review of the issue, noted above, it seems to be failing when the template is generating the Save method for the Campaigntargetsview. I presume that there should not be a Save method for a View. However, this failure did not occur in the version for VS 2010. For the record, I am using the SalesLogix_Eval database for my testing,

Thank you,


January 2, 2014 9:40 AM

Steve Meyers said:

Ok, it turns out that I had a bunch of tables in ReSyncTableDefs that did not actually exist. I also had to exclude several tables and views. This said, the boolean nature of the Char(1) fields seems to be lost in the CustomEntities.

January 6, 2014 5:17 PM

John Crumpton said:


Is it possible to override/extend the methods for a specific class?

For example, say you wanted to update Opportunity.SalesPotential each time an OpportunityProduct is saved?

I tried to do something like this on the OpportunityProduct entity

public override void Save(){

//do custom stuff


but Save is a part of Repository, not the Entity itself.

If I add public void Save() to the Entity, then the Repository's Save method never fires.

I worked around it by adding a new method and just calling that just before the Save method... it works, but would look a lot nicer if I could work that into the Save method somehow.



March 12, 2014 7:23 AM

Ryan Farley said:

Hi John,

Yes, you absolutely can create a custom Save method. The Save method is implemented in the entity itself. In fact, you can see the existing code for what Save does in the generated code file SublogixEntityObjects.cs (which is created from the templates).

Basically, all the Save method does is check to see if the entity exists already and then calls either repository.Add or repository.Update. That is it. So, if I were going to create my own custom Save method, I would start by just copying the Save method from the generated class in SublogixEntityObjects.cs and put that in your custom entity, then add whatever else you'd need.

Make sense?

March 12, 2014 12:15 PM

Leave a Comment

All contents Copyright © 2014 Customer FX Corporation
Customer FX Corporation
2324 University Avenue West, Suite 115
Saint Paul, Minnesota 55114
Tel: 800.728.5783

  Follow @CustomerFX on twitter
Follow the best news, tips, and articles
  Subscribe to Customer FX on youtube
Watch SalesLogix tutorial videos from Customer FX
Login / Register