3
votes

We can create an entity via the Organization Service, that has a reference to another entity. Is there a way to trigger the field mappings for this entity relationship via the Organization Service, so that all the required data is pulled through the inbuilt mappings for that relationship?

This is required as we have a POST create plugin that runs on that entity that is expecting the data to be populated via the mappings.

3
It sounds like you are dynamically creating entities and you want the entity to be pre-populated with data from another table, correct? Are we talking about multiple rows or just one? Are you doing this because of some security issue?Eccountable
+1, very interesting questionGuido Preite

3 Answers

1
votes

It sounds like you need to query the metadata service to retrieve the required fields.

Luckily with UR12 on 2011 (or in your case 2013), you can get only what's changed within the metadata and keep adequate performance.

http://msdn.microsoft.com/en-us/library/jj863599.aspx#BKMK_RetrieveJusttheMetadataYouNeed

1
votes

Someone else wrote the code for you:

http://code.msdn.microsoft.com/Automatic-mapping-of-child-9df6db11

When you create a child entity for an existing entity through the frontend CRM 2011 automatically maps some fields if an entity map exists for that relation. But when you create the child entity through the SDK nothing happens. This example tries to offer a solution for that.

0
votes

You have to use InitializeFromRequest to achieve this functionality.

InitializeFromRequest initialize = new InitializeFromRequest();

// Set the target entity (i.e.,Contact)
initialize.TargetEntityName = "contact";

// Create the EntityMoniker of Source (i.e.,Account)
initialize.EntityMoniker = new EntityReference("account", new Guid("<GUID>"));

// Execute the request
InitializeFromResponse initialized = (InitializeFromResponse)orgService.Execute(initialize);

// Read Intitialized entity (i.e.,Contact with copied attributes from Account)
if (initialized.Entity != null)
{
// Get entContact from the response
Entity entContact = initialized.Entity;

// Set the additional attributes of the Contact
entContact.Attributes.Add("firstname", "abc");
entContact.Attributes.Add("lastname", "xyz");

// Create a new contact
orgService.Create(entContact);
}

https://rajeevpentyala.com/2017/01/26/create-a-child-record-by-copying-mapping-fields-from-parent-using-crm-sdk/