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
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
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.
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.
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
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.
Is it possible to check if the field is read only or not?
I tried it and it threw the exception "Specified cast is not valid."
Thank you for the response
Can you try this.
entity.Attributes["datekilled"] =DateTime.UtcNow; (attribute name should be small case )
Thanks,
Saroj
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156