
Hello,
I've made a plugin in order to automatically create an order detail when an order is created.
Here is my code:
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity Order = (Entity)context.InputParameters["Target"];
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
Guid caseSerialNumber = Guid.Empty;
Guid orderId = Order.Id;
if (Order.GetAttributeValue("zst_case") != null)
{
caseSerialNumber = Order.GetAttributeValue("zst_case").Id;
QueryExpression query = new QueryExpression("zst_parttask");
query.ColumnSet = new ColumnSet("zst_product", "zst_quantity", "zst_unit");
query.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, caseSerialNumber);
EntityCollection collection = service.RetrieveMultiple(query);
if (collection.Entities.Count > 0)
{
foreach (Entity product in collection.Entities)
{
Entity orderProduct = new Entity("salesorderdetail");
orderProduct["salesorderid"] = new EntityReference("salesorder", orderId);
if (product.Attributes.Contains("zst_quantity"))
{
var inputQuantity = product.GetAttributeValue("zst_quantity");
orderProduct.Attributes.Add("quantity", Convert.ToDecimal(inputQuantity));
}
if (product.Attributes.Contains("zst_product"))
{
EntityReference taskProduct = (EntityReference)product.Attributes["zst_product"];
var LookupId = taskProduct.Id;
var logicalName = taskProduct.LogicalName;
orderProduct["productid"] = new EntityReference(logicalName, LookupId);
}
if (product.Attributes.Contains("zst_unit"))
{
EntityReference taskUnit = (EntityReference)product.Attributes["zst_unit"];
var LookupIdUnit = taskUnit.Id;
var logicalNameUnit = taskUnit.LogicalName;
orderProduct["uomid"] = new EntityReference(logicalNameUnit, LookupIdUnit);
}
bool _price = false;
bool _product = false;
product["ispriceoverridden"] = _price;
product["isproductoverridden"] = _product;
service.Create(orderProduct);
}
}
}
}
The plugin step is on Create of Order (Post operation, I've tried synchronously and asynchronously)
When I use the plugin profiler to debug it, I get the error "Salesorder id does not exist"
Anh help would be appreciated