Hello,
Can anyone help me how to proceed with my code.
This is my code for calculating totals when creating a record in order product lines.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
if (context.MessageName == "Create")
{
Entity salesorderdetail = context.InputParameters["Target"] as Entity;
if (salesorderdetail != null)
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
QueryExpression query = new QueryExpression()
{
Distinct = false,
EntityName = "crcd0_summaryorderproduct",
ColumnSet = new ColumnSet("crcd0_totalcost", "crcd0_totalmargin", "crcd0_roi", "crcd0_contractfromdate", "crcd0_contracttodate", "crcd0_totalamount"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "crcd0_existingproduct",
Operator = ConditionOperator.Equal,
Values =
{
salesorderdetail.GetAttributeValue<EntityReference>("productid").Id,
}
},
new ConditionExpression
{
AttributeName = "crcd0_salesorder",
Operator = ConditionOperator.Equal,
Values =
{
salesorderdetail.GetAttributeValue<EntityReference>("salesorderid").Id,
}
}
}
}
};
EntityCollection summaryCollection = service.RetrieveMultiple(query);
Entity summary = summaryCollection[0];
QueryExpression salesOrderQuery = new QueryExpression()
{
Distinct = false,
EntityName = "salesorderdetail",
ColumnSet = new ColumnSet("crcd0_summarycost", "crcd0_margin", "priceperunit"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "productid",
Operator = ConditionOperator.Equal,
Values =
{
salesorderdetail.GetAttributeValue<EntityReference>("productid").Id,
}
},
new ConditionExpression
{
AttributeName = "salesorderid",
Operator = ConditionOperator.Equal,
Values =
{
salesorderdetail.GetAttributeValue<EntityReference> ("salesorderid").Id,
}
}
}
}
};
QueryExpression salesorder = new QueryExpression()
{
Distinct = false,
EntityName = "salesorder",
ColumnSet = new ColumnSet("crcd0_totalcost", "crcd0_totalmargin"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "salesorderid",
Operator = ConditionOperator.Equal,
Values =
{
salesorderdetail.GetAttributeValue<EntityReference>("salesorderid").Id,
}
}
}
}
};
EntityCollection orderQuery = service.RetrieveMultiple(salesOrderQuery);
EntityCollection order = service.RetrieveMultiple(salesorder);
Entity salOrder = order[0];
var totalcost = (orderQuery.Entities.Sum(o => Convert.ToDecimal(o.GetAttributeValue<Money>("crcd0_summarycost").Value)));
var totalmargin = (orderQuery.Entities.Sum(o => Convert.ToDecimal(o.GetAttributeValue<Money>("crcd0_margin").Value)));
var roi = Convert.ToDecimal(0);
if (totalcost != 0)
{
roi = orderQuery.Entities.Sum(o => Convert.ToDecimal(o.GetAttributeValue<Money>("crcd0_summarycost").Value)) / orderQuery.Entities.Average(o => Convert.ToDecimal(o.GetAttributeValue<Money>("priceperunit").Value));
}
var totalamount = (orderQuery.Entities.Sum(o => Convert.ToDecimal(o.GetAttributeValue<Money>("priceperunit").Value)));
summary["crcd0_totalcost"] = new Money(totalcost);
summary["crcd0_totalmargin"] = new Money(totalmargin);
summary["crcd0_roi"] = roi;
summary["crcd0_totalamount"] = new Money(totalamount);
salOrder["crcd0_totalcost"] = new Money(totalcost);
salOrder["crcd0_totalmargin"] = new Money(totalmargin);
service.Update(summary);
service.Update(salOrder);
}
}
}else if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference && context.MessageName == "Delete")
{
}
}
I need to add on delete step and recalculate thw totals when a record is deleted. I tried creating the step as post op and adding pre image, but pre image is null.