how can i use preImage to update value in dynamic 365 using plugin, I already write part of the code but I cant complete it
- this is the code , I put a comment above preImage code
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace ProjectsTask
{
public class Price : IPlugin
{
public void Execute(IServiceProvider ServiceProvider)
{
IPluginExecutionContext Context = (IPluginExecutionContext)
ServiceProvider.GetService(typeof(IPluginExecutionContext));
Entity entity = (Entity)Context.InputParameters["Target"];
IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService Service = ServiceFactory.CreateOrganizationService(Context.UserId);
QueryExpression qe = new QueryExpression();
qe.EntityName = "cree5_task";
ColumnSet col = new ColumnSet("cree5_taskprice");
qe.ColumnSet = col;
EntityReference entityRef = (EntityReference)entity.Attributes["cree5_projecttask"];
qe.Criteria.AddCondition("cree5_projecttask", ConditionOperator.Equal, entityRef.Id);
EntityCollection collection = Service.RetrieveMultiple(qe);
// here the code I want to complete
if (Context.MessageName == "update")
{
Entity preImage = Context.PreEntityImages["PreImage"] as Entity;
if (entity.Contains("cree5_taskprice"))
{
throw new InvalidPluginExecutionException("updated");
}
else if (preImage.Contains("cree5_taskprice"))
{
}
}
decimal totalPrice = 0m;
foreach (var e in collection.Entities)
{
var price = ((Money)e.Attributes["cree5_taskprice"]).Value;
totalPrice += price;
}
var Entity = Service.Retrieve("cree5_projects", entityRef.Id, new ColumnSet(true));
Entity["cree5_totalprice"] = new Money(totalPrice);
Service.Update(Entity);
}
}
}