CRM 2011 Workflows: Finding the previous values

前端 未结 1 1419
轮回少年
轮回少年 2021-01-05 18:18

I currently have a workflow which is triggered when a certain decimal field is changed.

Is it possible to get the difference between the old and new values via a wor

相关标签:
1条回答
  • 2021-01-05 19:15

    Finally had the time to test this, and it is perfectly possible to retrieve the pre values in a workflow using a workflow assembly.

    Here's what I did:

    I created a workflow on Contact, with a trigger on LastName. The workflow contains a reference to the field lastname, and a custom workflow assembly. I opened a contact and changed it's lastname from 'Foo' to 'Bar'

    Code of the custom workflow assembly:

    protected override void Execute(CodeActivityContext context)
            {
                IWorkflowContext workflow = context.GetExtension<IWorkflowContext>();
                Entity preImage = workflow.PreEntityImages.Values.FirstOrDefault();
    
            string content = RetrievePreImageLastname(preImage);
    
            using (StreamWriter writer = new StreamWriter(@"C:\temp\log.txt", true))
            {
                writer.WriteLine("writing workflow assembly");
                writer.Write(content);
            }
        }
    
        public string RetrievePreImageLastname(Entity value)
        {
            if (value == null)
                return "PreImage is Empty";
    
            return string.Format("lastname pre image value: {0}", value.GetAttributeValue<string>("lastname"));
        }
    

    And this was the output:

    writing workflow assembly

    lastname pre image value: Foo

    Hope this helps anyone in for future use.

    0 讨论(0)
提交回复
热议问题