locked
Plugin Error RRS feed

  • Question

  • Dear All,

    I was developing a plugin on CRM 2011. My goal is to add two field valuess(new_a,new_b) and display the result on third field(new_c) on Create/Update. I wrote the following code and registered the Plugin Assembly. Then i added two steps under the Plugin. But I am not sure whether i am doing correct or wrong. Kindly guide me if i am wrong.

    1st Step:

    Message: Create,  Event Pipeline: Post-OPeration,  Execution Mode: Synchronous; Execution ORder: 1

    2nd Step:

    Message: Update, Event Pipeline: Post-OPeration,  Execution Mode: Synchronous;  Execution Order: 2

    When I am testing with only 1st step, it's working. When I register 2nd step and trying to update i am getting error as below.

    CODE:

    try
                    {
                         IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                   Entity member = service.Retrieve("new_test", (System.Guid) targetEntity["new_testid"] , new ColumnSet("new_a","new_b","new_c"));
                       if (!member.Contains("new_a") || !member.Contains("new_b"))
                       {
                        throw new InvalidPluginExecutionException("One of the valueshave not been entered.  Please make sure these are entered    so test_3 can be calculated.");
                       }

                       tracer.Trace("  Required fields present.");

                       if (member.Contains("new_c"))
                       {
                           Double a = ((Double)member["new_a"]);
                           Double b = ((Double)member["new_b"]);
                           Double c = (a + b);                      
                           member.Attributes.Add("new_c", (double)c);
                           //targetEntity.Attributes.Add("new_c", (double)c);

                       }
                       else
                       {
                           Double a = ((Double)member["new_a"]);
                           Double b = ((Double)member["new_b"]);
                           Double c = (a + b);
                           member.Attributes.Add("new_c", (double)c);

                       }

                       service.Update(member);
                    }
                   catch (FaultException<OrganizationServiceFault> ex)
                   {
                       throw new InvalidPluginExecutionException(String.Concat("An error occurred.Update plug-in.", ex.Message, Environment.NewLine, ex.StackTrace));
                   }
                   catch (Exception ex)
                   {
                       throw new InvalidPluginExecutionException(String.Concat("An error occurred.Update plug-in.", ex.Message, Environment.NewLine, ex.StackTrace));
                   }

    Error: (After Registering 2nd Step, Tried to update the record):

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred.Update plug-in.An item with the same key has already been added.
       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)Detail:
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220891</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
        <KeyValuePairOfstringanyType>
          <d2p1:key>OperationStatus</d2p1:key>
          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
        </KeyValuePairOfstringanyType>
      </ErrorDetails>
      <Message>An error occurred.Update plug-in.An item with the same key has already been added.
       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)</Message>
      <Timestamp>2011-09-05T06:02:14.652941Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText>

    [TestPlugin: TestPlugin.PluginUpdateTest]
    [b70b048d-84d7-e011-b7a1-00155dc8c90d: TestPlugin.PluginUpdateTest: Update of new_test]
    ENTER:Values.Execute
      Required fields present.

    </TraceText>
    </OrganizationServiceFault>

     

    • Edited by crmpnk1 Monday, September 5, 2011 6:20 AM
    Monday, September 5, 2011 6:13 AM

Answers

  • nagcrm,

    For your plugin registered in preOperation, Update, retrieve the current updated entity by using the following line:

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

    This entity contains only the key attribute and the attributes updated (ie. if you updated just A, B is not present).

    To be sure to retrieve A and B, add an image to your plugin step and retrieve the image by using following line:

    Entity preData = (Entity)context.preEntityImages["<The name of the preImage>"];

    This entity contains all data on the database of the entity before you triggered the save event.

    With these elements, you can write the following plugin:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    tracer.Trace("ENTER:Values.Execute");
    
    Entity updatedData = (Entity)context.InputParameters["Target"];
    if (updatedData.LogicalName != "new_test") { return; }
    
    Entity preData = (Entity)context.PreEntityImages["TEST"];
    
    double a = 0;
    double b = 0;
    
    // Checks if the attribute "a" was updated in the form
    if (updatedData.Contains("new_a"))
    {
           a = ((Double)updatedData["new_a"]);
    }
    // Checks if the attribute "a" was already present
    else if (preData.Contains("new_a"))
    {
            a = ((Double)preData["new_a"]);
    }
    
    // Checks if the attribute "b" was updated in the form
    if (updatedData.Contains("new_b"))
    {
            b = ((Double)updatedData["new_b"]);
    }
    // Checks if the attribute "b" was already present
    else if (preData.Contains("new_b"))
    {
            b = ((Double)preData["new_b"]);
    }
    
    // Adds the calculation, no need to update the entity, as 
    // we are in preUpdate stage
    updatedData["new_c"] = (double)(a + b);
    


    My blog : http://mscrmtools.blogspot.com

    All my tools for Dynamics CRM 4.0 on my dedicated site: MSCRMTools Repository

    • Marked as answer by crmpnk1 Monday, September 5, 2011 4:57 PM
    Monday, September 5, 2011 3:26 PM
    Moderator

All replies

  • Hi,

    You need to update your code following lines:

     

    if (member.Contains("new_c"))

    { 

    Double a = ((Double)member["new_a"]);

    Double b = ((Double)member["new_b"]);

    Double c = (a + b);

    member.Attributes["new_c"] =  (double)c;

    }

     


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Monday, September 5, 2011 6:16 AM
  • Dear Javeed,

    I checked like you said. Now shoing while creating itself the following error. Now my code is like this

    CODE:

    try
                    {
                         IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                   Entity member = service.Retrieve("new_test", (System.Guid) targetEntity["new_testid"] , new ColumnSet("new_a","new_b","new_c"));
                       if (!member.Contains("new_a") || !member.Contains("new_b"))
                       {
                        throw new InvalidPluginExecutionException("One of the valueshave not been entered.  Please make sure these are entered    so test_3 can be calculated.");
                       }

                       tracer.Trace("  Required fields present.");

                       if (member.Contains("new_c"))
                       {
                           Double a = ((Double)member["new_a"]);
                           Double b = ((Double)member["new_b"]);
                           Double c = (a + b);

                           member.Attributes["new_c"] = (double)c;                       
                 }
                       else
                       {
                           Double a = ((Double)member["new_a"]);
                           Double b = ((Double)member["new_b"]);
                           Double c = (a + b);
                           member.Attributes.Add("new_c", (double)c);

                       }

                       service.Update(member);
                    }
                   catch (FaultException<OrganizationServiceFault> ex)
                   {
                       throw new InvalidPluginExecutionException(String.Concat("An error occurred.Update plug-in.", ex.Message, Environment.NewLine, ex.StackTrace));
                   }
                   catch (Exception ex)
                   {
                       throw new InvalidPluginExecutionException(String.Concat("An error occurred.Update plug-in.", ex.Message, Environment.NewLine, ex.StackTrace));
                   }

     

    Error:

     

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again. For information about workflow logic, see Help.
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Retrieve(String entityName, Guid id, ColumnSet columnSet, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.RetrieveCore(String entityName, Guid id, ColumnSet columnSet)
       at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Retrieve(String entityName, Guid id, ColumnSet columnSet)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)Detail:
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220891</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
        <KeyValuePairOfstringanyType>
          <d2p1:key>OperationStatus</d2p1:key>
          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
        </KeyValuePairOfstringanyType>
      </ErrorDetails>
      <Message>An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.An error occurred.Update plug-in.This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again. For information about workflow logic, see Help.
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Retrieve(String entityName, Guid id, ColumnSet columnSet, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.RetrieveCore(String entityName, Guid id, ColumnSet columnSet)
       at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Retrieve(String entityName, Guid id, ColumnSet columnSet)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)</Message>
      <Timestamp>2011-09-05T06:33:53.5429525Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText>

    [TestPlugin: TestPlugin.PluginUpdateTest]
    [6546536e-84d7-e011-b7a1-00155dc8c90d: TestPlugin.PluginUpdateTest: Create of new_test]
    ENTER:Values.Execute
      Required fields present.

    </TraceText>
    </OrganizationServiceFault>

     


    • Edited by crmpnk1 Monday, September 5, 2011 6:31 AM
    Monday, September 5, 2011 6:30 AM
  • Hi,

    If you have registered the plugin for Update event and will update the same entity using Service.Update() method then it will go into the infinite loop and system will generate error. If you want to modify the same entity then instead of retrieving the entity you can register your plugin in Pre-Operation stage to get the entity from the context and then can update the entity. i.e.

    // Obtain the target entity from the input parameters.

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

    //instead of calling service.update, update the entity as mentioned below

    entity.Attributes["new_c"] = (double)c;

     


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Monday, September 5, 2011 6:42 AM
  • Hi Javeed,

    I am trying now only update event. I changed the code according to your suggestion and registered the plugin again. I am getting still error.

    Message: Update, Event Pipeline: Pre-OPeration,  Execution Mode: Synchronous;  Execution Order: 1

    CODE:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                tracer.Trace("ENTER:Values.Execute");

                Entity entity;

                if (context.InputParameters.Contains("Target") &&
                            context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target business entity from the input parameters.
                    entity = (Entity)context.InputParameters["Target"];

                    // Verify that the entity represents a contact.
                    if (entity.LogicalName != "new_test") { return; }
                }
                else
                {
                    return;
                }

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

                         
             //Entity member = service.Retrieve("new_test", (System.Guid) targetEntity["new_testid"] , new ColumnSet("new_a","new_b","new_c"));
                       
                       tracer.Trace("  Required fields present.");
                  
                           Double a = ((Double)entity["new_a"]);
                           Double b = ((Double)entity["new_b"]);
                           Double c = (a + b);
                           entity.Attributes["new_c"] = (double)c;
                    }
                   catch (FaultException<OrganizationServiceFault> ex)
                   {
                       throw new InvalidPluginExecutionException(String.Concat("An error occurred.Update plug-in.", ex.Message, Environment.NewLine, ex.StackTrace));
                   }
                   catch (Exception ex)
                   {
                       throw new InvalidPluginExecutionException(String.Concat("An error occurred.Update plug-in.", ex.Message, Environment.NewLine, ex.StackTrace));
                   }

    Error:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred.Update plug-in.The given key was not present in the dictionary.
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)Detail:
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220891</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
        <KeyValuePairOfstringanyType>
          <d2p1:key>OperationStatus</d2p1:key>
          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
        </KeyValuePairOfstringanyType>
      </ErrorDetails>
      <Message>An error occurred.Update plug-in.The given key was not present in the dictionary.
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at TestPlugin.PluginUpdateTest.Execute(IServiceProvider serviceProvider)</Message>
      <Timestamp>2011-09-05T12:41:16.6025933Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText>

    [TestPlugin: TestPlugin.PluginUpdateTest]
    [c2e85996-bbd7-e011-b7a1-00155dc8c90d: TestPlugin.PluginUpdateTest: Update of new_test]
    ENTER:Values.Execute
      Required fields present.bb38979d-8dd7-e011-b7a1-00155dc8c90d

    </TraceText>
    </OrganizationServiceFault>

     


    • Edited by crmpnk1 Monday, September 5, 2011 12:45 PM
    Monday, September 5, 2011 12:45 PM
  • Monday, September 5, 2011 2:59 PM
  • nagcrm,

    For your plugin registered in preOperation, Update, retrieve the current updated entity by using the following line:

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

    This entity contains only the key attribute and the attributes updated (ie. if you updated just A, B is not present).

    To be sure to retrieve A and B, add an image to your plugin step and retrieve the image by using following line:

    Entity preData = (Entity)context.preEntityImages["<The name of the preImage>"];

    This entity contains all data on the database of the entity before you triggered the save event.

    With these elements, you can write the following plugin:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    tracer.Trace("ENTER:Values.Execute");
    
    Entity updatedData = (Entity)context.InputParameters["Target"];
    if (updatedData.LogicalName != "new_test") { return; }
    
    Entity preData = (Entity)context.PreEntityImages["TEST"];
    
    double a = 0;
    double b = 0;
    
    // Checks if the attribute "a" was updated in the form
    if (updatedData.Contains("new_a"))
    {
           a = ((Double)updatedData["new_a"]);
    }
    // Checks if the attribute "a" was already present
    else if (preData.Contains("new_a"))
    {
            a = ((Double)preData["new_a"]);
    }
    
    // Checks if the attribute "b" was updated in the form
    if (updatedData.Contains("new_b"))
    {
            b = ((Double)updatedData["new_b"]);
    }
    // Checks if the attribute "b" was already present
    else if (preData.Contains("new_b"))
    {
            b = ((Double)preData["new_b"]);
    }
    
    // Adds the calculation, no need to update the entity, as 
    // we are in preUpdate stage
    updatedData["new_c"] = (double)(a + b);
    


    My blog : http://mscrmtools.blogspot.com

    All my tools for Dynamics CRM 4.0 on my dedicated site: MSCRMTools Repository

    • Marked as answer by crmpnk1 Monday, September 5, 2011 4:57 PM
    Monday, September 5, 2011 3:26 PM
    Moderator
  • Hi Tanguy T

    Thank you very much. It's working perfectly. I have one more concern here. If I want to calculate on CREATE Event (including UPDATE), Do i have to modify same code and register two steps  (one for Create another one for Update)?

    or else I need to create different Plugin for that?

    BR,

    NAG.


    • Edited by crmpnk1 Monday, September 5, 2011 4:58 PM
    Monday, September 5, 2011 4:56 PM
  • Hi,

    You can use the same code for Create message in Pre-Operation stage,  i have explained before the same to update the entity attributes directly instead of calling Service.Update (...);


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Monday, September 5, 2011 5:50 PM
  • Hi,

    On the same plugin (used Tanguy code) i added one more step for Create Message Pre-OPeration stage, But If i am trying to add the Pre-image (Name:TEST, Alias: TEST) for Create Message it's giving an error on plugin-registration tool itself. It might be the reason i already used TEST for Update Message Pre-Image.So removed only Pre-Image for Create and tested it's giving an below error on Creation. But still Update operation is fine.

    Error:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: The given key was not present in the dictionary.Detail:
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220891</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
        <KeyValuePairOfstringanyType>
          <d2p1:key>OperationStatus</d2p1:key>
          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
        </KeyValuePairOfstringanyType>
      </ErrorDetails>
      <Message>The given key was not present in the dictionary.</Message>
      <Timestamp>2011-09-06T06:53:51.450572Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText>

    [TestPlugin: TestPlugin.PluginUpdateTest]
    [d849c5d2-53d8-e011-b7a1-00155dc8c90d: TestPlugin.PluginUpdateTest: Create of new_test]
    ENTER:Values.Execute

    </TraceText>
    </OrganizationServiceFault>

     


    • Edited by crmpnk1 Tuesday, September 6, 2011 6:48 AM
    Tuesday, September 6, 2011 6:47 AM
  • Dear All,

    I was developing a small Plugin. My goal is I want to add two fields(a,b) and display it on third field(c) whenever i CREATE/UPDATE on the entity. After I started i got many errors,  with the help of above forum, UPDATE message is working perfectly. But I want same thing on CREATE message also. To achieve CREATE message, what needs to be done exactly?

    For this a) do i have to use the same code and  add new step for CREATE under same plugin?

    b) or do i have to use new code and register as new plugin ?

    Current Code for UPDATE Message:

    REgistered Plugin Name: TestPlugin

    Under this added new step, and Pre-Image (TEST).

    Message: Update, Event Pipeline: Pre-OPeration,  Execution Mode: Synchronous;  Execution Order: 1

    CODE:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    tracer.Trace("ENTER:Values.Execute");
    
    Entity updatedData = (Entity)context.InputParameters["Target"];
    if (updatedData.LogicalName != "new_test") { return; }
    
    Entity preData = (Entity)context.PreEntityImages["TEST"];
    
    double a = 0;
    double b = 0;
    
    // Checks if the attribute "a" was updated in the form
    if (updatedData.Contains("new_a"))
    {
           a = ((Double)updatedData["new_a"]);
    }
    // Checks if the attribute "a" was already present
    else if (preData.Contains("new_a"))
    {
            a = ((Double)preData["new_a"]);
    }
    
    // Checks if the attribute "b" was updated in the form
    if (updatedData.Contains("new_b"))
    {
            b = ((Double)updatedData["new_b"]);
    }
    // Checks if the attribute "b" was already present
    else if (preData.Contains("new_b"))
    {
            b = ((Double)preData["new_b"]);
    }
    
    // Adds the calculation, no need to update the entity, as 
    // we are in preUpdate stage
    updatedData["new_c"] = (double)(a + b);
    
    BR,
    Nag.
    Wednesday, September 7, 2011 6:02 AM