Hello Team,
Below i created plugin for Pre-Validation on invoice creating.
Issue : In this code error is not throwing on front side in D365 Project and Operations but if i debug this code it is working well as per condition but same error is not display while creation of invoice if conditions true.
I checked same in Pre-Operation but still not working
my Code:
protected override void ExecuteCdsPlugin(ILocalPluginContext localContext)
{
if (localContext == null)
{
throw new InvalidPluginExecutionException(nameof(localContext));
}
ITracingService tracingService = localContext.TracingService;
IPluginExecutionContext context = (IPluginExecutionContext)localContext.PluginExecutionContext;
IOrganizationService currentUserService = localContext.CurrentUserService;
try
{
if (context.InputParameters.Contains("Target"))
{
Entity invoiceEntity = (Entity)context.InputParameters["Target"];
invoiceEntity.Id = context.PrimaryEntityId;
Entity invoiceEntityData = currentUserService.Retrieve(context.PrimaryEntityName, invoiceEntity.Id, new ColumnSet(true));
EntityReference contract = invoiceEntityData.GetAttributeValue<EntityReference>("salesorderid");
var contractId = contract.Id;
QueryExpression query = new QueryExpression { EntityName = "msdyn_contractlinescheduleofvalue", ColumnSet = new ColumnSet(true) };
query.Criteria = new FilterExpression();
query.Criteria.AddCondition("msdyn_contract", ConditionOperator.Equal, contractId);
LinkEntity linkSetup = query.AddLink("salesorder", "msdyn_contract", "salesorderid", JoinOperator.Inner);
linkSetup.LinkCriteria.FilterOperator = LogicalOperator.And;
linkSetup.LinkCriteria.AddCondition("salesorderid", ConditionOperator.Equal, contractId);
query.Criteria.AddCondition("msdyn_invoicedate", ConditionOperator.LessThan, DateTime.Now);
DataCollection<Entity> fetchedcontractlinescheduleofvalueEntity = currentUserService.RetrieveMultiple(query).Entities;
if (fetchedcontractlinescheduleofvalueEntity.Count > 0)
{
throw new InvalidPluginExecutionException("You can not reaise invoice for this date.");
}
}
}
catch (InvalidPluginExecutionException ex)
{
throw new InvalidPluginExecutionException("An error occurred executing Plugin ISPLInvoicesValidation.ISPLInvoicesValidation.PreValidationinvoiceCreate .", ex);
}
}