Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Overwriting a modifiedon date when status changed

(0) ShareShare
ReportReport
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,087 Super User 2024 Season 1 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,087 Super User 2024 Season 1 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

News and Announcements

Announcing Category Subscriptions!

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans