public void Execute(IServiceProvider serviceProvider)
{
var organizationServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var pluginContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var orgService = organizationServiceFactory.CreateOrganizationService(pluginContext.UserId);
if (
pluginContext.InputParameters["Query"] is FetchExpression &&
pluginContext.MessageName.Equals("RetrieveMultiple", StringComparison.OrdinalIgnoreCase)
&& pluginContext.PrimaryEntityName.Equals("new_zcalcprice", StringComparison.OrdinalIgnoreCase)
&& pluginContext.InputParameters.ContainsKey("Query")
)
{
var fetchExpression = (FetchExpression)pluginContext.InputParameters["Query"];
FetchXmlToQueryExpressionRequest req = new FetchXmlToQueryExpressionRequest();
req.FetchXml = fetchExpression.Query;
var resp = (FetchXmlToQueryExpressionResponse)orgService.Execute(req);
QueryExpression queryExpression = resp.Query;
Entity calcpriceentity = new Entity("new_zcalcprice");
calcpriceentity["new_name"] = Guid.NewGuid().ToString();
WhoAmIRequest userRequest = new WhoAmIRequest();
WhoAmIResponse user = (WhoAmIResponse)orgService.Execute(userRequest);
Guid myUserId = user.UserId;
// calcpriceentity["ownerid"] = myUserId;
//AssignRequest assignRequest = new AssignRequest
//{
// Assignee = new EntityReference("systemuser", myUserId),
// Target = new EntityReference("account", myUserId)
//};
//orgService.Execute(assignRequest);
//calcpriceentity.Attributes.Add("ownerid", myUserId);
foreach (var condition in queryExpression.Criteria.Filters[0].Conditions)
{
if (condition.AttributeName.Equals("new_product"))
{
calcpriceentity["new_product"] = new EntityReference("product", (Guid)condition.Values[0]);
}
if (condition.AttributeName.Equals("new_pricelevelid"))
{
calcpriceentity["new_pricelevelid"] = (Guid)condition.Values[0];
}
if (condition.AttributeName.Equals("new_priceperunit"))
{
calcpriceentity["new_priceperunit"] = new Money(Convert.ToDecimal(condition.Values[0]));
}
if (condition.AttributeName.Equals("new_quantity"))
{
calcpriceentity["new_quantity"] = Convert.ToDecimal(condition.Values[0]);
}
if (condition.AttributeName.Equals("new_baseamount"))
{
calcpriceentity["new_baseamount"] = new Money(Convert.ToDecimal(condition.Values[0]));
}
}
orgService.Create(calcpriceentity);
}