locked
MS CRM DateTime field throws an error when date is already present (c# & MVC). RRS feed

  • Question

  • Hi,

    I'm having an issue when trying to programmatically update a DateTime field that already has a date stored in it.

    The error that i'm getting is:

    System.InvalidOperationException: There was an error generating the XML document. ---> System.ArgumentException: Value of type 'CrmDateTimeProperty' is not supported. Parameter name: value

    This is the code that performs the CRM update:

     public bool UpdatePromptList(PromptList list) 
       
    { 
           
    DynamicEntity c = crmService.RetrieveDynamicEntity("ntup1_promptlist", list.PromptListId); 
     
           
    if (c != null) 
           
    { 
                c
    .UpdateBoolean("examplebooleanfield", list.booleanField); 
                c
    .UpdateDateTime("exampledatefield", list.dateField); 
                c
    .UpdateString("examplestringfield", list.stringField);      
     
               
    try 
               
    { 
                    crmService
    .Update(c); 
               
    } 
               
    catch (SoapException) 
               
    { 
                   
    return false; 
               
    }  
           
    } 
         
    } 

    And here is the code that validates the DateTime value to be passed into CRM:

     

     public static CrmDateTime FromUser(DateTime userTime) 
       
    { 
           
    return new CrmDateTime(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:s}", userTime)); 
       
    } 
     
     
       
    public static void UpdateDateTime(this DynamicEntity entity, string property, DateTime? date) 
       
    { 
           
    if (date == null) 
           
    { 
               
    if (entity.Properties.Contains(property)) 
               
    { 
                    entity
    .Properties.Remove(property); 
               
    } 
               
    return; 
           
    } 
     
           
    CrmDateTime crmDate = FromUser(date.Value); 
           
    CrmDateTimeProperty crmProp = new CrmDateTimeProperty(property, crmDate); 
     
           
    if (entity.Properties.Contains(property)) 
           
    { 
                entity
    .Properties[property] = crmProp; 
           
    } 
           
    else 
           
    { 
                entity
    .Properties.Add(crmProp);  
           
    } 
       
    } 

    Any help on this would be greatly appreciated.

    Thursday, May 26, 2011 2:41 PM

Answers

  • I usually do the following formatting, it has worked for me.

        public static CrmDateTime FromUser(DateTime userTime)
        {
          return new CrmDateTime(userTime.ToString("yyyy-MM-ddTHH:mm:ss"));
        } 
    
    

    But if it fails for you (it should be related to the locale setting of the server if it does fail), you will just have to obey what CRM asks for. Probably something like this.

        public static CrmDateTime FromUser(DateTime userTime)
        {
          return new CrmDateTime(userTime.ToString("yyyy/MM/ddTHH:mm:ss"));
        } 
    
    

     


    Daniel Cai | http://danielcai.blogspot.com
    Friday, May 27, 2011 2:00 PM

All replies

  • Try:

      public static void UpdateDateTime(this DynamicEntity entity, string property, DateTime? date) 
      { 
        if (date == null) 
        { 
          if (entity.Properties.Contains(property)) 
          { 
            entity.Properties.Remove(property); 
          } 
          return; 
        } 
     
        CrmDateTime crmDate = FromUser(date.Value); 
     
        if (entity.Properties.Contains(property)) 
        { 
          entity.Properties[property] = crmDate; 
        } 
        else 
        { 
          CrmDateTimeProperty crmProp = new CrmDateTimeProperty(property, crmDate); 
          entity.Properties.Add(crmProp); 
        } 
      } 
    
    
    
    

    Daniel Cai | http://danielcai.blogspot.com
    Thursday, May 26, 2011 3:08 PM
  • Hi,

     

    Thank you for your reply.  Unfortunately, i'm still experiencing the same error.  If there is any more information that you need, let me know and I will post it.

     

    Thanks again.

    Thursday, May 26, 2011 6:38 PM
  • That's very strange. Which branch has been executed?

     entity.Properties[property] = crmDate; 
    or 
     CrmDateTimeProperty crmProp = new CrmDateTimeProperty(property, crmDate); 
     entity.Properties.Add(crmProp); 


    Daniel Cai | http://danielcai.blogspot.com
    Friday, May 27, 2011 1:31 AM
  • It was:

    entity.Properties[property] = crmProp;

     

    Thanks.

    Friday, May 27, 2011 6:45 AM
  • I've just noticed that 'crmDate' has an exception on the UniversalTime property: "

    UniversalTime = base {System.SystemException} = {"CrmDateTime must be initialized in one of the following formats: yyyy/MM/ddTHH:mm:ss[+-]aa:bb or yyyy/MM/ddTHH:mm:ssZ"}"

    The value within the 'Value' property is "2011-05-27T11:25:58" so that seems to be the problem.  The only thing that I don't understand now is why it is being generated in the wrong format.

    Thanks.

    Friday, May 27, 2011 10:30 AM
  • I usually do the following formatting, it has worked for me.

        public static CrmDateTime FromUser(DateTime userTime)
        {
          return new CrmDateTime(userTime.ToString("yyyy-MM-ddTHH:mm:ss"));
        } 
    
    

    But if it fails for you (it should be related to the locale setting of the server if it does fail), you will just have to obey what CRM asks for. Probably something like this.

        public static CrmDateTime FromUser(DateTime userTime)
        {
          return new CrmDateTime(userTime.ToString("yyyy/MM/ddTHH:mm:ss"));
        } 
    
    

     


    Daniel Cai | http://danielcai.blogspot.com
    Friday, May 27, 2011 2:00 PM
  • It's working now.  Thank you so much for your help :).

    Monday, May 30, 2011 6:20 PM