One of the best things about working with the SalesLogix 7.2 Web application is how great the entity model is. You have this single place to put all your rules and logic, the UI is really just an afterthought. Something that I do quite often is create business rules to return data specific to an entity instance. For example, a rule to return the primary contact for an account. Let's take that a step further and return a list of objects from an business rule. For this scenario, we'll create a business rule that will return a list of child accounts for the current account instance.
To do this, go to the Account entity in the Application Architect and create a new business rule. Give the rule a name of GetChildAccounts. What we will be returning from this rule is a List<IAccount>. However, you'll get problems if you try to add that as the return type for the rule in some versions. That's fine, we can specify that we'll be returning an object and all will be OK (If the return type is object, you will have some problems with grids. It is better to manually type in the specific return type, like this: Add System.Collections.Generic.IList<Sage.Entity.Interfaces.IAccount> as the return type). Add a step for the rule and it will end up looking like the following:

Click the Edit Code Snippet and we'll enter the following code:
#region Usings
using System;
using Sage.Entity.Interfaces;
using Sage.Platform;
using Sage.Platform.Repository;
#endregion Usings
namespace Sage.BusinessRules.CodeSnippets
{
public static partial class AccountBusinessRules
{
public static void GetChildAccountsStep1( IAccount account, out object result)
{
IRepository<IAccount> repository = EntityFactory.GetRepository<IAccount>();
IQueryable qry = (IQueryable)repository;
IExpressionFactory ef = qry.GetExpressionFactory();
ICriteria criteria = qry.CreateCriteria();
criteria.Add(ef.Eq("ParentId", account.Id.ToString()));
result = criteria.List<IAccount>();
}
}
}
Let's talk about what that code is alll about for a sec. We first create a repository for IAccount. This is basically the "place" and "type" where we will be getting our data. We next create an IQueryable object which represents our query. We add an ICriteria, via the ExpressionFactory of our IQueryable object, which specifies that we want anything from our repository that meets the criteria that the entity's "ParentId" property equals the current account instance's Id value. From there we simply get a List of the results. I'll be posting a lot more about how to use and understand this syntax in the future, but I wanted to point out that it is all NHibernate under the covers. This means you can use HQL queries as well, which might make for more readable syntax for some. Either way, there is plenty of good info online already. See http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html_single/#querycriteria for starters.
Tomorrow we'll look at binding those results to a grid using the GetByMethod option for the datasoruce.