Hello,
We have a form with 2 categories “technical and controls”. When the user creates the control record, we have a workflow to create the Technical record. On the update of the control record the technical record should be updated based on the Control record. I created a plugin on the pre operation update, the code retrieves the correct values but doesn’t save the changes. Please check the below code
namespace PTD.Plugins
{
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Linq;
public class PreProjectTechnicalDataUpdate01: Plugin
{
public PreProjectTechnicalDataUpdate01()
: base(typeof(PreProjectTechnicalDataUpdate01))
{
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Update", "new_projecttechnicaldata", new Action<LocalPluginContext>(ExecutePreProjectTechnicalDataUpdate01)));
}
protected void ExecutePreProjectTechnicalDataUpdate01(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
Entity entity = null;
Entity preentity = null;
if (localContext.PluginExecutionContext.InputParameters.Contains("Target") && localContext.PluginExecutionContext.InputParameters["Target"] is Entity)
{
entity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
}
else
{
return;
}
if (localContext.PluginExecutionContext.PreEntityImages.Contains("test"))
{
preentity = (Entity)localContext.PluginExecutionContext.PreEntityImages["test"];
}
else
{
return;
}
new_projecttechnicaldata company = entity.ToEntity<new_projecttechnicaldata>();
new_projecttechnicaldata project = preentity.ToEntity<new_projecttechnicaldata>();
using (GeneratedEntities orgContext = new GeneratedEntities(localContext.OrganizationService))
{
if (company.new_Type == "Technical")
{
}
else if (project.new_Type == "Technical")
{
}
else
{
var CT = (from b in orgContext.new_projecttechnicaldataSet
where (b.GetAttributeValue<Guid>("new_company") == project.new_Company.Id)
&& (b.GetAttributeValue<Guid>("new_project") == project.new_Project.Id)
&& (b.GetAttributeValue<DateTime>("new_date") == project.new_Date)
&& b.GetAttributeValue<string>("new_type") == "Technical"
select b).FirstOrDefault();
Int32 CertifiedClaims = 0;
if (company.Contains("new_certifiedclaims"))
{
CertifiedClaims = (Int32)company.new_CertifiedClaims;
}
else if (project.Contains("new_certifiedclaims"))
{
CertifiedClaims = (Int32)project.new_CertifiedClaims;
}
CT["new_certifiedclaims"] = CertifiedClaims;
decimal GrossProfitt = 0;
if (company.Contains("new_grossprofitt"))
{
GrossProfitt = (decimal)company.new_GrossProfitt;
}
else if (project.Contains("new_grossprofitt"))
{
GrossProfitt = (decimal)project.new_GrossProfitt;
}
CT["new_grossprofitt"] = GrossProfitt;
}
}
}
}
}