locked
The given key was not present in the dictionary RRS feed

  • Question

  • Hi,

    I am tring to update the quote entity with my own id sequence. The field 'quotenumber' is to be updated upon creation of the entity by the useof a plugin. However, when I save the new quote i get the message: "The given key was not present in the dictiobnary"

    The plugin is designed to:l

    1. Get the business unit of the newly created quote

    2. Do a lookup for a record on held by another entity containing the current quote sequnce number based on the business unit

    3. Increment the sequence number by one and then update that record containing the current sequence number

    4. Update the newly created quote record with the increemented quote number

    Here is my code bvelow in c#:

    using System;
    using System.IO;
    using System.Net;
    using System.Xml;
    using System.Web.Services.Protocols;
    
    using System.IO;
    using Microsoft.Crm.Sdk;
    
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;
    
    
    namespace KFPlugins 
    {
      public class GetQuoteNumber : IPlugin
      {
             
    
        public void Execute(IPluginExecutionContext context)
        {
          DynamicEntity entity = null;
          if (context.InputParameters.Properties.Contains("Target") && context.InputParameters.Properties["Target"] is DynamicEntity)
          {
            entity = (DynamicEntity)context.InputParameters.Properties["Target"];
    
            if (entity.Name != EntityName.quote.ToString()) { return; }
          }
          else
          {
            return;
          }
    
          try
          { 
            ICrmService service = context.CreateCrmService(true);
    
            //1. Get the business unit of the newly created quote
    
            Guid busid = new Guid(context.OutputParameters.Properties["owningbusinessunit"].ToString());
            TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();
            targetRetrieve.EntityName = EntityName.businessunit.ToString();
            targetRetrieve.EntityId = busid;
            RetrieveRequest retrieve = new RetrieveRequest();
            retrieve.Target = targetRetrieve;
            retrieve.ColumnSet = new AllColumns();
            retrieve.ReturnDynamicEntities = true;
            
            RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);
    
            DynamicEntity busunit = (DynamicEntity)retrieved.BusinessEntity;
            string busunitname = busunit.Properties["name"].ToString();
            StreamWriter printout = new StreamWriter("C:\\temp\\printout.txt");
            printout.WriteLine("Business unit is " + busunitname);
            printout.Flush();
    
    
            //2.Do a lookup for a record on held by another entity containing the current quote sequnce number based on the business unit
            
            
            ConditionExpression con = new ConditionExpression();
            con.AttributeName = "new_name";
            con.Operator = ConditionOperator.Equal;
            con.Values = new string[] { busunitname };
            FilterExpression filter = new FilterExpression();
            filter.FilterOperator = LogicalOperator.And;
            filter.AddCondition(con);
            QueryExpression query = new QueryExpression();
            query.EntityName = "new_quotetrackingid";
            query.ColumnSet = new AllColumns();
            query.Criteria = filter;
            RetrieveMultipleRequest retrieveid = new RetrieveMultipleRequest();
            retrieveid.Query = query;
            retrieveid.ReturnDynamicEntities = true;
            RetrieveMultipleResponse retrievedid = (RetrieveMultipleResponse)service.Execute(retrieveid);
    
            DynamicEntity seqnumberrecord = (DynamicEntity)retrievedid.BusinessEntityCollection.BusinessEntities[0];
            string locationcode = seqnumberrecord.Properties["new_locationcode"].ToString();
            printout.WriteLine("location code is " + locationcode);
            printout.Flush();
            string quoteseqid = seqnumberrecord.Properties["new_id"].ToString();
            printout.WriteLine("Quote id is " + quoteseqid);
            printout.Flush();
            
    
    //3. Increment the sequence number by one and then update that record containing the current sequence number
    
            
            Int32 updatequoteseqid = Convert.ToInt32(quoteseqid) + 1;
    
            printout.WriteLine("Updated quote id is " + updatequoteseqid);
            printout.Flush();
    
            seqnumberrecord.Properties.Add(new StringProperty("new_id", updatequoteseqid.ToString()));
            TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();
            updateDynamic.Entity = seqnumberrecord;
            UpdateRequest update = new UpdateRequest();
            update.Target = updateDynamic;
            UpdateResponse updated = (UpdateResponse)service.Execute(update);
    
            //4. Update the newly created quote record with the increemented quote number
    
            string newquoteid = "Q" + locationcode + updatequoteseqid.ToString();
            printout.WriteLine("Quote id is " + newquoteid);
            printout.Flush();
            printout.Close();
    
            entity.Properties.Add(new StringProperty("quotenumber", newquoteid));
            TargetUpdateDynamic updateDynamicDeal = new TargetUpdateDynamic();
            updateDynamicDeal.Entity = entity;
            UpdateRequest updateDeal = new UpdateRequest();
            updateDeal.Target = updateDynamicDeal;
            UpdateResponse updatedDeal = (UpdateResponse)service.Execute(updateDeal);
          }
          catch (System.Web.Services.Protocols.SoapException ex)
          {
            throw new InvalidPluginExecutionException(" An error occured in the plugin.", ex);
          }
    
    
        
    
        
    
        }
      
      }
    }
    
    

     

     

    Tuesday, October 5, 2010 6:08 AM

Answers

  • You can not update 'quotenumber' because it is not valid for update. Check the SDK for the same.

     Try following workarounds :

    1. I think you'ld try it with 'Pre-event plug-in'. It might work because it is valid for create.

    2. Add custom attribute to maintain Quote Number as per you business logic.

    Thank you.

    JayshriP

    • Proposed as answer by JayshriP Wednesday, October 6, 2010 8:21 AM
    • Edited by JayshriP Wednesday, October 6, 2010 8:23 AM workaround to try
    • Marked as answer by Stanley_Lai Thursday, October 7, 2010 12:16 AM
    Wednesday, October 6, 2010 8:20 AM

All replies

  • make sure you are using correct field names.

    Could you please provide plugin registeration information ???


    Mahain : http://mahenderpal.wordpress.com
    Tuesday, October 5, 2010 6:44 AM
    Moderator
  • Hi.

     

    I don't think that following line will work:

    Guid busid = new Guid(context.OutputParameters.Properties["owningbusinessunit"].ToString());

     

    to get business unit identifier you should try to use following code:

    Guid busid = ((Lookup)entity["owningbusinessunit"]).Value;

     

    Also this is very hard to debug such code in the head. You should debug this code on the server.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Tuesday, October 5, 2010 7:35 AM
    Moderator
  • Hi I have progressed on this one and discovered that I need to register a postImage.

    All the code works until the very last part of updating the newly creaed quote entity. Do I need to change some settings on the plugin registration tool?

    Mty registration settings are:

    Message: Create

    Primary Entity: quote

    Secondary Entity: none

    Filtering Attributes: All attributes

    Plugin: (Plugin) KFPlugins.GetQuoteNumber

    Run in User's Context: Calling User

    Execution Order: 1

    Stage of Execution: Post Stage

    Execution Mode: Synchronous

    Triggering Pipeline: Parent

    Step Deployment: Server

     

    My code is :

    using System;
    using System.IO;
    using System.Net;
    using System.Xml;
    using System.Web.Services.Protocols;
    
    using Microsoft.Crm.Sdk;
    
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;
    
    
    namespace KFPlugins 
    {
      public class GetQuoteNumber : IPlugin
      {
             
    
        public void Execute(IPluginExecutionContext context)
        {
          DynamicEntity entity = null;
          
          Guid busunitid = new Guid();
          
          entity = (DynamicEntity)context.PostEntityImages.Properties["QuoteImage"];
    
          try
          {
            //Get Business unit of quote
           
            StreamWriter printout = new StreamWriter("C:\\temp\\printout.txt");
            if (entity.Properties.Contains("owningbusinessunit"))
            {
    
               busunitid = ((Lookup)entity.Properties["owningbusinessunit"]).Value;
              //busunit = busunitid.ToString();
            }
              
            ICrmService service = context.CreateCrmService(true);
    
            TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();
            targetRetrieve.EntityName = EntityName.businessunit.ToString();
            targetRetrieve.EntityId = busunitid;
            RetrieveRequest retrieve = new RetrieveRequest();
            retrieve.Target = targetRetrieve;
            retrieve.ColumnSet = new AllColumns();
            retrieve.ReturnDynamicEntities = true;
            
            RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);
    
            DynamicEntity busunitentity = (DynamicEntity)retrieved.BusinessEntity;
            string busunitname = busunitentity.Properties["name"].ToString();
    
            //Find the sequence number for the quote create based on franchise
            
            
            ConditionExpression con = new ConditionExpression();
            con.AttributeName = "new_name";
            con.Operator = ConditionOperator.Equal;
            con.Values = new string[] { busunitname };
            FilterExpression filter = new FilterExpression();
            filter.FilterOperator = LogicalOperator.And;
            filter.AddCondition(con);
            QueryExpression query = new QueryExpression();
            query.EntityName = "new_quotetrackingid";
            query.ColumnSet = new AllColumns();
            query.Criteria = filter;
            RetrieveMultipleRequest retrieveid = new RetrieveMultipleRequest();
            retrieveid.Query = query;
            retrieveid.ReturnDynamicEntities = true;
            RetrieveMultipleResponse retrievedid = (RetrieveMultipleResponse)service.Execute(retrieveid);
    
            DynamicEntity seqnumberrecord = (DynamicEntity)retrievedid.BusinessEntityCollection.BusinessEntities[0];
            string locationcode = seqnumberrecord.Properties["new_locationcode"].ToString();
            
            string quoteseqid = seqnumberrecord.Properties["new_id"].ToString();
            
            //Increment tracking quote id and update tracking table
    
            Int32 updatequoteseqid = Convert.ToInt32(quoteseqid) + 1;
            char c = '0';
            string paddednum = updatequoteseqid.ToString().PadLeft(6, c);
    
            seqnumberrecord.Properties.Add(new StringProperty("new_id", updatequoteseqid.ToString()));
            TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();
            updateDynamic.Entity = seqnumberrecord;
            UpdateRequest update = new UpdateRequest();
            update.Target = updateDynamic;
            UpdateResponse updated = (UpdateResponse)service.Execute(update);
    
            //Update quote record
            string newquoteid = "Q" + locationcode + paddednum;
            printout.WriteLine("Quote id is " + newquoteid);
            printout.Flush();
            printout.Close();
    
    
            //=============================================================
            // QUOTE DOES NOT UPDATE WITH NEW QUOTE ID NUMBER
            //=============================================================== 
            entity.Properties.Add(new StringProperty("quotenumber", newquoteid));
            
            TargetUpdateDynamic updateDynamicQuote = new TargetUpdateDynamic();
            updateDynamicQuote.Entity = entity;
            UpdateRequest updateQuote = new UpdateRequest();
            updateQuote.Target = updateDynamicQuote;
            UpdateResponse updatedQuote = (UpdateResponse)service.Execute(updateQuote);
            //=====================================================================
            //END
            //=====================================================================
          }
          catch (System.Web.Services.Protocols.SoapException ex)
          {
            throw new InvalidPluginExecutionException(" An error occured in the plugin.", ex);
          }
    
    
        
    
        
    
        }
      
      }
    }
    
    
    Wednesday, October 6, 2010 6:26 AM
  • You can not update 'quotenumber' because it is not valid for update. Check the SDK for the same.

     Try following workarounds :

    1. I think you'ld try it with 'Pre-event plug-in'. It might work because it is valid for create.

    2. Add custom attribute to maintain Quote Number as per you business logic.

    Thank you.

    JayshriP

    • Proposed as answer by JayshriP Wednesday, October 6, 2010 8:21 AM
    • Edited by JayshriP Wednesday, October 6, 2010 8:23 AM workaround to try
    • Marked as answer by Stanley_Lai Thursday, October 7, 2010 12:16 AM
    Wednesday, October 6, 2010 8:20 AM
  • Can you post the soap exception here. You will get it using ex.Detail.InnerXml.

    Regards,

    Amol

    Wednesday, October 6, 2010 8:36 AM
  • You can not update 'quotenumber' because it is not valid for update. Check the SDK for the same.

     Try following workarounds :

    1. I think you'ld try it with 'Pre-event plug-in'. It might work because it is valid for create.

    2. Add custom attribute to maintain Quote Number as per you business logic.

    Thank you.

    JayshriP

    Great this worked ! . I just used the 'name' field instead.

    Thank you so much for the help guys !

    Wednesday, October 6, 2010 11:24 PM