Login / Register  search  syndication

          Ryan Farley's Blog

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

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.

What's This?
Bookmark and Share

About Ryan Farley

   Ryan Farley is the Director of Development for Customer FX Corporation and the creator of slxdeveloper.com.


Related Content
   Creating Pretty Prefixes and Suffixes in the SalesLogix web client
In the SalesLogix web client you may run across a reason you need to generate a “Pretty Key”. This is wh
Posted on Mar 17, 2010 by Kris Halsrud to Kris Halsrud's Blog
 
   SalesLogix Web Client- Working with Activities
In this webinar the user will learn to work with Activities from the Activities entity. This feature all
Posted on Mar 16, 2010 by Dale Richter to SalesLogix Training
 
   Adding an assembly reference to code files in the SalesLogix Application Architect
Often when adding custom code to SalesLogix you need to reference Assemblies that are not included by def
Posted on Mar 15, 2010 by Kris Halsrud to Kris Halsrud's Blog
 
   SalesLogix Web Client- Marketing Campaigns Part I [Video]
In this video webinar the Marketing Professional will learn how to create a New Marketing Campaign in Sal
Posted on Mar 15, 2010 by Dale Richter to SalesLogix Training
 
   Launching a report from a button in the SalesLogix web client
Starting in SalesLogix 7.5.1, Sage released an undocumented Reporting enhancement that allowed for intera
Posted on Mar 12, 2010 by Kris Halsrud to Kris Halsrud's Blog
 
Comments

 

Jason Webb said:

Thanks for the info.  Good post.

Personally I would be more inclined to use code snippets instead of C# Snippet Actions if the code snippets didn't just disappear on me all the time.  I am tired of trying to fix it in the XML all the time.

April 8, 2009 12:55 PM
 

Ryan Farley said:

Thanks Jason.

I agree. Although I miss the ease of using the C# Snippets and having access to everything on the page, there will be longer term benefits to having the separation that the Code Snippet Actions give.

April 8, 2009 12:59 PM
 

Mark Dykun said:

There are definately benifits to the codesnippet action but also downsides such as the loss of the snippet/renaming of the file. But also if you need to make that smartpart a custom one, the form interface will not be generated and it is a possible breakage point.

April 17, 2009 11:53 AM
 

Chris said:

Thanks for the post Ryan, much appreciated.

Doing first SLX 7.5 development at the moment and this is the second time you've saved me a fair amount of research through your blogs.. Ta!

May 6, 2009 10:30 PM

Leave a Comment

(required)  
(optional)
(required)  
Add
All contents Copyright © 2010 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