Accessing the Current Parent Entity from a Code Snippet Action

The transition from using pre-version 7.5.1 “C# Snippet Actions”, where the code is placed inline in the form, and the new “Code Snippet Actions”, where the code is compiled into a separate DLL and a psudo-form reference is passed in, can be frustrating at times. I ran into yet another instance of that today. Before, we were spoiled using the C# Snippet Actions. We had access to everything on the form. Everything. That’s no longer the case with Code Snippet Actions. While in a Code Snippet Action, I needed a reference to the current parent entity (not the current entity that is exposed via form.CurrentEntity, but it’s parent). This Code Snippet Action was on a LoadAction of an Insert screen so the parent had not yet been set. Let’s take a look at how to solve that.

Before you go on, read a previous post of mine where I discuss the differences between C# Snippet Actions and Code Snippet Actions if you’ve not done so already: Setting the Default Sort for a DataGrid in SalesLogix Web REVISITED

The Problem

The problem I needed to solve was get the current entity’s parent from a LoadAction on an insert screen. At that time the parent had not yet been set so we couldn’t just use myentity.ParentEntity to get it. If this were an older C# Snippet Action then I could have simply used the following:

IMyParentEntity myparent = this.GetParentEntity() as IMyParentEntity;

That would have been great. But this wasn’t in a C# Snippet Action and “this” in a Code Snippet Action is an entirely different thing where GetParentEntity did not exist. My first thought was to use the form.NativeForm and cast it as an EntityBoundSmartPart. That seemed logical. The cast worked, however when I attempted to call GetParentEntity it failed due to it’s protection level. Doh! It is protected so I can’t call it from another assembly.

The Solution

Thankfully, one of the most useful things the form referenced that is passed in has is the page’s WorkItem. Through the WorkItem you can get a reference to one of the many useful services in the portal. In this case, the EntityContextService. The EntityContextService has a method named GetEntity that will give you the currently accessed entity. Not the child entity that my dialog was for, but the entity the page itself is for, my parent entity. I wired up the following code:

// Get a reference to the EntityContextServce
// and call GetEntity()
IMyParentEntity myparent = form.WorkItem.Services.Get<Sage.Platform.Application.IEntityContextService>().GetEntity() as IMyParentEntity;

That is it. It works and it isn’t so bad. However, I do miss the C# Snippet Actions as well. I realize I can still use them even though they are marked as obsolete, however, I think there is also value in learning the new way to do things and living with the changes.

ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

1 Comment

  1. Ryan I really appreciate all that you and your co-workers have done to publish solutions to various issues.

    This one came in very handy today.

    Thanks!

    Reply

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!