web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Entity (Target) is not bring PostImage values.

(0) ShareShare
ReportReport
Posted on by

I have a plug-in that is attempting to read a custom entity and create a history record in a related entity.  However, I am getting no values in my Target entity.  Here is the "Execute" code:

 ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

 IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

 if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

{

 Entity entity = (Entity)context.InputParameters["Target"];

 if (entity.LogicalName != "lena_dlp")

return;

///my code goes past here so I AM getting the proper entity from the form.

 IOrganizationServiceFactory serviceFactory =

(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

try

{

 Entity dlphist = new Entity("lena_dlphistory");

///this next line is what causes the Exception. 

dlphist["lena_dlp_sn"] = entity["lena_dlp_sn"];

When looking at the "entity" object, the values are non-existent.  I can look at entity.Attributes[0] or any other notation and get no results.

What am I missing?

Don DeVeux

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hi Don,

    In the title you said postimage, but in your code nothing I found the code lines related to Post Image..

    To get the postImage you need the code something like:

    if (context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] is Entity)

                       {

                           //get postImageEntity

                           Entity postImageEntity = context.PostEntityImages["PostImage"];

    }

    "When looking at the "entity" object, the values are non-existent.  I can look at entity.Attributes[0] or any other notation and get no results."

    But if the entity object is null, the code will not pass this:

    if (entity.LogicalName != "lena_dlp")

    Instead, it will 'return' and then stop execute your plugin.

    But, it seems it pass the code so, the 'entity' is not null.

    Can I know you register in what message event?

  • Community Member Profile Picture
    on at

    Hi Aileen - thanks for the reply.

    Let me clarify. The entity I am looking to get values from is the "lena_dlp", which is being returned.  My reference to the PostImageEntity was misstated.  What I meant was I was looking for post-process values (after the update was completed) so I'm looking for the PostEntityImage values.  At a later date I will also look for the PreEntityImage values, but that's for another day.

    In other words, I want to read the values from the updated record in "lena_dlp" and save those values in another entity.  My program errors out when I try to read the values from the "lena_dlp" instance.

    My plug-in is registered for the "Update" event for the lena_dlp entity.

    Does that help clarify things?

  • Suggested answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hi Don,

    Update event Post or Pre?

    ///this next line is what causes the Exception.

    dlphist["lena_dlp_sn"] = entity["lena_dlp_sn"];

    I am curious what exception actually you got if you run the plugin?

    Is that Object reference not set, Null value or anything else?

    Because I worry that the Null value is not coming from entity, but instead, coming from

    attribute lena_dlp_sn in this: entity["lena_dlp_sn"];

    So, better you check if entity.Contains("lena_dlp_sn")

    Or you can use GetAttributeValue()

    There is a great example from Ben Hosking:

    crmbusiness.wordpress.com/.../crm-2013-using-entity-getattributevalue-instead-of-entity-contains

    And my next question is: does the "lena_dlp_sn" attribute value was updated? Because if not, it will not give you any value unless you use PreImage entity.

    Thanks.

  • Community Member Profile Picture
    on at

    Hi Aileen - that got me farther.  

    I changed the processing to Pre- and used the Entity.GetAttributeValue() method.  Now, it looks like this:

                       Entity dlphist = new Entity("lena_dlphistory");

                       dlphist["lena_dlp_sn"] = entity.GetAttributeValue<string>("lena_dlp_sn");

                       dlphist["lena_name"] = entity.GetAttributeValue<string>("lena_dlp_sn");

                       dlphist["lena_dlp_statusid"] = entity.GetAttributeValue<OptionSetValue>("lena_dlp_statusid");

                       dlphist["lena_dlp_statusdate"] = DateTime.Today;

                       dlphist["lena_dlp_statusreason"] = "just because";

    But I get an exception when trying to create the record:

                       service.Create(dlphist);

    The error message reads:

    An event was requested after the recorded event queue has been completed

    Ideas?

  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Rengeek,

    Are you sure you have this field:

    dlphist["lena_dlp_statusreason"] = "just because";

    I never encounter that error.

    But, for history, I recommend you to register on Post Update....

    Please comment the codes and change to minimalist code like this:

    Entity dlphist = new Entity("lena_dlphistory");

    dlphist["lena_name"] = "Hello Rengeek";

    service.Create(dlphist);

    Because I really want to know what exactly makes this error.

    Is that because of wrong assigned attribute value or not.

    Thanks.

  • Community Member Profile Picture
    on at

    Hi Aileen,

    I have other "Business Required" fields in the Entity.  Won't that make your code fail?

    Don

  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Rengeek,

    No...Through plugin it will be skipped.

    Unless you create through UI and open the form (after create through plugin), it will give you error.

    Just to make sure that you can create the history.

  • Community Member Profile Picture
    on at

    Hi Aileen,

    New code:

                       Entity dlphist = new Entity("lena_dlphistory");

                       dlphist["lena_name"] = "Rengeek";

                       service.Create(dlphist);

    Same error as above.

  • Aileen Gusni Profile Picture
    44,524 on at

    remove the service.Create?

  • Community Member Profile Picture
    on at

    First, I added code to try and create an account record:

                       Entity account = new Entity("account");

                       account["name"] = "Fourth Coffee";

                       mAcctId = service.Create(account);

    Same error:

    Then I removed the service.Create statements and it functioned without error, but, of course, it didn't create the record.

    I looked at the service object and it seems valid - at least it's not causing an error, but I can't really see any values.  When I try I get the following:

    Count = Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans