Hello Experts,
i try to get Data from different Entities in Dynamics.
I created a custom Workflow activity to retrieve the Data from the Entities but the Workflow didnt work.
It dont return me any data.
Can you spot the issue in my code?
Thank you in advance!
Kind Regards
Léon
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.Linq; using System.Text; using System.ServiceModel; using System.Threading.Tasks; using System.Activities; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk.Metadata; using System.Runtime.Serialization; using Microsoft.Xrm.Sdk.Query; namespace WorkflowActivityLibrary5 { /// </summary> public sealed class SelectProvision : WorkFlowActivityBase { #region Properties //Property for Entity mrd_abrufschein [RequiredArgument] [Input("Abrufschein")] [ReferenceTarget("mrd_abrufschein")] public InArgument<EntityReference> abrufscheinRef { get; set; } #endregion private IOrganizationService service { get; set; } /// <summary> /// Executes the WorkFlow. /// </summary> /// <param name="crmWorkflowContext">The <see cref="LocalWorkflowContext"/> which contains the /// <param name="executionContext" > <see cref="CodeActivityContext"/> /// </param> /// <remarks> /// For improved performance, Microsoft Dynamics 365 caches WorkFlow instances. /// The WorkFlow's Execute method should be written to be stateless as the constructor /// is not called for every invocation of the WorkFlow. Also, multiple system threads /// could execute the WorkFlow at the same time. All per invocation state information /// is stored in the context. This means that you should not use global variables in WorkFlows. /// </remarks> public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext) { // Create the tracing service ITracingService tracingService = executionContext.GetExtension<ITracingService>(); if (tracingService == null) { throw new InvalidPluginExecutionException("Failed to retrieve tracing service."); } tracingService.Trace("Entered GetProvision.Excecute(), Activity Instance Id: {0}, Workflow Instance Id: {1}", executionContext.ActivityInstanceId, executionContext.WorkflowInstanceId); // Create the context IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); if (context == null) { throw new InvalidPluginExecutionException("Failed to retrieve workflow context."); } tracingService.Trace("GetProvision.Execute(), Correlation Id: {0}, Initiating User: {1}", context.CorrelationId, context.InitiatingUserId); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); service = serviceFactory.CreateOrganizationService(context.UserId); try { Guid abrufscheinId = Guid.Empty; string produkthersteller = null; DateTime datum = new DateTime(); EntityReference aref = abrufscheinRef.Get<EntityReference>(executionContext); // Abrufschein ermittlen Entity abrufschein = service.Retrieve(aref.LogicalName, aref.Id, new ColumnSet("mrd_abrufscheinid", "mrd_produkt_hersteller", "mrd_datum_austellung_abrufschein", "mrd_zulassungsdatum")); Guid.TryParse(abrufschein.Attributes["mrd_abrufscheinId"].ToString(), out abrufscheinId); // tracingService.Trace("AbrufscheinID " # abrufscheinId); if (abrufscheinId == Guid.Empty) { throw new InvalidPluginExecutionException("Die Entität, an der Provisionen angehängt werden, wurde nicht gefunden"); } // Produkt ermitteln produkthersteller = abrufschein.GetAttributeValue<string>("mrd_produkt_hersteller"); // tracingService.Trace("Abrufschein ID " # produkthersteller); if (produkthersteller == null) { throw new InvalidPluginExecutionException("Der Hersteller wurde nicht gefunden"); } //Datum ermitteln datum = abrufschein.GetAttributeValue<DateTime>("mrd_zulassungsdatum"); // tracingService.Trace("Abrufschein ID " # produkthersteller); if (datum == new DateTime()) { datum = abrufschein.GetAttributeValue<DateTime>("mrd_datum_austellung_abrufschein"); if (datum == new DateTime()) { throw new InvalidPluginExecutionException("Es wurde kein Datum gefunden"); } } Provisionsdaten provdaten = GetProvision(produkthersteller, datum); ProvisionWertMRD.Set(executionContext, provdaten.ProvisionWertMRD); ProvisionWertMR.Set(executionContext, provdaten.ProvisionWertMR); ProvisionWertMRDPro.Set(executionContext, provdaten.ProvisionWertMRDPro); ProvisionWertMRPro.Set(executionContext, provdaten.ProvisionWertMRPro); ProvisionPauschalWertMRD.Set(executionContext, provdaten.ProvisionPauschalWertMRD); ProvisionPauschalWertMR.Set(executionContext, provdaten.ProvisionPauschalWertMR); } catch { } } // Provision ermitteln internal Provisionsdaten GetProvision(string produkthersteller, DateTime datum) { Entity provision = new Entity(); QueryExpression query = new QueryExpression("mrd_ekv_provision"); query.ColumnSet = new ColumnSet(true); Provisionsdaten provdaten = new Provisionsdaten(); GetEinloesequote(produkthersteller, datum); GetProvisionPauschal(produkthersteller, datum); query.Criteria.AddCondition("mrd_ekv_hersteller", ConditionOperator.Equal, produkthersteller); query.Criteria.AddCondition("mrd_ekv_datumvon", ConditionOperator.GreaterThan, datum); query.Criteria.AddCondition("mrd_ekv_datumbis", ConditionOperator.LessThan, datum); // query ausführen EntityCollection retrieveprovision = service.RetrieveMultiple(query); if (retrieveprovision.Entities.Count == 0) { // Template nicht gefunden } else if (retrieveprovision.Entities.Count > 1) { throw new InvalidPluginExecutionException("Es wurde mehrere Datensätze von Einlösequoten gefunden"); } else { provision = retrieveprovision.Entities[0]; } provdaten.ProvisionWertMR = provision.GetAttributeValue<decimal>("mrd_ekv_provision_mr"); provdaten.ProvisionWertMRD = provision.GetAttributeValue<decimal>("mrd_ekv_provision_mrd"); return provdaten; } // Provision Pauschal ermitteln internal Provisionsdaten GetProvisionPauschal(string produkthersteller, DateTime datum) { Entity provisionpauschal = new Entity(); QueryExpression query = new QueryExpression("mrd_ekv_provisionen_pauschal"); query.ColumnSet = new ColumnSet(true); Provisionsdaten provdaten = new Provisionsdaten(); query.Criteria.AddCondition("mrd_ekv_hersteller", ConditionOperator.Equal, produkthersteller); query.Criteria.AddCondition("mrd_ekv_datum_von", ConditionOperator.GreaterThan, datum); query.Criteria.AddCondition("mrd_ekv_datum_bis", ConditionOperator.LessThan, datum); // query ausführen EntityCollection retrievprovpauschal = service.RetrieveMultiple(query); if (retrievprovpauschal.Entities.Count == 0) { // Template nicht gefunden } else if (retrievprovpauschal.Entities.Count > 1) { throw new InvalidPluginExecutionException("Es wurde mehrere Datensätze von Einlösequoten gefunden"); } else { provisionpauschal = retrievprovpauschal.Entities[0]; } provdaten.ProvisionPauschalWertMR = provisionpauschal.GetAttributeValue<decimal>("mrd_ekv_wert_provision_mr"); provdaten.ProvisionPauschalWertMRD = provisionpauschal.GetAttributeValue<decimal>("mrd_ekv_wert_provision_mrd"); return provdaten; } // Einlösequote ermitteln internal Provisionsdaten GetEinloesequote(string produkthersteller, DateTime datum) { Entity einloesequoten = new Entity(); QueryExpression query = new QueryExpression("mrd_ekv_einloesequoten"); query.ColumnSet = new ColumnSet(true); Provisionsdaten provdaten = new Provisionsdaten(); query.Criteria.AddCondition("mrd_ekv_hersteller", ConditionOperator.Equal, produkthersteller); query.Criteria.AddCondition("mrd_ekv_datum_von", ConditionOperator.GreaterThan, datum); query.Criteria.AddCondition("mrd_ekv_datum_bis", ConditionOperator.LessThan, datum); // query ausführen EntityCollection retrieveedxpan = service.RetrieveMultiple(query); if (retrieveedxpan.Entities.Count == 0) { // Template nicht gefunden } else if (retrieveedxpan.Entities.Count > 1) { throw new InvalidPluginExecutionException("Es wurde mehrere Datensätze von Einlösequoten gefunden"); } else { einloesequoten = retrieveedxpan.Entities[0]; } provdaten.Einloesequote = einloesequoten.GetAttributeValue<int>( "mrd_ekv_einloesequote_prozent"); return provdaten; } // Output aus Class Provisionsdaten [Output("Provision Tatsächlich MRD")] public OutArgument<double> ProvisionWertMRD { get; set; } [Output("Provision Tatsächlich MR")] public OutArgument<double> ProvisionWertMR { get; set; } [Output("Provision Tatsächlich MR Prozent")] public OutArgument<int> ProvisionWertMRPro { get; set; } [Output("Provision Tatsächlich MRD Prozent")] public OutArgument<int> ProvisionWertMRDPro { get; set; } [Output("Provision Pauschal MRD")] public OutArgument<int> ProvisionPauschalWertMRD { get; set; } [Output("Provision Pauschal MR")] public OutArgument<int> ProvisionPauschalWertMR { get; set; } [Output("Einlösequote")] public OutArgument<int> Einloesequote { get; set; } } }
*This post is locked for comments