Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
CRM tips from the vault – A series which I have started on request of my blog readers who are quite new to CRM. My blog focus on niche topics which mostly require a prior in-depth understanding of Dynamics 365. However in this series I will mostly cover the topics which are quite simple, used in almost every project implementations and more importantly have withstood the test of time.
Here is my Tip 4 – How to create a child record from Relationship field mappings in Dynamics CRM?
Field Mappings or relationship mappings has been there since the advent of Microsoft CRM Platform. And we so many times use it without even being aware of it. For example, when you convert a lead to opportunity, you will find the lead information is being copied to the opportunity. And how does this happen. This happens because there are field mappings enabled OOB for the relationship between lead and opportunity.
So if you open the relationship “opportunity_originating_lead” and check the mappings section, you can find quite a few fields being mapped there. Off-course you have add/ remove the mappings.
Well that’s good but here we are not going to do those mundane stuffs. Here we are going to accomplish a requirement where in we need to initialize a child record from the mappings using SDK code.
Fort that let’s take a simple requirement here. I have two entities – Parent Entity and Child Entity. Quite clear from the naming, I have set-up 1:N relationship between the Parent and Child entity.
In the relationship field mapping, I have mapped few fields from the parent entity to the child entity.
Parent Entity (new_cop1) ——————> Child Entity (new_field1)
Parent Entity (new_field2) —————–> Child Entity (new_field2)
This relationship mapping would work fine when you add the Child Entity record from a sub-grid on the Parent Entity record form
But how to handle the situation when you are creating the associated child record programmatically? Unfortunately in scenario, the field mappings are not respected. But wait there is some good news.
There is a specific request in SDK – InitializeFromRequest which does just that. So let’s see some code in action.
InitializeFromRequest initializeFromRequest = new InitializeFromRequest(); initializeFromRequest.EntityMoniker = new EntityReference(“new_parententity”, “<parentrecordguid>”); initializeFromRequest.TargetEntityName = "new_childentity"; InitializeFromResponse initializeFromResponse = (InitializeFromResponse)service.Execute(initializeFromRequest); Entity childEntity= initializeFromResponse.Entity; // the child entity record is just initialized from the field mappings and not yet created and not yet created childEntity.Id = service.Create(childEntity);
Awesome isn’t it? And trust me, this requirement is so common even these days but thanks to very low publicity of this request, such a hidden gem is unknown to many.
Hope this helps!
Debajit Dutta
(Microsoft MVP)
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.