Hi All,
I am trying to update a record on case entity. A field on case record needs to be updated with a value from another entity(labor rates) based on certain criteria. Below is the code I have used.
Entity PostEntity = (Entity)Context.PostEntityImages["POST_IMAGE"];
Entity a = (Entity)Context.InputParameters["Target"];
if (PostEntity.LogicalName != "incident")
{
return;
}
if (Context.MessageName == "Create" || Context.MessageName == "Update")
{
if (PostEntity.Attributes.Contains("new_sitecountry"))
{
string siteCountry = PostEntity.GetAttributeValue<string>("new_sitecountry");
string serviceProviderPreference = PostEntity.GetAttributeValue<string>("new_3rdpartyinstallationordealerreplacement");
int serviceProviderType = ((OptionSetValue)PostEntity["new_fieldactiontype"]).Value;
// Query the corresponding values from Labor rates entity
QueryExpression laborRateQuery = new QueryExpression("new_laborrates"); //Another Entity labor rates
ColumnSet rateColumns = new ColumnSet("new_country", "new_serviceprovidertype", "new_inspectionlaborrate", "new_cableinstalllaborrate", "new_replacementlaborrate", "new_laborratesid");
laborRateQuery.ColumnSet = rateColumns;
laborRateQuery.Criteria.AddCondition("new_country", ConditionOperator.Equal, siteCountry);
laborRateQuery.Criteria.AddCondition("new_serviceprovidertype", ConditionOperator.Equal, serviceProviderPreference);
EntityCollection rates = Service.RetrieveMultiple(laborRateQuery);
if(rates.Entities.Count == 0)
{
#if DEBUG
debugString += "\nRecord not found: ";
#endif
}
else
{
foreach (var v in rates.Entities)
{
Guid RateId = (Guid)v.Attributes["new_laborratesid"]; //primary key of labor rates entity
a["new_fieldactionlaborrate"] = new EntityReference("new_replacementlaborrate", RateId);
}
}
Service.Update(a);
After scratching my head for so long I am not able to find out why I am getting the error:
System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Incorrect attribute value type System.String (Fault Detail is equal to Exception details:
ErrorCode: 0x80040203
Message: Incorrect attribute value type System.String;
[Microsoft.Crm.ObjectModel: Microsoft.Crm.ObjectModel.TargetAttributeValidationPlugin]
[fc743e7d-fbea-4695-bdb9-7d78334c8474: TargetAttributeValidationPlugin]
TimeStamp: 2019-03-20T06:00:11.7932911Z
OriginalException: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Incorrect attribute value type System.String (Fault Detail is equal to Exception details:
ErrorCode: 0x80040203
Message: Incorrect attribute value type System.String;
[Microsoft.Crm.ObjectModel: Microsoft.Crm.ObjectModel.TargetAttributeValidationPlugin]
[fc743e7d-fbea-4695-bdb9-7d78334c8474: TargetAttributeValidationPlugin].
The fields "new_fieldactionlaborrate" and "new_replacementlaborrate" are whole number fields.
Thanks
Amrita