Example of a simple custom workflow for Mscrm
Custom workflow in Dynamics 365, D365 Custom workflow
This simple workflow is taking the input parameters and returning the value of a custom configuration entity in MSCRM. This workflow will help you understand and build your own custom logic.
using System; using System.Linq; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk.Query; using System.Activities; using System.ServiceModel; namespace Example.CRM.WorkFlow { ///summary /// Custom Action to Get the Configuration Values /// public class GetConfigurationValues : CodeActivity { #region Input Parameters #endregion #region Output Parameters [Output("Configuration Entity")] [ReferenceTarget("new_configuration")] public OutArgument OutputConfigValue { get; set; } [Input("Contact Name :")] [ReferenceTarget("contact")] public InArgument SingleContact { get; set; } [Input("Marketing List :")] [ReferenceTarget("list")] public InArgument MarketingList { get; set; } [RequiredArgument] [Input("FieldName")] [Default("")] public InArgument FieldName { get; set; } #endregion #region GetConfiguration Values protected override void Execute(CodeActivityContext executionContext) { #region Service Declaration /// Create the tracing service ITracingService tracingService = executionContext.GetExtension(); if (tracingService == null) throw new InvalidWorkflowException("Failed to retrieve tracing service."); /// Create the context IWorkflowContext context = executionContext.GetExtension(); if (context == null) throw new InvalidWorkflowException("Failed to retrieve workflow context."); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); #endregion try { string fieldValue= FieldName.Get(executionContext); QueryByAttribute query = new QueryByAttribute("new_configuration"); query.AddAttributeValue("new_name", FieldValue); query.AddAttributeValue("statecode", 0); query.ColumnSet = new ColumnSet("new_configurationid"); EntityCollection configCollection = service.RetrieveMultiple(query); if (configCollection.Entities.ToList().Count() > 0) { Entities.new_configuration config = (Entities.new_configuration)configCollection.Entities.ToList().First(); EntityReference configRefer = new EntityReference("new_configuration", config.Id); OutputConfigValue.Set(executionContext, configRefer); } } catch (FaultException e) { throw new InvalidWorkflowException("Exeception in GetConfigurationValues Custom Workflow -" + e.Detail + e.InnerException); } catch (Exception ex) { throw new InvalidWorkflowException("Exeception in GetConfigurationValues Custom Workflow -" + ex.Message + ex.InnerException); } } #endregion } }