Search code examples
c#dynamics-crm

CodeActivity GetExtension null


Saying that i never used Workflow and i started coding from a sample, i have a problem with the CodeActivityContext.GetExtension<> function.

This is the constructor

public SendReportMethod(Guid var1, string var2)
{
    this.var1= var1;
    this.var2= var2;
}

And this is the piece of code giving me the error:

protected override void Execute(CodeActivityContext  executionContext)
{
    IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
    IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
}

Variables are all null, and the last one is obiouvsly giving me nullPointer.

This is the way i'm calling the CodeActivity:

WorkflowInvoker.Invoke(new SendReportMethod(interventionID, codiceFornitore));

What i'm doing wrong? any help will be appreciated. Thanks


Solution

  • It looks like you are using Dynamics CRM, and you want to use a custom workflow activity in your workflows. Here's an example of how you would do that (found at https://msdn.microsoft.com/en-us/library/gg334455.aspx)

    Also, here's a more comprehensive example: https://www.credera.com/blog/technology-insights/microsoft-solutions/three-essential-custom-workflow-activities-crm-2013/

    public sealed class SimpleSdkActivity : CodeActivity
    {
        [Input("Account Name")]
        [Default("Test Account")]
        public InArgument<string> AccountName { get; set; }
        protected override void Execute(CodeActivityContext executionContext)
        {
            //Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();
    
            //Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
            var entityName = AccountName.Get<string>(executionContext);
        }
    }