Archive
Set “Created On”,” Created By”, “Modified On”, “Modified By” fields using SDK/Data Import/Plug-in – Dynamics 365
I got an ask from one of my blog followers on how to set “Created On” field of a record, as it defaults to current date.
In this article I am going to explain various ways to set these values.
Override ‘Created On’ using Data Import:
- Using ‘Data Import’, setting the “Created On” is straight forward and all you need is to add a column “Created On” in your .csv file and import.
- For non-system admin users who import, make sure “Override Created on or Created by…” Privilege is checked in their Security Role.
Points to Ponder:
- Even though the privilege “Override Created on or Created by for records during data import” name suggests overriding “Created by” field but we cannot override “Created by” via Data Import.
Override ‘Created On’ using SDK:
- Set the “overriddencreatedon” field to the date, which you want your “Created On” date as.
- Post the execution of code, “Created On” value will be overridden with the Date set in “overriddencreatedon” field and “overriddencreatedon” field will default to todays date.
- For example, today is “01-01-2017” and you want the “Created On” as “01-01-2016”
- Set “overriddencreatedon” to “01-01-2016”
- After code execution, “Created On” will set to “01-01-2016” and “overriddencreatedon” will be today (i.e., 01-01-2017)
Sample code:
Entity contact = new Entity(“contact”);
contact[“firstname”] = “Contact Migrated”;
contact[“overriddencreatedon“] = new DateTime(2016, 01, 01);
_service.Create(contact);
Override “Created On”,” Created By”,” Modified On”,” Modified By” using Plug-in:
- To Override the fields, we need to go for plug-in approach.
- Register a “PreOperationCreate” plug-in and set the fields.
Plug-in Code:
public void Execute(IServiceProvider serviceProvider){
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));// 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 TargetEntity = (Entity)context.InputParameters[“Target”];// Set date in Past
TargetEntity[“createdon”] = new DateTime(2016, 01, 01);
TargetEntity[“modifiedon”] = new DateTime(2016, 01, 01);
// Set the GUID of non-calling user
TargetEntity[“createdby”] = new EntityReference(“systemuser”, new Guid(“5BB6EF5F-6BCC-E711-A831-000D3A1BE051”));
TargetEntity[“modifiedby”] = new EntityReference(“systemuser”, new Guid(“5BB6EF5F-6BCC-E711-A831-000D3A1BE051”));
}
}
- Once this plug-in executes, values will get overridden with the passed values.
Points to Ponder:
- As you would have noticed, it requires a plug-in to set the ”Created By”,” Modified On”,” Modified By” fields which is an overhead.
- But this approach is extremely useful in “Data Migration” as this is common ask while migrating historical client data.
- Have the plug-in step enabled during “Data Migration” and deactivate post migration.
Bulk Updating or Inserting records from Excel using CRM Data Import
Hi,
In CRM, you may come across scenarios where you need to Update/Insert a very large number of records very quickly. In these cases, opening each record’s form to make the change can be time-consuming.
We can handle this better by using CRM Data Import feature by which we can make bulk Update/Insert very quickly.
Below are the steps to achieve this. I am using ‘Contact’ entity for this article.
Steps :-
- Navigate to ‘Contact’ entity
- Click on “Export to Excel” button in the Ribbon menu
- In the “Export Data to Excel” dialog box which comes up, Select “Static worksheet with records” and check the “Make this data available…” check box.
- Save the file
- Open the Excel file and it looks as below
Important Points :-
* This Excel file has some unique characteristics which simplifies the data entry and re-import process.
* As you click on each cell, you will notice a pop-up that tells you format of the data and whether the field is required or not.
* Required fields are not enforced in Excel (i.e., Excel won’t validate even if you miss the value in required cell).
* If you miss a value in required field cell, the record wont be updated in the import process.
* You can enter “Lookup” values also, but the values must match with parent record.
For example, In this Excel “Parent Customer” cell is a lookup to the “Account” entity. So i need to give “Account” full name in the cell.
* If any wrong data entry in the lookup cell, the record will not be updated on import
- In the imported Excel (Above screen), If you observe, I don’t have “Middle Name” for any of my contacts. So I am giving middle name as “Updated” (Below screen)
- Next, I want to insert a new record “Rajeev Pentyala” to my contacts.
Important Point :-
– We can create new records by entering them in the bottom of Excel. (***Be sure to fill all required fields ***).
- So, In my last row of Excel, I enter “First Name”,”Last Name” ,”Email”,”Parent Customer” (i.e.,Valid full name of existing Account)
- Save the Excel file. (Ignore the warning and continue saving)
- In the CRM, Click on “Import Data” button in the Ribbon
- In the dialog window browse the saved file
- Click “Next” button
- In the next window, click on “Submit” button
- Refresh the CRM application
- Now you can see the newly inserted contact “Rajeev Pentyala” with the given values in Excel
- You can also verify the remaining contacts with the updated “Middle Name”
- This is how we handle “Data import” process
Tip :-
- To verify your import finished successfully or any problems with it.
- Navigate to “Settings -> Data Management -> Imports”
This article provides you a basic knowledge about Data import from Excel.
Hope it helps 🙂