0

I have a custom workflow that copies the attachments from an email and places the copy in the notes.At the moment, this process is ran manually. I want to enable this process to be ran automatically everyday at a specified time of day. However i am unsure how to do this.

public class Email_Attachments_to_Regarding_Document_Store : CodeActivity
{
    protected override void Execute(CodeActivityContext executionContext)
    {
        ITracingService tracer = executionContext.GetExtension<ITracingService>();
        IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
        IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        try
        {
            //Retrieve Email the workflow is running against from Dynamics
            Email email = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true)).ToEntity<Email>();

            //Retrieve the attachment mimes related to the email
            DataCollection<Entity> mimes = service.RetrieveMultiple(new QueryExpression
            {
                EntityName = ActivityMimeAttachment.EntityLogicalName,
                ColumnSet = new ColumnSet(true),
                Criteria =
                {
                    Conditions =
                    {
                        //Filter by related Email, 'objectid' is the related email.
                        new ConditionExpression("objectid", ConditionOperator.Equal, email.Id)
                    }
                }
            }).Entities;

            foreach(ActivityMimeAttachment mime in mimes)
            {
                Annotation note = new Annotation
                {
                    AnnotationId = Guid.NewGuid(),
                    DocumentBody = mime.Body,
                    IsDocument = true,
                    FileName = mime.FileName,
                    MimeType = mime.MimeType,
                    ObjectTypeCode = email.RegardingObjectId.LogicalName,
                    ObjectId = email.RegardingObjectId
                };
                note.IsDocument = true;
                service.Create(note);

            }
        }
        catch (Exception e)
        {
            throw new InvalidPluginExecutionException(e.Message);
        }

    }

This code copies the email attachments into notes. However i am unable to run this code at a specified time of day. The code below is an azure function that connects to dynamics

 public static class Function1
{
    [FunctionName("Function1")]
    public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
    {
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");


        IServiceManagement<IOrganizationService> orgServiceManagement = 
            ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri("https://disco.crm11.dynamics.com/XRMServices/2011/Discovery.svc"));


        AuthenticationCredentials authCredentials = new AuthenticationCredentials();
        authCredentials.ClientCredentials.UserName.UserName = "xxx@s.co.uk";
        authCredentials.ClientCredentials.UserName.Password = "xxxxx";
        AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);

        //Retreive the service

        IOrganizationService service = new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);



    }
}

i want the azure function to implement/run the custom workflow but i am not sure how

Sam
  • 149
  • 2
  • 15

1 Answers1

0

Hopefully this isnt too late. When I've needed to do something similar, I create an Action in Dynamics. The action will call the Workflow Activity.

I then call the action from my Azure Function App Code like this.

 OrganizationRequest req = new OrganizationRequest([insert action name here]);

 //execute the request
 OrganizationResponse response = organizationService.Execute(req);

Also I'd avoid putting credentials in your Azure Code. Ideally use Azure Key Vault for this. You can refer this post for ways to do this. Azure Function Authentication To Dynamics CRM Using Azure Key Vault

Andrew N
  • 502
  • 7
  • 20