RE: Entity Id must be the same as the value set in property bag - While trying to Update
Hi Shaminderpal
i have the same problem in my plugin
and use that coed
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "msdyn_agreementbookingdate")
{
if (entity.Contains("msdyn_agreement"))
{
if (entity["msdyn_agreement"] != null)
{
Guid agreementId = ((EntityReference)entity.Attributes["msdyn_agreement"]).Id;
// get booking dates with the same agreement and does not have the same id as the entity
var fetchXmlDates = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
fetchXmlDates += "<entity name='msdyn_agreementbookingdate'>";
fetchXmlDates += "<attribute name='msdyn_bookingsetup' />";
fetchXmlDates += "<attribute name='msdyn_bookingdate' />";
fetchXmlDates += "<attribute name='msdyn_agreement' />";
fetchXmlDates += "<attribute name='msdyn_agreementbookingdateid' />";
fetchXmlDates += "<order attribute='msdyn_bookingdate' descending='true' />";
fetchXmlDates += "<filter type='and'>";
fetchXmlDates += "<condition attribute='msdyn_agreement' operator='eq' uitype='msdyn_agreement' value='" + agreementId + "' />";
fetchXmlDates += "<condition attribute='msdyn_agreementbookingdateid' operator='ne' value='" + entity.Id + "' />";
fetchXmlDates += "</filter>";
fetchXmlDates += "<link-entity name='msdyn_agreementbookingsetup' from='msdyn_agreementbookingsetupid' to='msdyn_bookingsetup' alias='bookRec'>";
fetchXmlDates += "<attribute name='new_bookingrecurring' />";
fetchXmlDates += "<filter type='and'>";
fetchXmlDates += "<condition attribute='new_bookingrecurring' operator='not-null' />";
fetchXmlDates += "</filter>";
fetchXmlDates += "</link-entity>";
fetchXmlDates += "</entity>";
fetchXmlDates += "</fetch>";
var resultDates = service.RetrieveMultiple(new FetchExpression(fetchXmlDates));
try
{
if (resultDates.Entities.Count > 1)
{
#region Update Customer Asset
foreach (Entity c in resultDates.Entities)
{
var fetchXmlGETCAForeachBS = $@"
<fetch>
<entity name='msdyn_agreementbookingsetup'>
<attribute name='new_customerasset' />
<attribute name='msdyn_agreementbookingsetupid' />
<filter type='and'>
<condition attribute='msdyn_agreementbookingsetupid' operator='eq' value='{c.GetAttributeValue<EntityReference>("msdyn_bookingsetup").Id}'/>
</filter>
</entity>
</fetch>";
var resultD = service.RetrieveMultiple(new FetchExpression(fetchXmlGETCAForeachBS));
foreach (Entity b in resultD.Entities)
{
if (c.GetAttributeValue<EntityReference>("msdyn_bookingsetup").Id == (Guid)b.Attributes["msdyn_agreementbookingsetupid"])
{
entity.Id = (Guid)c.Attributes["msdyn_agreementbookingdateid"];
entity.Attributes["new_customerasset"] = Convert.ToString(b.GetAttributeValue<EntityReference>("new_customerasset").Id);
service.Update(entity);
}
}
}
#endregion
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException("Have An Error : " +e.Message.ToString());
}
}
}
}