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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Handle entity virtual fields in x++

(3) ShareShare
ReportReport
Posted on by 150
Hi,

If we have a table with a container field (that gets filled from the UI like this)
 
 
Now in the entity, i will need to be able to export and import those values

so let's say currently we have 2 values in the available list and one value in the selected list

in the entity i created 3 No/Yes fields (Is Computed Field : No)
Field1
Field2
Field3
 
when exporting for this certain record, i should see the Field1 and Field2 are both no. And field3 is Yes

and when importing i could make Field3 as No and Field2 as yes for example. And this should be reflected in the container field and they should appear correctly in the UI

What code to put in the entity to achieve this?
 
Categories:
I have the same question (0)
  • Martin Dráb Profile Picture
    238,740 Most Valuable Professional on at
    Are you sure you want to violate the first normal form of database design? The recommended design is storing the values in an table with 1:n relation, not in s single field, converted to binary data. Then you can do things like searching the data, index them, export them with data entities and so on.
     
    And if you insist on your design despite all the problem, do you also insist on a solution that can't deal with more than three values?
  • CU22120935-0 Profile Picture
    150 on at
    Hi @Martin Dráb,

    The container field in this table and the table itself is not in my model. So I need to stick with that.
    Quick question (even though i won't implement): if i use a table instead, i will still be able to use the same UI to move things from left to right?

    And yes for now I need three No/Yes fields, and each time they add something new, we will need to add another NoYes field. But it's been a while since they added anything. So hopefully no future changes. Is there another way?

    So now how can i handle the export and import in this case please and as I described in the question using the three fields and using another way if you have one?
  • Suggested answer
    Martin Dráb Profile Picture
    238,740 Most Valuable Professional on at
    Sure, the UI can be the same even with the correct data model. These are two separate things. You can find plenty of examples in the standard application where it's done this way.
     
    Your question seems to be how to work with your container in code, such as getting and setting a value at a particular index. Check out X++ container runtime functions in F&O documentation. You'll learn that you can use conPeek() to get a value and conPoke() to set a value. You can also use a special syntax involving [ ], but let's keep it simple here.
     
    Implementing your virtual fields will be trivial - you simply call conPeek() three times (with varying indexes) and assign a value to your three fields.
     
    On import, you'll create a container from your three virtual fields and assign the container to the table field.
     
    An alternative design would be having a single virtual field with containing all the values separated by a delimiter. An advantage is that it wouldn't require development when the number of values change. You can see this approach when importing and exporting financial dimensions.
  • CU22120935-0 Profile Picture
    150 on at
     
    I was also thinking that I should use conPeek in case of export. But the question is where? is it in postLoad method or mapDataSourceToEntity Method? but mapDataSourceToEntity doesn't get called in export when debugging

    Same thing for import, you said i should use conPoke(), but where? postLoad? (but this one gets called in both import and export) or mapEntityToDataSource or inserEntityDataSource or updateEntityDataSource? where to write the code?

    You also said that i can find plenty of examples that use the same UI, can you please tell me how? i click find references on class SysListPanel but didn't find something useful. Most forms don't have datasource or tmp table. What place shall i look at where i can find a table with container field?

    Also if I understood correctly, do you mean financial dimension table has a container field, and when using the entity, we can import the values by a delimiter using one virtual field. Can you please give me the entity name so that I see how it works exactly please?

    @Martin Dráb
  • CU22120935-0 Profile Picture
    150 on at
     
    Did u get a chance to look at my last reply? can you please take a look?
     
  • Suggested answer
    Martin Dráb Profile Picture
    238,740 Most Valuable Professional on at
    I'm sorry; I didn't get a notification about your reply.
     
    Use postLoad() for import and mapEntityToDataSource() for export. Note that you could have simply found the information in F&O documentation, namely Computed columns and virtual fields in data entities.
     
    When I said you can find examples in the standard application, I was responding to your question about using a table instead of a container. As I explained, your usage of a container violates the basic of relational DB design and you're unlikely to find that in the standard application. References of SysListPanel will show you examples of using tables to store the selected values. For instance, look at EntAssetDocuTypeListPanel class and notice how getData() takes the information about selected records from EntAssetDocuType table and addData() inserts records there.
     
    No, I said nothing like that "financial dimension table has a container field".  This is what actually said: "An alternative design would be having a single virtual field with containing all the values separated by a delimiter. [...] You can see this approach when importing and exporting financial dimensions." As you see, it's about a virtual field on an entity, not about a container field of a table. When you, say, export a record with financial dimensions, you may get a string like "Dim1-Dim2-Dim3", despite the fact that dimensions are actually stored in a completely different way in the database. And no, dimensions aren't stored in a container - they use tables, such as DimensionAttribute or DimensionAttributeValueCombination. Forget the idea that containers should be instead of tables.
  • CU22120935-0 Profile Picture
    150 on at
    Hi @Martin Dráb,

    Thank you for coming back to me
     
    • I did it like this and it worked, this is how you wanted the code right?
          public void postLoad()
          {
              next postLoad();
      
              container  containerField;
      
              Table1 table1 = Table1::find(this.Account);
      
              if (table1 && table1.containerField)
              {
                  for (int i = 1; i <= conLen(table1.containerField); i++)
                  {
                      int field = conPeek(table1.containerField, i);
      
                      switch (field)
                      {
                          case 0:
                              this.Field1 = NoYes::Yes;
                              containerField+=[field];
                              break;
      
                          case 1:
                              this.Field2 = NoYes::Yes;
                              containerField+=[field];
                              break;
      
                          case 2:
                              this.Field3 = NoYes::Yes;
                              containerField+=[field];
                              break;
      
                          default:
                              break;
                      }
                  }
              }
          }
      	
      	
      	public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
      	{  
      		if (dataEntityDataSourceStr(Table1Entity, Table1) == _dataSourceCtx.name())
      		{ 
      			Table1 table1DS = _dataSourceCtx.getBuffer();
      
      			container containerField;
      			 
      			if (this.Field1)
      			{
      				containerField += [0];
      			}
      			if (this.Field2)
      			{
      				containerField += [1];
      			}
      			if (this.Field3)
      			{
      				containerField += [2];
      			}
      			
      			table1DS.ContainerField   = containerField;
      
      
      			_dataSourceCtx.setBuffer(table1DS);
      	    }
      
      		next mapEntityToDataSource(_entityCtx, _dataSourceCtx);
      	}
       
     
    • And for some reason, it seems mapDataSourceToEntity gets called when importing and not just mapEntityToDataSource, any idea why?
       
    • For the financial dimension one, do u mean in my case i can't do it as one virtual field because of the container? or would it still work for me to replace those 3 No/Yes fields with one virtual field? if yes how will the code change?
  • CU22120935-0 Profile Picture
    150 on at
    Hi @Martin Dráb,

    Did you get a chance to look at my last reply? especially the last point.

    Thank you
  • Martin Dráb Profile Picture
    238,740 Most Valuable Professional on at
    Your code in postLoad() doesn't make a good sense to me. You use a loop, but then you ignore everything except of the first three elements, therefore you don't need the loop at all. You write values to containerField variable and then ignore it. You always set field values to Yes, regardless of what data you have in the container. If you insist of using a container and having the number of values hard-coded to three, we've already discussed how to do it: "you simply call conPeek() three times (with varying indexes) and assign a value to your three fields". Like this:
    public void postLoad()
    {
        next postLoad();
        
        this.Field1 = conPeek(table1.containerField, 0);
        this.Field2 = conPeek(table1.containerField, 1);
        this.Field3 = conPeek(table1.containerField, 2);
    }
    See Data entity method - when and where to use for a discussion about the methods.
     
    No, I don't mean that you "can't do it as one virtual field because of the container". On the contrary, I told you that it's an approach you could use ("An alternative design would be having a single virtual field with containing all the values separated by a delimiter.).
  • CU22120935-0 Profile Picture
    150 on at
    Hi @Martin Dráb,

    i will fix the postLoad and get back to you.


    But for doing it using one virtual field, what the code will be in postLoad and in mapEntityToDatasource?

    I tried to export financial dimensions entity, i can't see any delimiter even though this "UseValuesFrom" is a virtual field (DimensionAttributeEntity)

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 577 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 309

#3
Diego Mancassola Profile Picture

Diego Mancassola 259

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans