Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

Error in updating Entity B from Entity A

Posted on by Microsoft Employee

I'm trying to update entity A from entity B on click of save button. Below is my code

using System;
using Microsoft.Xrm.Sdk;

namespace MyPlugin
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

if (context.InputParameters != null)
{
try
{
Entity entity = context.PostEntityImages["PostImage"];
System.Diagnostics.Debug.WriteLine(entity.Attributes);

string m = (string)entity.Attributes["new"];
tracingService.Trace(m);

string sig = (string)entity.Attributes["sig"];

tracingService.Trace(sig);
System.Diagnostics.Debug.WriteLine(m + sig);

EntityReference parent = (EntityReference)entity.Attributes["contact"];

string required = m + sig;


Entity parententity = new Entity("contact");
parententity.Id = parent.Id;
parententity.Attributes["new_current"] = required;

service.Update(parententity);
}

catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
}

I'm getting an error in line Entity entity = context.PostEntityImages["PostImage"]; says "Key doesn't exist in the dictionary". Am I doing anything wrong in getting post image of the entity?

Any help would be appreciated.

Thanks


  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Error in updating Entity B from Entity A

    Hi,

    This error occurs if you haven't register the post image for the step or the name you have used while registering the post image is different.

    Hope this helps.

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Error in updating Entity B from Entity A

    Hi ,

    Few things you need to check-

    1.  Make sure you have provide exact post image name in below code , to avid confusion make sure you are putting same name in the Image Name and Alias name in plugin registration tool.

       Entity entity = context.PostEntityImages["PostImage"]; says "Key doesn't exist in the dictionary".

    2. When you are getting value from post image you should check attributes contains like below.

        Entity _postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains("PostImage")) ? context.PostEntityImages["PostImage"] : null;

        if (_postImageEntity.Attributes.Contains("Fieldame Of SIG"))

                   {

                       if (_postImageEntity.GetAttributeValue<string>("Fieldame") != "" && _postImageEntity.GetAttributeValue<string>("Fieldame Of SIG") != null)

                       {

                           sig = _postImageEntity.GetAttributeValue<string>("Fieldame Of SIG");

                       }

                   }

    3.  The attribute name should be with field publisher name .

        _postImageEntity.GetAttributeValue<string>("Fieldame Of SIG with publisher ");

    4.  Here is wrong in below line , what is contact? you should provide proper field name to get the id,  if its contact entity then contactid  need to be put.

       EntityReference parent = (EntityReference)entity.Attributes["contact"];

      You can write like below -

     if (_entity.Attributes.Contains("contactid"))
                    {
                        if (_entity.Attributes["contactid"] != null)
                        {
                            parent = _entity.Attributes["contactid"] as EntityReference;
                        }
                    }

    5.  Make sure you provide filtering attributes to all in plugin registration tool to get the post image.

    Please find final code which will be look like below - 

          public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity _entity = (Entity)context.InputParameters["Target"];
    
                    Entity _postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains("PostImage")) ? context.PostEntityImages["PostImage"] : null;
    
                    if (_entity.LogicalName != "customentity")  // Provide entityname
                        return;
    
                    string sig = string.Empty;
                    string m = string.Empty;
                    // For Single line text
                    if (_postImageEntity.Attributes.Contains("Fieldame Of SIG"))
                    {
                        if (_postImageEntity.GetAttributeValue<string>("Fieldame") != "" && _postImageEntity.GetAttributeValue<string>("Fieldame Of SIG") != null)
                        {
                            sig = _postImageEntity.GetAttributeValue<string>("Fieldame Of SIG");
                        }
                    }
    
    
                    // For Single line text
                    if (_postImageEntity.Attributes.Contains("second Field name "))
                    {
                        if (_postImageEntity.GetAttributeValue<string>("second Field name") != "" && _postImageEntity.GetAttributeValue<string>("second Field name") != null)
                        {
                            m = _postImageEntity.GetAttributeValue<string>("second Field name");
                        }
                    }
    
                    string concatval = sig + "" + m;
    
    
                    // to update another entity 
    
                    EntityReference parent = new EntityReference();
    
                    if (_entity.Attributes.Contains("contactid"))
                    {
                        if (_entity.Attributes["contactid"] != null)
                        {
                            parent = _entity.Attributes["contactid"] as EntityReference;
                        }
                    }
    
    
                    Entity parententity = new Entity("contact");
                    parententity.Id = parent.Id;
                    parententity.Attributes["new_current"] = concatval;
                    service.Update(parententity);
    
    
    
                }
            }


    Hope this helps.

  • Verified answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Error in updating Entity B from Entity A

    You will get more help at below link:

    www.magnifez.com/pre-image-and-post-image-in-dynamics-crm-plugins-advanced-plugin-concepts-part-1

  • Verified answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Error in updating Entity B from Entity A

    Hi,

    You have to select following fields

    new

    SIG

    contact

    in post image.

    Make sure these fields has values and also update your code as below:

    using System;

    using Microsoft.Xrm.Sdk;

    namespace MyPlugin

    {

    public class Class1 : IPlugin

    {

    public void Execute(IServiceProvider serviceProvider)

    {

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

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

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

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

    if (context.InputParameters != null)

    {

    try

    {

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

    System.Diagnostics.Debug.WriteLine(entity.Attributes);

    string m=string.empty;

    if(entity.Attributes.contains("new"))

    {

    m= (string)entity.Attributes["new"];

    }

    tracingService.Trace(m);

    string sig=string.empty;

    if(entity.Attributes.contains("sig"))

    {

    sig = (string)entity.Attributes["sig"];

    }

    tracingService.Trace(sig);

    System.Diagnostics.Debug.WriteLine(m + sig);

    EntityReference parent=new EntityReference();

    if(entity.Attributes.contains("contact"))

    {

    parent = (EntityReference)entity.Attributes["contact"];

    }

    else

    {

    return;

    }

    string required = m + sig;

    Entity parententity = new Entity("contact");

    parententity.Id = parent.Id;

    parententity.Attributes["new_current"] = required;

    service.Update(parententity);

    }

    catch (Exception ex)

    {

    throw new InvalidPluginExecutionException(ex.Message);

    }

    }

    }

    }

    }

    Please check syntax once you copy paste it in Visual Studio.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans