web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / Data entity import vir...
Finance forum

Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

(0) ShareShare
ReportReport
Posted on by

Hi,

I have a problem when importing data entity with virtual fields. 

I want to import data in next table:

pastedimage1568025080521v2.png

You can see that it has relations with PMEBuilding (Ground etc) table using RefRecId and RefTableId. It also

The thing is that I want user to have in Excel BuildingId, GroundId etc. and not RefRecId.

So I created data entity and added unmapped fields ObjectId and ObjectType.

Export works fine, but when I try to import it keeps telling me:

pastedimage1568025292249v3.png

I overridden ofcourse postLoad and mapEntityToDataSource methods:

 public void postLoad()
    {
        super();
       
        switch (this.RefTableId)
        {
        
            case tableNum(PMEGround):
                this.ObjectType = PMEObjectType::Ground;
                this.ObjectId = PMEGround::findRec(this.RefRecId).groundId;
                break;
           
            case tableNum(PMEBuilding):
                this.ObjectType = PMEObjectType::Building;
                this.ObjectId = PMEBuilding::findRec(this.RefRecId).buildingId;
                break;
            case tableNum(PMEParcel):
                this.ObjectType = PMEObjectType::Parcel;
                this.ObjectId = PMEParcel::findRec(this.RefRecId).parcelId;
                break;
            case tableNum(PMESection):
                this.ObjectType = PMEObjectType::Section;
                this.ObjectId = PMESection::findRec(this.RefRecId).sectionId;
                this.Building = PMESection::findRec(this.RefRecId).buildingId;
                break;
            case tableNum(PMEZone):
                this.ObjectType = PMEObjectType::Zone;
                this.ObjectId = PMEZone::findRec(this.RefRecId).zoneId;
                this.Building = PMEZone::findRec(this.RefRecId).buildingId;
                this.Section = PMEZone::findRec(this.RefRecId).sectionId;
                break;
               
        }

  public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
    {
        super(_entityCtx, _dataSourceCtx);
        if (_dataSourceCtx.name() == dataEntityDataSourceStr(PMRealEstateOrganizationTypeEntity, PMGBuildingOrganizationType))
        {
            PMGBuildingOrganizationType realEstateOrgType = _dataSourceCtx.getBuffer();
           
            switch (this.ObjectType)
            {
                case PMEObjectType::Ground:
                    realEstateOrgType.RefTableId = tableNum(PMEGround);
                    realEstateOrgType.RefRecId = PMEGround::find(this.ObjectId).RecId;
                    break;
                case PMEObjectType::Building:
                    realEstateOrgType.RefTableId = tableNum(PMEBuilding);
                    realEstateOrgType.RefRecId = PMEBuilding::find(this.ObjectId).RecId;
                    break;
                case PMEObjectType::Parcel:
                    realEstateOrgType.RefTableId = tableNum(PMEParcel);
                    realEstateOrgType.RefRecId = PMEParcel::find(this.ObjectId).RecId;
                    break;
                case PMEObjectType::Section:
                    realEstateOrgType.RefTableId = tableNum(PMESection);
                    realEstateOrgType.RefRecId = PMESection::find(this.Building, this.ObjectId).RecId;
                    break;
                case PMEObjectType::Zone:
                    realEstateOrgType.RefTableId = tableNum(PMEZone);
                    realEstateOrgType.RefRecId = PMEZone::find(this.Building, this.Section, this.ObjectId).RecId;
                    break;
               
            }
        }
But it DOESN'T help. Can someone help me?
Just note that RefTableID and RefRecId are part of primary key of this table which I am trying to import data.
Thanks in advance
I have the same question (0)
  • Martin Dráb Profile Picture
    236,289 Most Valuable Professional on at
    RE: Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

    First of all, check if you have correct data on the input. Your code above won't throw any error when an invalid ObjectType is provided; it'll keep RefTableId and RefRecId fields empty (despite the fact that they're mandatory, according to the validation error message).

    You can also debug the code to see what happens in your code and when the validation gets called.

  • Community Member Profile Picture
    on at
    RE: Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

    I checked and I have correct data. I tried to debug but without success because it fails in validateWrite on entity and I can't debug mapEntityToDatasource, the debugger doesn't hit. Is there any way to debug this method?

  • André Arnaud de Calavon Profile Picture
    299,069 Super User 2025 Season 2 on at
    RE: Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

    Hi devgirlax,

    I do assume in the target table, the fields reftableid and refrecid are mandatory. How did you set the properties for these fields on the data entity? Try to have them not mandatory.

  • Community Member Profile Picture
    on at
    RE: Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

    Hi Andre, yes, they are mandatory since they are the part of the primary index of target table. But they should be filled in my mapEntityToDatasource method, am I right? But I cannot debug that method since the debugger wont hit.

    P.S. When I set properties on Data entity for those fields not to be mandatory, I can successfully import, but only new rows, when it comes to update I am getting duplicate key error because it does not recognize that it is update database operation, it marks it as an insert, so ofcourse I can't insert. What should I do?

  • Martin Dráb Profile Picture
    236,289 Most Valuable Professional on at
    RE: Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

    There is nothing special needed to debug mapEntityToDataSource() - simply put a breakpoint there, attach to IIS/IIS Express and run an import.

    But I think you're wrong in assuming that you have a problem with debugging. The debugger doesn't stop on your breakpoint because debugging is broken; it's simply because the method isn't called.

    mapEntityToDataSource() is called when the framework is trying to persist an entity to database, but its validateWrite() method is called before such an attempt and if it returns false, nothing is being written and mapEntityToDataSource() is skipped too. I guess that's your case.

  • Community Member Profile Picture
    on at
    RE: Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

    But before I map my fields refRecId and refTableId thanks to objectId and objectType they are empty so validateWrite will return false always in that case? Where should I then write my code? Do you see maybe that something is wrong with my code? I explained what I want to achieve sto please help me with your suggestions.

  • Martin Dráb Profile Picture
    236,289 Most Valuable Professional on at
    RE: Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

    Note that I'm talking about validation of the entity, not of the table. Also note that you've never showed the entity to us, therefore I can only guess how it looks like.

    The validateWrite() method on the entity does not call validateWrite() of target tables (at least not by default), therefore if it fails complaining about reference fields, it sound like you have mandatory reference fields on the entity. If it's the case, do you see any reason for having them there? If not, delete them. If you need them, set their Mandatory property to No.

    And again, don't forget that you can use the debugger to see what's going on. For instance, my theory is that validateWrite() on the entity return false. You can easily test whether this theory is correct or not and then plan next steps based on this knowledge.

  • Community Member Profile Picture
    on at
    RE: Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

    I need those fields. I set their mandatory property to No and validateWrite method returns true, but the problem is that it tries insert instead of update. Sometimes, using data entity I want to update current records, but it does insert and it causes duplicate key error. I am using debugger and I can't see why it does that.

  • André Arnaud de Calavon Profile Picture
    299,069 Super User 2025 Season 2 on at
    RE: Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

    If these fields are empty, it means that the coding which you shared in your initial post is not working correctly. This should fill the correct values. Did you try to debug this coding?

  • Community Member Profile Picture
    on at
    RE: Data entity import virtual fields (mapping virtual fields with RefTableId and RefRecId)

    These fields do not exist in Excel file. Instead of refrecid and reftableid (since user doesn't know which values to provide) there are object type (building) and object id (for example Building 123) and using those I want to find which is the table id and reference to a record. This table that I am trying to import data is child to Building/Ground/Section etc. tables, but I am not using classic Id, I am using refrecid and reftableid.

    When I am importing NEW values it works correctly, but when I want to update, it doesn't find the target and since the target.recid is 0, it tries to insert which causes duplicate key error.

    Do you maybe know some similar standard data entity where can I have a look? 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans