D365: When and how to use mapEntityToDatasource() method

Purpose:

Understand when and how to use mapEntityToDatasource() method in data management.

Application:

Dynamics 365 for Finance and Operations

Prerequisites:

  • Basic understanding of data entities and how data management works

Business requirement:

Business requirement is to initialize datasource fields or create datasource table records while importing data into D365.

Solution:

We can take the code below as an example to see how can we initialize datasource fields while importing data into D365.

Code

/// <summary>
/// This method checks if there is an existing <c>VendInvoiceInfoSubTable</c> record, if not we create one
/// and initialize the DocumentOrigin field of the <c>VendInvoiceInfoTable</c> record.
/// </summary>
/// <param name = "_entityCtx"></param>
/// <param name = "_dataSourceCtx"></param>
public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
{
	if (_entityCtx.getDatabaseOperation() == DataEntityDatabaseOperation::Insert)
	{
		switch (_dataSourceCtx.name())
		{
			case dataentitydatasourcestr(VendorInvoiceHeaderEntity, VendInvoiceInfoTable):
				VendInvoiceInfoTable vendInvoiceInfoTable = _dataSourceCtx.getBuffer();
				vendInvoiceInfoTable.DocumentOrigin = DocumentOrigin::Service;
				this.VendorInvoiceReviewStatus = VendInvoiceRequestStatus::Draft;
				break;
		}        
	}

	super(_entityCtx, _dataSourceCtx);
}

Leave a comment

Blog at WordPress.com.

Up ↑