Showing results for 
Search instead for 
Do you mean 
Community Home Request Access Read Blogs Share Your Ideas Search Community View My Settings
Reply
Employee
Posts: 24
Registered: ‎07-15-2009
Accepted Solution

Check IsPrimary Business Rule with custom Entity

Hi guys

 

We have a 1:M table called ContactEmailAddress that can hold multiple emails per contact, if the primary is set then all other IsPrimary must be set to false and if no IsPrimary is set then it should be set to true (ie first record inserted).

 

I have replicated the 3 Contact Business Rules (CheckIsPrimary, AccountHasPrimaryContact & ClearPrimaryContact) in a class library (external assembly):

 

namespace ContactEmailAddress
{
public class ContactEmailAddressBusRules
{
public static void CheckIsPrimary(IContactEmailAddress conemail)
{
if (!ContactHasPrimaryEmail(conemail.Contact))
{
if (!conemail.IsPrimary.HasValue)
{
conemail.IsPrimary = true;
}
}
else if (conemail.IsPrimary == true)
{
ClearPrimaryEmail(EntityFactory.GetById<IContact>(conemail.Contact.Id));
conemail.IsPrimary = true;
}
}

public static bool ContactHasPrimaryEmail(IContact contact)
{
IQueryable repository = (IQueryable) EntityFactory.GetRepository<IContactEmailAddress>();
IExpressionFactory expressionFactory = repository.GetExpressionFactory();
ICriteria criteria = repository.CreateCriteria();
criteria.Add(expressionFactory.Eq("ContactId", contact.Id.ToString()));
criteria.Add(expressionFactory.Eq("IsPrimary", true));
IProjection projection = repository.GetProjectionsFactory().Count("Id");
criteria.SetProjection(projection);
int num = (int) criteria.UniqueResult();
return (num > 0);
}

public static void ClearPrimaryEmail(IContact contact)
{
foreach (IContactEmailAddress conemail in contact.ContactEmailAddresses)
{
if (conemail.IsPrimary == true)
{
conemail.IsPrimary = false;
}
}
}
}
}

 

I am not getting any compile errors however when I use the ContactHasPrimaryEmail before the Save Business Rule I get the following error:

 

 Unable to cast object of type 'NewNamespace.ContactEmailAddress' to type 'Sage.Entity.Interfaces.IContact'.

Saleslogix Error Id: SLX988CAD8235FE4BE7

URL: http://localhost:3333/SlxClient/Contact.aspx?entityid=C6UJ9A000R24&modeid=Detail

Exception type: System.InvalidCastException

Source: Sage.Platform

 

Stack Trace:

at (Object )
at Sage.Platform.DynamicMethod.DynamicMethodLibrary.Execute(String methodName, Object[] args) in d:\BuildRoot\Gobi\SLX8.1HotFix\src\Sage\Platform\DynamicMethod\DynamicMethodLibrary.cs:line 84

 

Debugging is limited as I am using VS Express

 

Does anyone have any idea where I have gone wrong?

 

Regards,

Byron

Nickel Contributor
Posts: 56
Registered: ‎10-26-2010

Re: Check IsPrimary Business Rule with custom Entity

Have you wired up ContactHasPrimaryEmail to a business rule on ContactEmailAddress?

Employee
Posts: 24
Registered: ‎07-15-2009

Re: Check IsPrimary Business Rule with custom Entity

[ Edited ]

Hi Collin, yes I have.

 

I have created 3 different Business Rules under ContactEmailAddress for each method, could it be how I am referencing the other methods (Business Rules) from within ContactHasPrimaryEmail?

Nickel Contributor
Posts: 56
Registered: ‎10-26-2010

Re: Check IsPrimary Business Rule with custom Entity

ContactHasPrimaryEmail needs to be wired to a business rule on Contact since it accepts an IContact.

Employee
Posts: 24
Registered: ‎07-15-2009

Re: Check IsPrimary Business Rule with custom Entity

Hi Collin, thanks for you input. I have tried it on the Contact entity but the same error:

Unable to cast object of type 'NewNamespace.ContactEmailAddress' to type 'Sage.Entity.Interfaces.IContact'.

After having a look at the Contacts Business Rules, the Rule AccountHasPrimaryContact is wired up to IAccount but still sits under the Contact entity so doubt it was the issue.
Highlighted
Nickel Contributor
Posts: 56
Registered: ‎10-26-2010

Re: Check IsPrimary Business Rule with custom Entity

I've implemented IsPrimary and the business logic in an external assembly for another entity off contact with the same premise as you are doing for ContactEmailAddress.

 

I would call CheckIsPrimary onBeforeUpdate and onBeforeInsert on ContactEmailAddress. The CheckIsPrimary does the work. I don't call ContactHasPrimaryEmail directly. I also don't have ContactHasPrimaryEmail wired up to a business rule in AA.

 

Hope this helps,

Collin