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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Online Retreive Plugin

(0) ShareShare
ReportReport
Posted on by

Hi,

Newbie here in plugins. Can anyone help me about my issue?

Here's my code:

//Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            
            // Obtain the execution context from the service provider.
            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            
            // The InputParameters collection contains all the data passed in the message request.            
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                //Entity entityWBSDetail = (Entity)context.InputParameters["Target"];
                
                // Obtain the target entity from the input parmameters.
                //EntityReference entityRef = (EntityReference)context.InputParameters["Target"];                
                Guid ParentGuid = Guid.Empty;
                Entity ParentEntity = new Entity("new_wbsdetail");
                Entity CurrEntity = new Entity("new_wbsdetail");

                try
                {
                    tracingService.Trace("create entity Entity");
                    Entity entity = (Entity)context.InputParameters["Target"];
                    //Instead of getting entity from Target, we use the Image

                    //ParentGuid = entity.GetAttributeValue<Guid>("new_parentwbsdetailid");                    
                    tracingService.Trace("Before Entity");
                    CurrEntity = service.Retrieve("new_wbsdetail", entity.Id, new ColumnSet(new string[] { "new_name", "new_parentwbsdetailid" }));
                    tracingService.Trace("After Entity");
                    EntityReference EntityRef = ((EntityReference)(CurrEntity.Attributes["new_parentwbsdetailid"]));
                    tracingService.Trace("My Lookup ID {0}" + EntityRef.Id);
                    ParentEntity = service.Retrieve("new_wbsdetail", EntityRef.Id, new ColumnSet(new string[] { "new_name", "new_parentwbsdetailid" }));
                    tracingService.Trace("Parent Entity {0}", CurrEntity["new_name"]);

                    ParentEntity.Attributes["new_name"] = "test1";
                    //tracingService.Trace("Parent Entity Est No of Hours {0}", ParentEntity["new_estnoofhours"]);
                    
                    tracingService.Trace("Test");
                    service.Update(ParentEntity);
                    tracingService.Trace("AFter Update");                    
                }
                catch (Exception ex)
                {
                    tracingService.Trace("Updating Parent Record: {0}", ex.ToString());
                    throw;
                }                
            }


 And I'm having error

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): UpdateWBSParentDetails.UpdateParentWBSDetails: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.Detail:
<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
<ErrorCode>-2147220956</ErrorCode>
<ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
<Message>Unexpected exception from plug-in (Execute): UpdateWBSParentDetails.UpdateParentWBSDetails: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.</Message>
<Timestamp>2015-11-22T16:11:25.8692087Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText>create entity Entity
Before Entity
After Entity
My Lookup ID {0} 01119046-cd90-e511-80f2-3863bb341b70
Parent Entity test
Test
Updating Parent Record: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Unexpected exception from plug-in (Execute): UpdateWBSParentDetails.UpdateParentWBSDetails: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).

Please help. Really need it bad! Thank you.

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hei, if the error come from your code. Try to sorting your code to find where the error come from. You can start to commend from the inside of your 'try-catch' statement. You can use "/* */" or "//". And try to register your plugin again and see if its show error message again or not.

    thanks

  • LMP Profile Picture
    680 on at

    [Quote]

    The given key was not present in the dictionary.

    [/Quote]

    One of the field you are trying to access is not present. Double check the name of the fields if they are correct.

  • Mahendar Pal Profile Picture
    45,095 on at

    Make sure in all of the plugin which you have register under update, or any related plugin, check value under property bag before getting it

    so first check it ,

    if(entityobject.Contains("attributename"))

    {

    //read value here

    }

    Thanks

  • Community Member Profile Picture
    on at

    Hi HIMBAP,

    Thank you for your reply. I tried doing that but it says that the attribute does not contain any value so it skip "If" statement and go to "else". But in my record, there's a value in the lookup "new_parentwbsdetailid". That's why I'm wondering on why does it not return any value?

    Regards,

    Coline

  • Community Member Profile Picture
    on at

    Hi LMP,

    The field names are correct. I double checked it.

    I used tracing service and it shows that the error is in here

    service.Update(ParentEntity);

    Regards,

    Coline

  • Community Member Profile Picture
    on at

    Hello Saraswati,

    Thank you for your reply,

    I used Traccing service and the error is in here "service.Update(ParentEntity);"

    Please help.Thanks

    Regards,

    Coline

  • Community Member Profile Picture
    on at

    Hi Guido Preite,

    I already changed the plugin name to UpdateWBSParentDetails.UpdateWBSParentDetails

    but still nothing. :(

    Please help.Thanks

    Coline

  • Mahendar Pal Profile Picture
    45,095 on at

    I will suggest you to debug your plugin code, you can use pluginr registration tool to debug your code. support.microsoft.com/.../2778280

    Thanks

  • Suggested answer
    LMP Profile Picture
    680 on at

    Try checking your 'ParentEntity' if its null. Because maybe the result is zero and you are updating a null record that is why you get that error. Or, best is to learn how to debug a plug-in for online and on-premise. :)

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans