Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Possible to update "Modified By" field using workflow?

Posted on by Microsoft Employee

Hello, I want to create a workflow that updates the "Modified By" field based on a text field called "Prepared By".  Prepared By contains a user's full name.  Is this possible?  When I try to add an "Update Record" step in my workflow, it doesn't seem to let me update the Modified By field.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Possible to update "Modified By" field using workflow?

    As an update, I've been working on a pre-validation plug-in for this.  I believe it is close to being done.  However, it is still not successfully updating the Modified By field.  I was wondering if someone could take a look and see if I am missing anything, making any mistakes, or give some advice on how I can successfully get this to update the "Modifed By" field based on the "Prepared By" text field.

    Here is my code currently:

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using System;

    using System.Linq;

    namespace TimClassLibrary1.Plugins

    {

       public class CreateUpdateContact : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               var service = factory.CreateOrganizationService(context.UserId);

               tracingService.Trace("Start plugin");

               tracingService.Trace("Validate Target");

               if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))

                   return;

               tracingService.Trace("Retrieve Target");

               var target = (Entity)context.InputParameters["Target"];

               String message = context.MessageName.ToLower();

               SetCreatedByAndModifiedBy(tracingService, service, target, message);

           }

           private void SetCreatedByAndModifiedBy(ITracingService tracingService, IOrganizationService service, Entity target, string message)

           {

               tracingService.Trace("Start SetPriceList");

               tracingService.Trace("Validate Message is Create or Update");

               if (!message.Equals("create", StringComparison.OrdinalIgnoreCase) && !message.Equals("update", StringComparison.OrdinalIgnoreCase))

                   return;

               tracingService.Trace("Retrieve Attributes");

               var createdByReference = target.GetAttributeValue<EntityReference>("new_createdby");

               var modifiedByReference = target.GetAttributeValue<EntityReference>("new_modifiedby");

               tracingService.Trace("Retrieve And Set User for Created By");

               RetrieveAndSetUser(tracingService, service, target, createdByReference, "createdby");

               tracingService.Trace("Retrieve And Set User for Modified By");

               RetrieveAndSetUser(tracingService, service, target, modifiedByReference, "modifiedby");

           }

           private void RetrieveAndSetUser(ITracingService tracingService, IOrganizationService service, Entity target, EntityReference reference, string targetAttribute)

           {

               tracingService.Trace("Validating Reference");

               if (reference == null)

                   return;

               tracingService.Trace("Retrieving and Validating User");

               var user = RetrieveUserByName(service, reference.Name, new ColumnSet(false));

               if (user == null)

                   return;

               tracingService.Trace("Setting Target Attribute");

               target[targetAttribute] = user.ToEntityReference();

           }

           private Entity RetrieveUserByName(IOrganizationService service, string name, ColumnSet columns)

           {

               var query = new QueryExpression

               {

                   EntityName = "systemuser",

                   ColumnSet = columns,

                   Criteria = new FilterExpression

                   {

                       FilterOperator = LogicalOperator.And,

                       Conditions =

                       {

                           new ConditionExpression

                           {

                               AttributeName = "fullname",

                               Operator = ConditionOperator.Equal,

                               Values = { name }

                           }

                       }

                   }

               };

               var retrieveResponse = service.RetrieveMultiple(query);

               if (retrieveResponse.Entities.Count == 1)

               {

                   return retrieveResponse.Entities.FirstOrDefault();

               }

               else

               {

                   return null;

               }

           }

       }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Possible to update "Modified By" field using workflow?

    Kokulan,

    Since I'm having trouble opening the Plugin Registration tool, I've been trying to create a Custom Workflow Activity to do get this to work.  Would you be able to take a look at my code and see if I'm on the right track, if it is even possible to update the "Modified By" field using a custom workflow activity? I know you mentioned a pre-validation plug-in would be best.

    My code and the error I'm running into can be found here: community.dynamics.com/.../need-help-fixing-error-sandboxfault-throwifguidempty-entityid---may-be-a-coding-issue-with-my-custom-workflow-activity

    Or would you be able to provide some sample code for the pre-validation plug-in?

    Thanks!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Possible to update "Modified By" field using workflow?

    Kokulan,

    I tried to open the Plugin Registration tool but it does not seem to be working.  Each time it prompts me to log in, then once I'm logged in it tells me to select an environment in my organization, then once I click the environment it tells me to log in again.  This happens over and over.  Any idea how I can get around this?

    Also, any advice on how I can update my code that gets the Prepared By lookup value to actually pass that value into the Modified By field?

    Thank you.

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Possible to update "Modified By" field using workflow?

    Hi

    You do not need a CWA for this. I would suggest you can copy the code in your CWA to get the user lookup value fro Prepared By text field value.

    You can then set the modified by in the plugin

    You have to create a pre-validation plugin.

    Please follow the link below to get an idea about how you can create a pre-validation plugin, let me know if you need any further assistance on this

    carldesouza.com/dynamics-365-pre-validation-plugin

    In the article, it shows how to create using C# DLL but since you have Developer toolkit installed, you can create proper plugin project using that. so you do not need to follow first few steps as is.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Possible to update "Modified By" field using workflow?

    Kokulan,

    Thank you.  How would I set the Modified By field?  Would this be done within the CWA?

    Also, I'm not sure I fully understand what should go in this pre-validation plugin.

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Possible to update "Modified By" field using workflow?

    Hi

    As suggested by Radu it is not very straightforward or easy to change the Modified By.

    However, I did not have to do this much but the following approach should work for you

    01. You can create a Pre-Validation plugin for the record event - Let's say Quote Create / Update.

    02. In the Pre-Validation plugin, you can use the similar code you used on CWA to get the user and then set that to Modified By and CRM should keep whatever set in the pre-validation stage.

  • Suggested answer
    Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Possible to update "Modified By" field using workflow?

    Hello,

    ModifiedBy is a platform field and can't be set via the SDK directly.

    What you can do is influence the value of this field, whereby you can create a plug-in or a workflow assembly that when triggered fetches the value of the prepared by field, impersonates that user and then makes an update on the record - thus will set the ModifiedBy to have the same value as Prepared By.

    Still this is not very accurate as anyone who makes any update afterwards to the record will be set as Modified By.

    My suggestion is to create a custom field and use that for whatever purpose you would have with the Modified By field, rather than relying Business Logic on a Platform owner field.

    Regards,

    Radu

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans