I recently worked on a Dynamics 365 for Sales project where we needed to migrate Marketing List data. If you have ever tried to do this using the out of box import data tool you will find it’s not possible. The Marketing List can be imported however the linkage to accounts / contacts or leads cannot.
The Solution
In summary the solution is fairly straight forward and involves a new entity and a plug-in. Once these two are in place you can import the list members into the new entity and the plugin will take care of setting up the Marketing List relationship.
I’ve outlined the steps to do this below:
- Create a new entity called List Member with the following attributes:
Schema Name: new_listmember
Many to One Relationships (N:1):
- Contact:
- Relationship Name: new_listcontact
- Lookup Field Name: new_contactid
- Account:
- Relationship Name: new_listaccount
- Lookup Field Name: new_accountid
- Lead:
- Relationship Name: new_listlead
- Lookup Field Name: new_leadid
- Marketing List:
- Relationship Name: new_marketinglist
- Lookup Field Name: new_marketinglistid
- Publish the entity
- Create a new plugin to create the marketing list association as the a new List Member record is created.
Register the plugin and add a step with the following attributes:
Message: Create
Primary Entity: new_listmember
Execution Stage: PostOperation
The plugin code is outlined below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
IPluginExecutionContext oContext = ((IPluginExecutionContext)(oServiceProvider.GetService(typeof(IPluginExecutionContext)))); IOrganizationService oServicProxy; Entity oPassedEntity; string sMarketingListGUID = ""; string sEntityGUID = ""; try { //Setup the service: IOrganizationServiceFactory oServiceFactory = ((IOrganizationServiceFactory)(oServiceProvider.GetService(typeof(IOrganizationServiceFactory)))); oServicProxy = oServiceFactory.CreateOrganizationService(oContext.UserId); //Ensure the context is correct: if (oContext.InputParameters.Contains("Target") && oContext.InputParameters["Target"] is Entity) { oPassedEntity = (Entity)oContext.InputParameters["Target"]; if ((oPassedEntity.LogicalName != "new_listmember")) { return; } } else { return; } //Check if it's an Account: if (oPassedEntity.Attributes.Contains("new_accountid") == true) { EntityReference oAccountRef = new EntityReference(); oAccountRef = (EntityReference)oPassedEntity["new_accountid"]; sEntityGUID = oAccountRef.Id.ToString(); } //Check if it's a contact: if (oPassedEntity.Attributes.Contains("new_contactid") == true) { EntityReference oContactRef = new EntityReference(); oContactRef = (EntityReference)oPassedEntity["new_contactid"]; sEntityGUID = oContactRef.Id.ToString(); } //Check if it's a Lead: if (oPassedEntity.Attributes.Contains("new_leadid") == true) { EntityReference oLeadRef = new EntityReference(); oLeadRef = (EntityReference)oPassedEntity["new_leadid"]; sEntityGUID = oLeadRef.Id.ToString(); } //Get the Marketing List ID if (oPassedEntity.Attributes.Contains("new_marketinglistid") == true) { EntityReference oMarketingListRef = new EntityReference(); oMarketingListRef = (EntityReference) oPassedEntity["new_marketinglistid"]; sMarketingListGUID = oMarketingListRef.Id.ToString(); } //Add the Marketing List Member: if(sMarketingListGUID.Length > 0 && sEntityGUID.Length > 0) { AddMemberListRequest oRequest = new AddMemberListRequest() { ListId = new Guid(sMarketingListGUID), EntityId = new Guid(sEntityGUID) }; oServicProxy.Execute(oRequest); } } catch (Exception ex) { throw new InvalidPluginExecutionException("List Member Plugin Error - " + ex.ToString()); } |
- Import the data using the List Member entity.
Note: If importing a mixture of marketing list types (i.e. contacts and accounts and /or leads) ensure each List Member record contains only one reference. I.e. split out contacts and accounts into separate list member records.
Good luck with the import! Any questions, please comment below.