Hi all,
In CRM 4.0 i have custom entity "Delivery Order" and link relationship (N:1) with sales order. I want if my salesorderid have used in delivery order, user can not "cancel order", i have used this code in below:
[PluginStep(pluginStep.Cancel, PluginStepStage.PreEvent, PrimaryEntityName = ExtSchema.SO, InvocationSource = PluginStepInvocationSource.ParentPipeline)]
public class SalesOrderUpdatePlugin : IPlugin
{
private static object _sync = new object();
public void Execute(IPluginExecutionContext context)
{
DynamicEntity entity = null;
Moniker moniker = null;
lock (_sync)
{
using (ICrmService service = context.CreateCrmService(true))
{
try
{
//initialisasi
SalesOrder oSalesOrder = new SalesOrder();
if(context.MessageName == "Cancel")
{
#region"Cancel"
//get data from entity target
entity = (DynamicEntity)context.InputParameters.Properties["OrderClose"];
//get entity id data
//get SalesOrderId
Guid gSalesOrderId = (Guid)(entity.Properties[SalesOrder.Fields.Salesorderid] as Lookup).Value;
//-->get delivery order by sales order
BusinessEntityCollection DeliveryOrderData =
this.GetDeliveryOrder_BySalesOrderId(service, gSalesOrderId);
if (DeliveryOrderData.BusinessEntities.Count > 0)
{
throw new InvalidPluginExecutionException
("You can not cancel this transaction, cause have used in delivery order.");
}
#endregion
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw new InvalidPluginExecutionException(
"An error occurred in the SalesOrderCreatePlugin", ex);
}
}
}
}
private BusinessEntityCollection GetDeliveryOrder_BySalesOrderId(ICrmService _ICrmService, Guid _SalesOrderId)
{
BusinessEntityCollection ret = new BusinessEntityCollection();
try
{
QueryExpression query = new QueryExpression();
query.EntityName = "xts_deliveryorder";
query.ColumnSet = new ColumnSet(new string[] { "xts_ordnbr" });
query.Criteria.AddCondition("xts_ordnbr", ConditionOperator.Equal, _SalesOrderId);
RetrieveMultipleRequest request = new RetrieveMultipleRequest()
{
ReturnDynamicEntities = true,
Query = query
};
RetrieveMultipleResponse response = (RetrieveMultipleResponse)_ICrmService.Execute(request);
ret = response.BusinessEntityCollection;
}
catch (InvalidPluginExecutionException ex)
{
throw new InvalidPluginExecutionException(
"An error occurred in the function GetDeliveryOrder_BySalesOrderId().", ex);
}
return ret;
}
i try code on above but always get error.. anyone have solution??
thanks,
Glo.