Invalid Argument – Microsoft.Crm.ObjectModel. TargetAttributeValidationPlugin in Dynamics 365
While you are working on Plugins you may face such kind of exception so in this post I have explained about the solution and Root Cause.
ERROR YOU GET
Exception Message: Incorrect type of attribute value System.Int32
ErrorCode: -2147220989
HexErrorCode: 0x80040203
ErrorDetails:
ApiExceptionSourceKey: Plugin/Microsoft.Crm.ObjectModel.TargetAttributeValidationPlugin
ApiStepKey: 92cd503b-c175-eb11-a812-002248097506
ApiDepthKey: 1
ApiActivityIdKey: 28ef67fe-5899-4a8a-8cf5-05cc9a3b3d85
ApiPluginSolutionNameKey: System
ApiStepSolutionNameKey: System
ApiExceptionCategory: ClientError
ApiExceptionMesageName: InvalidArgument
ApiExceptionHttpStatusCode: 400
HelpLink: http://go.microsoft.com/fwlink/?LinkID=398563&error=Microsoft.Crm.CrmException%3a80040203&client=platform
TraceText:
[Softchief Plugins Samples: Softchief_Plugins_Samples.UpdateAgebyDOB]
[92cd503b-c175-eb11-a812-002248097506: Softchief_Plugins_Samples.UpdateAgebyDOB: Update of contact]
Activity Id: cfb6118b-7fe2-456a-9326-15dc121cf614
Exception Message: An unexpected error occurred.
ErrorCode: -2147220970
HexErrorCode: 0x80040216
ErrorDetails:
ApiExceptionSourceKey: Plugin/Microsoft.Crm.Common.ObjectModel.ContactService
ApiStepKey: c5cdbb1b-ea3e-db11-86a7-000a3a5473e8
ApiDepthKey: 1
ApiActivityIdKey: 3bda0c6a-9cc7-443e-a673-ffbf1b9ba414
ApiPluginSolutionNameKey: System
ApiStepSolutionNameKey: System
ApiExceptionCategory: SystemFailure
ApiExceptionMesageName: UnExpected
ApiExceptionHttpStatusCode: 400
HelpLink: http://go.microsoft.com/fwlink/?LinkID=398563&error=Microsoft.Crm.CrmException%3a80040216&client=platform
Activity Id: cc56e29f-c458-4183-8e49-c375d1318a42
SOLUTION
The ROOT Cause of this error is when Dynamics 365 is Expecting a Different Data Type of a Field Value and You are passing with a Wrong Data Type.
Let me tell you an example. You have created a field (crb9b_age) of string data type in Dynamics 365 and in Plugin you have written the following code.
var age = (DateTime.Now.Subtract(DOB).Days) / 365;
ent["crb9b_age"] = age;
The above line of code will give error because When age variable is assigned the value the value becomes Integer because of dynamic Data Type assignment due to use of VAR keyword.
SO when we will assign the value to attribute then we have to change it to string otherwise it will through error.
So the solution is.
var age = (DateTime.Now.Subtract(DOB).Days) / 365;
ent["crb9b_age"] = age.ToString();
Now there is no error.
NOTE:
Your case may be different so analyze the data type values you are passing and what D365 is expecting then it will resolve your issue.