Monday 30 June 2014

CRM 2011/ 2013 Modify CreatedOn, CreatedBy, ModifiedOn, and ModifiedBy Using SDK C#

Often, we have requirements to import data from external data source to CRM and to keep the history, we should keep on eye for these following fields:

1. CreatedOn
2. CreatedBy,
3. ModifiedOn,
4. ModifiedBy

And we often think to update the database directly, yes, it is actually possible.

But, beside it is unsupported way, it is only applicable for those who implement CRM OnPremise, but in fact, we also have another CRM Online.

So, to update those fields using SDK, you can use these code lines:

TargetEntity["createdon"] = new DateTime(2011, 8, 8);
TargetEntity["overriddencreatedon"] = new DateTime(2011, 8, 8);
TargetEntity["createdby"] = new EntityReference("systemuser", new Guid("DDF26A49-4CBA-E311-9400-001CC4EECDD6"));
TargetEntity["modifiedby"] = new EntityReference("systemuser", new Guid("F1F26A49-4CBA-E311-9400-001CC4EECDD6"));
TargetEntity["modifiedon"] = new DateTime(2012, 8, 8);

Do not forget to change to your dynamic values for those assigned field values.

*You should use Late Bound since you cannot use Early Bound:

SNAGHTML1427c435

It will give error says “it is read only”;
And here is the result:

image

 image

Hope it helps!

37 comments:

  1. Hi

    Do you have a sample project for this code, I have been trying to do this with Dynamics CRM Online and I am having no success. I have tried a console app, a custom workflow (no success) and a plugin (seems to work for a single record, but isn't working for a bulk import).

    If you have any guidance I would be really grateful.

    ReplyDelete
  2. Hi Jonathan,

    I've tried using the plugin and console apps.
    You are trying to create custom import?

    ReplyDelete
  3. Could you please supply more sample code, I do not fully understand how to do this from reading this blog post. I've tried to update "modified on" field by doing the entity.attributes["modifiedon"] = __ method, but this does not work. And looking around online, this is about the only place on the entire world wide web that says this is not impossible to do via API , therefore I feel you should supply some more information when you say this can be done.

    ReplyDelete
    Replies
    1. HI Simon,

      Yes, basically many websites said it is not possible, and if I use Early Bound it is also not possible.
      I used the code inside a plugin and it worked after I tried the impossible.
      Thx.

      Delete
  4. I'v tried to do this in a console app.
    "createdon" is overridden by the current datetime. The "overriddencreatedon" stores the date in the "createdon". That's what I want.
    But... "createdby" and/or "createdonbehalfby" cannot be set. The values are overridden by the logged in user.

    ReplyDelete
    Replies
    1. I guess you need a plugin to override it back again, like what I did.
      createdonbehalfby I haven't tried.

      Thanks.

      Delete
  5. Hi Aileen... I can't update createdby field with you istructions! :-(

    ReplyDelete
  6. Marco,

    I tried using plugin and it worked, captured in the picture..
    Now for other apps, to be honest I am becoming unsure, need to try again, thanks Marco for the comment.:)

    ReplyDelete
  7. Aileen Nice Blog!! Same thing I'm doing in CRM2015 online version by C# but it's only updating Created on Field but Created by and Modify by is not updating.

    Can you tell me in CRM2015 it will work???

    ReplyDelete
  8. I have just tested it against CRM 2015 On-line spring release and it worked fine. The trick here is that you MUST register the plug-in step on Pre-Operation execution and just update the incoming target entity in the plug-in context. You don't need to use service.update as well as it is in Pre-Operation it will be updated at the end of the execution pipeline. The code is as below:
    Hope that help.

    public void Execute(IServiceProvider serviceProvider)
    {
    var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

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

    // The InputParameters collection contains all the data passed in the message request.
    if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"] is Entity)
    {
    // Obtain the target entity from the input parameters.
    Entity entity = (Entity)context.InputParameters["Target"];
    entity["modifiedon"] = new DateTime(2013, 8, 8);
    }

    }

    ReplyDelete
  9. I too can confirm this solution does work if you want the status to remain a default status for the activity. However if you attempt to set the status during data migration it 1) Creates the record 2) Updates the status of the record. Unfortunately, during this second stage, it updates the Modified On and Modified By to be the time of the import and which ever user you chose to import the data as. To get around this you need to first create a flag in your CRM entity and each import record (default to false). Then cut an additional plugin message step for the Update:

    if (preImageEntity.Contains("csp_ismigrationcomplete") && (bool)preImageEntity["csp_ismigrationcomplete"] == false && preImageEntity.Contains("csp_sugarmodifiedon"))
    {
    localContext.TracingService.Trace("PrePhonecallUpdate: Setting the Modified On Date equal to the Sugar Modified On Date.");

    targetEntity["modifiedon"] = (DateTime)preImageEntity["csp_sugarmodifiedon"];
    targetEntity["csp_ismigrationcomplete"] = false; //ensure we set it only once
    }

    I am not sure if you need both Create and Update but possibly records with efult status may not fire the update. You'd certainly need it for any records that didn't have a status set during import.

    All a bit clunky but does the job.

    ReplyDelete
    Replies
    1. I know this is a very old post, but it's one of the only ones that comes up when searching for this topic.

      I just tried this recently in Dynamics 365 when importing Activities and had the following results:

      Using the "Pre-Operation-Create" plugin to set the modifiedon date worked fine as long as the Activity is Open

      If the Activity is closed, the migration (performed in SSIS with Kingswaysoft) appears to do the insert as two steps:
      1. Create the Activity
      2. Update the Activity with the "close" state


      The second operation (the update) of course then overrides the modifiedon date again.
      I saw the post by Ashley Childs where they then write a second plugin for the "Pre-Operation-Update" however when I tried this it didn't work for me

      As I could not get the update working I had to revert all my changes because the mixed results (correct date for open records, wrong date for closed records) was a more confusing result for my client

      Delete
  10. Thanks Aileen, helped me today for CRM 2015 migration.

    ReplyDelete
  11. Thanks! Above code works for CRM 2016 online.

    ReplyDelete
  12. Thanks Aileen!! Above code works for Dynamics 365 online.

    ReplyDelete
  13. Hi there,

    I have inserted several notes but their createdon date are the date i ran the program.

    I know i could borrow overridencreatedon to keep historical date.

    Does overridencreatedon could only be updated on record create?

    Thanks
    Randy

    ReplyDelete
  14. Just wanted to say I love to go through your blog and look forward to all your posts!
    Keep up the superb work!
    Customized CRM Solutions
    Customer Relationship Management Solutions in Bangalore

    ReplyDelete
  15. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
    rpa training in bangalore
    rpa training in chennai
    rpa training in pune
    best rpa training in bangalore

    ReplyDelete
  16. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command

    python training in chennai
    python course in chennai
    python training in bangalore

    ReplyDelete
  17. Read all the information that i've given in above article. It'll give u the whole idea about it.
    python training in rajajinagar
    Python training in bangalore
    Python training in usa

    ReplyDelete
  18. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    AWS Training in Bangalore
    AWS training in sholinganallur
    AWS training in Tambaram
    AWS training in Velachery

    ReplyDelete
  19. Visit for AWS training in Bangalore:- AWS training in Bangalore

    ReplyDelete
  20. Thanks for sharing the information...
    AWS Training in Bangalore | AWS Cours | AWS Training Institutes - RIA Institute of Technology
    - Best AWS Training in Bangalore, Learn from best AWS Training Institutes in Bangalore with certified experts & get 100% assistance.

    ReplyDelete
  21. Really nice post. Thank you for sharing amazing information...
    AWS Training in Bangalore

    ReplyDelete
  22. Really Great Post & Thanks for sharing.

    Oflox Is The Best Airtel Fancy Numbers & Airtel VIP Number Seller.

    ReplyDelete

  23. I am very happy when read this blog post because blog post written in good manner and write on good topic. Thanks for sharing valuable information about AWS Course.

    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  24. Top Digital Marketing training in India with Customized Digital Marketing course for All. Classroom, Online and Bootcamp classes by Experienced Trainers. To know more please visit our website.
    https://onlineidealab.com/digital-marketing-courses/

    ReplyDelete
  25. The article was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents.
    by Cognex offers AWS Training in Chennai, Microsoft azure, prince2 foundation courses are also available in cognex.

    ReplyDelete
  26. Simply wish to say your article is as astonishing. The clarity in your post is simply great, and I could assume you are an expert on this subject. Same as your blog i found another one Oracle Project Portfolio Management Cloud Software .Actually I was looking for the same information on internet for Oracle PPM and came across your blog. I am impressed by the information that you have on this blog. Thanks a million and please keep up the gratifying work.

    ReplyDelete
  27. I would like to take this opportunity to thank the author for his incredible work. I believe Customer relationship management is essential for every business to create a loyal customer base. keep doing it. all the best
    top accounting software

    ReplyDelete
  28. Thanks for providing this information it was rally helpful for . I got to know that these are the points which we should kep in mind while purchasing a marketing tool. Want to read more then please check out the best 10 CRM software in 2022.

    ReplyDelete
  29. We SVJ Technocoat are the leading Service Provider and Exporter of an extensive array of PVD Coating Service and Vapor Deposition Coating Service etc. We are a well known firm for providing excellent quality coating services across the nation and in a timely manner. Owing to our improvised business models, our professionals are offering integrated solutions for our clients.

    ReplyDelete

My Name is..