web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Update using PreImage

(0) ShareShare
ReportReport
Posted on by 10

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);

}
}
}

I have the same question (0)
  • mo jala Profile Picture
    10 on at
    RE: Update using PreImage

    When I want to change (Update) the value of previous record filed in dynamic 365, it give me an error

    some people tell me to use PreImage.

    the code u write above I already use it to update the totalPrice of values, but here I want to update another field represent (subTotal)!

  • Ram Prakash Profile Picture
    2,287 on at
    RE: Update using PreImage

    Hello,

    in Pre Operation you don't want to make an update operationm in the same entity. Instead You just directly pass

    Entity entity = (Entity)Context.InputParameters["Target"];

    entity["cree5_totalprice"] = new Money(totalPrice);

    thats it :)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 174

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 121 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans