Skip to main content

Notifications

Microsoft Dynamics CRM forum
Suggested answer

Overwriting a modifiedon date when status changed

Posted on by 25

Hi,

I have a requirement, where after updating the status of the record from open to complete, modified on date should be same as before the status update . We tried it using the plugin at the pre Operation stage and registered Pre-image to get the modified on date before status update. The date we got from preimage has been set to the modifiedon date to the target entity form the plugin context.

modified on date is not updating with the modified on date that obtained from the preimage, where it is updating with the current date&time when status is updated. When I debugged the code it's showing that preimage modifiedon date is getting passed to the "activity["modifiedon"]".we have checked with the other plugin if any plugin is getting triggered after status updated, there are no plugins or other activities are getting triggered or updating the entity as we checked in the audit history. In audit history it's showing status update information and no other field is getting updated after status update.

Overwriting modifiedon date is working and reflecting for other fields update except status update.

Can one help me regarding this issue, why overriding modifiedon on status change is not working?

  • Hareesh Ganoji Profile Picture
    Hareesh Ganoji 25 on at
    RE: Overwriting a modifiedon date when status changed

    Below is the code that we are using for overriding the modifiedon date at pre-operation stage and  it is working for rest of the fields other than status field. If I am updating name field when the time is X the modifiedon contains y time, after updating the name field the modified on time is beign overwritten with the y time.

    public void Execute(IServiceProvider serviceProvider)

           {

               try

               {

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

                   IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                   IOrganizationService service = servicefactory.CreateOrganizationService(context.InitiatingUserId);

                   ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

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

                   {

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

                           Entity preActivity = (Entity)context.PreEntityImages["PreActivity"];

                           DateTime modifiedOnfromPreImage = preActivity.GetAttributeValue<DateTime>("modifiedon");

                           if ( activity.Attributes.Contains("statecode"))

                           {

                               int status = activity.Attributes.Contains("statecode") ? ((OptionSetValue)activity.Attributes["statecode"]).Value : 0;

                               if (status == 1) //complete

                               {

                                   activity["modifiedon"] = modifiedOnfromPreImage;

                               }

                           }

                       }

                       else

                       {

                           return;

                       }

               }

               catch (Exception)

               {

                   throw;

               }

           }

  • Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: Overwriting a modifiedon date when status changed

    I don't know your code and the logic behind it, but if you are updating a record when the time is X the modifiedon will contain X, if the time is Y the modified on will contain Y, you can't modify in Y time and put X time.

  • Hareesh Ganoji Profile Picture
    Hareesh Ganoji 25 on at
    RE: Overwriting a modifiedon date when status changed

    Thank you Preite

    But When we check for all other fields which are available in entity modifiedon date is being overwritten with the plugin. When we check for status field update it is not getting overwritten.

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: Overwriting a modifiedon date when status changed

    modifiedon is an internal field, every time the record is updated the date changes and you cannot set a custom value inside it.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,469 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans