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
   ComboFix Kills Saleslogix Sync
ComboFix, a third party malware utility, will alter the Operating system so .qts and .que files will no l
Posted on Sep 12, 2014 by Brianna Ojard to SalesLogix Support
 
   How do I access the new Opportunity that I created in Saleslogix??
I had a user ask why they could not access a Salelogix opportunity when they clicked the hyperlink from t
Posted on Sep 04, 2014 by Dale Richter to SalesLogix Questions & Answers
 
   Retrieving Items from a Picklist via Javascript
Saleslogix is very light on how to use client side processes to do things in the Saleslogix web client.&n
Posted on Aug 29, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   Saleslogix Xbar update coming soon
Scheduled to be released 8/29/2014 Features in Update •Installation improvements •Enterp
Posted on Aug 21, 2014 by Scott Weber to SalesLogix Product Blog
 
   How do you add a parameter for File Attachment Size and Timeout in Saleslogix 8.1?
Question: Is it possible to set a parameter for file attachment size and timeout in Salelogix? If
Posted on Aug 07, 2014 by Dale Richter to SalesLogix Questions & Answers
 
Comments

 

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:

Ryan,

   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,

Steve

January 1, 2014 9:57 AM
 

Steve Meyers said:

Ryan,

   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,

Steve

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:

Ryan,

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.

thanks,

John

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

(required)  
(optional)
(required)  
Add
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