Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Updating a date field in Post Update plug-in step throws invalid cast operation

Posted on by Microsoft Employee

Greetings,

In a CRM 2013 on-premise plug-in post operation step when I am trying to update a DateTime field in an opportunity record after the opportunity has been closed as Lost for status, an exception of "Invalid cast operation" is thrown no matter what  DateTime value format is assigned to the field:

Examples:

entity.Attributes["DateKilled"] = entity.GetAttributesValue<DateTime>("DateClosed");  // DateKilled is blank

or

entity.Attributes["DateKilled"] = entity.GetAttributesValue<DateTime>("DateClosed").ToShortDateString(); // DateClosed is populated

or

entity.Attributes["DateKilled"] = new DateTime(2016, 09, 15);

The exception is thrown no matter what format or type is assigned.

Is there a different way to update date type fields in CRM?

Thanks in advance for any help or suggestions.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updating a date field in Post Update plug-in step throws invalid cast operation

    Hi,

    Just to test I used DateTime.Now but still threw the exception. As a recommended approach, in the plug-in step, I limited the attributes to the two fields involved (DateKilled and DateClosed) and the exception went away, it had nothing to do with what I was doing. Through trial and error I narrowed them down to two fields, statecode and status_code, which caused the exception but I did go further to determine which one was the culprit. There are other plug-ins that fire on the Update event as well and I wonder if that has anything to do with it.

    After resolving that issue the new value for the DateKilled did not get saved so after assigning the new date I executed the contex.Update(entity) but it caused an infinite loop. Then I found  if (context.Depth <= 1) {context.Update(entity);} which solved this issue.

    I still have one more issue and that is to execute the DateKilled update if the statecode value is greater 0 and DateKilled is null and the statecode value appears to be 0 (zero) still within the Update post-event. As for the null value for the DateKilled it looks like null dates are represented as a value like 1/1/0001 12:00a.

    Thanks

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: Updating a date field in Post Update plug-in step throws invalid cast operation

    Hi,

    Still you are facing the issue? May I know what is the date format of DateKilled Field Date?

    Cast Operation error is because of wrong date formation.

    Thanks.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updating a date field in Post Update plug-in step throws invalid cast operation

    Sorry my bad, the exception is back again and says the "specified cast is invalid".  It was working but just not saving the update so I added service.Update(entity) which caused an infinite loop. I removed the service.Update(entity) but the error came back. I don't know what happened and why it worked for a while.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updating a date field in Post Update plug-in step throws invalid cast operation

    I don't have remote debugging running on the server to debug but I verified the correct field names. I was researching this yesterday and I came across a different notation for assigning a value to the datedead field which I think has to do with late binding as you mentioned earlier as follows:

    instead of using :

        entity.Attributes["datekilled"] =DateTime.Now;

    I used:

        entity["datekilled"] = DateTime.Now;

    and it worked and did not throw the invalid cast operation exception anymore. It did not however save the data ,maybe because its a post update operation, but I verified the datedead field was updated by throwing an exception using the following code:

    var DateDead = entity.GetAttributeValue<DateTime>("datedead");

    throw new InvalidPluginExecutionException("DateDead =" + DateDead);

    and it seemed to have the correct data.

    So I guess what I need to do now is to explicitly save the record to make the changes persistent but I am a little confused on how to go about it.

    Would appreciate any suggestions.

    Thanks

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updating a date field in Post Update plug-in step throws invalid cast operation

    If you are debug the plugin, try to check  if entity.Attributes["DateKilled"] is null or not.

    Since it is the late bound and the field name may be not correct, I always use below method to confirm the field name should use:

    Use Chrome to open the "hostname"/XRMServices/2011/OrganizationData.svc/OpportunitySet and then you can find the correct string for Datekilled and DateClosed.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updating a date field in Post Update plug-in step throws invalid cast operation

    Is it possible to check if the field is read only or not?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updating a date field in Post Update plug-in step throws invalid cast operation

    I tried it and it threw the exception "Specified cast is not valid."

    Thank you for the response

  • Suggested answer
    Saroj Das Profile Picture
    Saroj Das 3,355 on at
    RE: Updating a date field in Post Update plug-in step throws invalid cast operation

    Can you try this.

    entity.Attributes["datekilled"] =DateTime.UtcNow; (attribute name should be small case )

    Thanks,

    Saroj

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans