locked
How to test if a custom account field is empty? RRS feed

  • Question

  •  

    Hi!

     

    I've addes some new fields to my account entity, and now I'm trying to access them in my plug-in, but it seems that empty fields cause my code to throw a weird exception: "The given key was not in the dictionary." First I thought it couldn't find the field due to case sensitivity, but that should have been handled in my code and I should have seen my own error message, but I did not... Then I tried encasing the reading of that field in a try\catch and specifically catch any exceptions to confirm to myself that it really was the reading of the property that caused the exception, but again I got the same error... Here is my code:

     

    Code Snippet

      //Validate Entity (Returns blank string if successfull)
      private string ValidateEntity(DynamicEntity entity)
      {
       try
       {
        // Debug
        #if(DEBUG)
         this.WriteDebugLine("TradingCRMService.Plugin.ValidateEntity() started...");
        #endif

        // Verify that the entity represents an account.
        if (entity.Name != EntityName.account.ToString())
        {
         throw new Exception("The plug-in was invoked for an entity that is not supported! Supported entity is Account. Action attempted on an entity of type " + entity.Name);
        }

     

        // Verify that the entity has a Sync property
        if (entity.Properties["new_sync"] == null)
        {
         throw new Exception("The plug-in was invoked for an entity that does not have a Sync property.");
        }

        // Get the value of the Sync property
        CrmBoolean SyncValue = (CrmBoolean)entity.Properties["new_sync"];

        // Debug
        #if(DEBUG)
         this.WriteDebugLine("Sync = " + SyncValue.Value.ToString());
        #endif

     

        // Verify that the entity has an Account Name property
        if (entity.Properties["name"] == null)
        {
         throw new Exception("The plug-in was invoked for an entity that does not have an Account Name property.");
        }

        // Get the value of the Account Name property
        string AccountName = entity.Properties["name"].ToString();

        // Debug
        #if(DEBUG)
         this.WriteDebugLine("Account Name = " + AccountName);
        #endif

     

        // Verify that the entity has a Klientnr property
        if (entity.Properties["new_klientnr"] == null)
        {
         throw new Exception("The plug-in was invoked for an entity that does not have a Klientnr property.");
        }

        // Get the value of the Klientnr property
        string KlientNr = entity.Properties["new_klientnr"].ToString();
        
        // Debug
        #if(DEBUG)
         this.WriteDebugLine("Klientnr = " + KlientNr);
        #endif

     

        // Verify that the entity has a Kunde property
        if (entity.Properties["new_Kunde"] == null)
        {
         throw new Exception("The plug-in was invoked for an entity that does not have a Kunde property.");
        }

        // Get the value of the Kunde property
        CrmBoolean Kunde = (CrmBoolean)entity.Properties["new_Kunde"];

        // Debug
        #if(DEBUG)
         this.WriteDebugLine("Kunde = " + Kunde.Value.ToString());
        #endif

     

        // Verify that the entity has a Leverandør property
        if (entity.Properties["new_Leverandor"] == null)
        {
         throw new Exception("The plug-in was invoked for an entity that does not have a Leverandør property.");
        }

        // Get the value of the Leverandør property
        CrmBoolean Leverandør = (CrmBoolean)entity.Properties["new_Leverandor"];

        // Debug
        #if(DEBUG)
         this.WriteDebugLine("Leverandør = " + Leverandør.Value.ToString());
        #endif

     

        // Verify that the entity has a Transportør property
        if (entity.Properties["new_Transportor"] == null)
        {
         throw new Exception("The plug-in was invoked for an entity that does not have a Transportør property.");
        }

        // Get the value of the Transportør property
        CrmBoolean Transportør = (CrmBoolean)entity.Properties["new_Transportor"];

        // Debug
        #if(DEBUG)
         this.WriteDebugLine("Transportør = " + Transportør.Value.ToString());
        #endif

     

        // Verify that the entity has a Valutakode property
        if (entity.Properties["new_Valutakode"] == null)
        {
         throw new Exception("The plug-in was invoked for an entity that does not have a Valutakode property.");
        }

        // Get the value of the Valutakode property
        string Valutakode = entity.Properties["new_Valutakode"].ToString();

        // Debug
        #if(DEBUG)
         this.WriteDebugLine("Valutakode = " + Valutakode);
        #endif

     

        // Validation was successfull, return an empty string
        return string.Empty;
       }
       catch (Exception ex)
       {
        string debugTekst = "TradingCRMService.Plugin.ValidateEntity() threw an exception: " + ex.Message + " StackTrace: " + ex.StackTrace;
        string strTekst = "TradingCRMService.Plugin.ValidateEntity() threw an exception: " + ex.Message;
        this.WriteDebugLine(debugTekst);
        return strTekst;
       }
       finally
       {
        // Debug
        #if(DEBUG)
         this.WriteDebugLine("TradingCRMService.Plugin.ValidateEntity() completed...");
        #endif
       }

      }

     

     

    My log shows this:

    [05:25:06 - 4644] TradingCRMService.Plugin.Execute() started...
    [05:25:06 - 4744] Plug-in context: Create
    [05:25:06 - 4744] TradingCRMService.Plugin.ValidateEntity() started...
    [05:25:06 - 4744] Sync = True
    [05:25:06 - 4744] Account Name = Dummy Trading Customer
    [05:25:06 - 4744] TradingCRMService.Plugin.ValidateEntity() threw an exception: The given key was not present in the dictionary. StackTrace:    at System.ThrowHelper.ThrowKeyNotFoundException()
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at Microsoft.Crm.Sdk.PropertyCollection.get_Item(String propertyName)
       at TradingCRMService.Plugin.ValidateEntity(DynamicEntity entity)
    [05:25:06 - 4744] TradingCRMService.Plugin.ValidateEntity() completed...
    [05:25:06 - 4744] En uventet feil oppstod under kjøring av CRM 4.0 Maritech Trading Plug-in! Melding: The target entity did not pass validation. TradingCRMService.Plugin.ValidateEntity() threw an exception: The given key was not present in the dictionary.
    [05:25:06 - 4744] TradingCRMService.Plugin.Execute() completed...

     

    So clearly it fails after reading Account Name. Ie. it fails when trying to read new_klientnr. The error goes away if I put some text in the new_klientnr field, but I want to be able to detect if this field is empty so I can tell the user WHY this operation failed (because of an empty field, not because a "given key was not in the dictionary". WTF is the word dictionary doing there in the first place?) Is there a way of checking if a field is empty?

    Monday, April 28, 2008 12:34 PM

Answers

  • Hi,

     

    You have to use Contains() method of the Properties object to check if the key exist is contained in the Properties because the fields without value (null) are not included in the properties dictionary. That's the reason why you get that exception in your code when you try to get the value of a property with a key that is not included in the dictionary.

     

    So, instead of

     

    Code Snippet
    if (entity.Properties["new_klientnr"] == null)

     

     

     

    You should use

     

    Code Snippet
    if (!entity.Properties.Contains("new_klientnr")

     

     

     

    Hope it helps,


    Marco Amoedo

    Monday, April 28, 2008 3:31 PM
    Moderator