Many-to-many relationship using 2 grids in the same form - axapta

I am new to AX, so it could be a simple question.
Imagine two tables Users and Groups in many-to-many relationship:
One user can belongs to many groups.
One group can belongs to many users.
So, I created 3 tables in AX, to represent this relationship:
User
Group
GroupUser (intermediate table used to create the many-to-many relationship with PKs from User and Group tables)
Now imagine I have a form with 2 grids.
Each grid has only one column not editable.
One grid with the Users list (with the User table defined as DataSource) and another with the Group list (with the Group table defined as DataSource).
Everything is displaying fine, but I would like to have the behaviour that when selecting a Group just appears the Users it belongs, and them I can select the user.
How to do this last part ?

Showing users belonging to a group involves using an exists join on the GroupUser table.
It was not clear to me from your description, whether this was the case always or only when a group was selected.
Make a new form datasource using GroupUser with JoinMode set ExistsJoin and linked to the User table.
If only filtered when a group is selected, make the GroupUser datasource disabled:
user_ds.query().dataSourceTable(tableNum(GroupUser)).enabled(<group is selected>);

Related

How to sort a table for which its datasource is a relation

I have a Page with a Table for which its datasource is a relation and needs to be sorted based on fields from another model:
Page
Datasource = Indicators
Table
Datasource = Indicators [one] : MetadataText [many] (relation)
The Table needs to be sorted based on a field from another Model called MetadataField, which has a one to many relation with MetadataText.
I have the datasource of MetadataField sorted. But the content in the Table appears in random order. When I first access the application, the Table is sorted by the order that the records were loaded. After view some records, the sorting of the records changes and keeps changing.
I am using Google Drive tables.
You can easily sort related records by one of the fields that belong to the related record itself, but only once (you'll received those records sorted from server).
But it seems, that you want to sort related records by their related record. App Maker will not be your friend in this case... but javascript will be! Since App Maker loads all related records you can safely sort them on client using javascript:
indicatorsDatasource.load(function() {
indicatorsDatasource.items.forEach(function(indicator) {
indicator.MetadataTexts.sort(function(a, b) {
return /* here goes your sorting logic */;
});
});
});
It will work in O(n * m * log(m)) in case you have n Indicators on the page and every indicator has m associated MetadataTexts. If you want to let users to sort related records by clicking table's header, you'll need to implement that logic on your own. So... all this hassle leads us to alternative solution! What if we decouple related records and introduce separated datasource for them? Having that you'll be able to use full power of App Maker's tables (sorting/paging) with almost no effort. You can take a look at implementation sample in Project Tracker template ViewProject page.

How to include a relation of a relation in an AppMaker table field?

I have three data models, Partner, Client and SOW, where Client is a one-to-many relation to SOW, and Partner is a one-to-many relation to Client. I would like to include Partner Name as a column in a table displaying SOWs. Is there a way to do this with a datasource query script? Or a different approach altogether?
I was able to add a label to the SOW table row that is data bound to the Partner name via relation, but I have yet to tackle sorting and filtering by Partner, and this method appears to fetch Partner real-time after the table loads. I am currently using Drive Tables as my datasource, but am open to switching to Cloud SQL.
You can manually add header and 'cell'(one more label in list row).
Binding for the label in the header:
#models.Partner.fields.Name.displayName
Binding for the label in the list row:
#datasource.item.Client.Partner.Name
And don't forget to add both Client and Client.Partner relations to Prefetch, it should make your page load/render faster.
Useful tip:
You can copy/paste existing table labels to duplicate all original margins/paddings/styles and make them look/behave in the same way as all other labels within the table. In this case you'll need just to adjust your bindings and maybe rename some things.
Note:
Most likely you'll not be able to sort your table by relation of relation, but feel free to try(checkout onClick event handlers of labels in the table header).

Challenges with adding a 1-1 relational table (Users and UserSettings)

My issue is related to this question: Entity Framework One-To-One Mapping Issues
I have a Users table that already has a bunch of records.
Users (Id, UserName, Password, FullName, Gender)
I need to add a bunch of notification options for each user:
NotifyForNewComment
NotifyForNewPost
NotifyWhenFriendSignsUp
I may have to add more options later, but there will always be a 1-1 relationship, so my question is whether to store these in a separate table, say UserSettings, or just add them as columns to the Users table.
In the linked question (above), the advice was to create a new table and make the UserId in the UserSettings table as the primary key (because otherwise, Entity Framework doesn't like it). If that's what I have to do, then I have a few questions regarding it:
All my tables have an Id column. The UserSettings will not have an Id column then, since the UserId will be the primary key?
I'd have to turn on Identity Insert for the UserSettings table, so that when I insert a new User record, I can also insert a UserSettings record with the UserId?
Given that I already have a bunch of records in the Users table, what do I have to do if I'm going to introduce the new UserSettings table now which will have a 1-1 relationship with the Users table? Do I just run a script to add records for each user in the Users table with some default values? Or do I make it into a 0-1 relationship?
Since it's a 1-1 relationship, should I not worry about a new table, and instead just add them as columns to the existing Users table?
I think you are missing the point of a UserSettings table. It would have columns like:
UserSettingsId
UserId
Notification
It might also contain things like when the notification was created, whether it is currently enabled, and other information.
If you know exactly what the notifications are, and they are not going to change, then you might consider adding them as separate columns in the user table.
On the other hand, this is a natural 1-N relationship, and you should probably implement it as such.

How to share table property between two tables in ADO Entity Data?

i'm having a little problem in my project. i'm using ADO.Net Entity Data Model,
let's say i have 2 Tables:
Offices : a. id
b. Name
Requests: a. rid
b.fname
c.lname
d.mobile
i want the requests table will have a relations to the offices table that each row in requests will have the id of the one of the tables.
i tried to do 1 to many relations but it didn't work , i just couldnt add data to the table.
thanks for your guidence
Your Requests table needs to have a field to relate back to the offices table. Which typically should be named OfficeID or something similar. Add that field and create the relationship from Offices.ID to Requests.OfficeID and it will work fine.

EF4.1 CodeFirst: Add field to join table

I am using EF 4.1 RC and CodeFirst/POCO to build my database by code.
Imagine having a many-to-many relationship like Teachers-Students (one teacher can have many students, and one student may have many teachers). Accordingly I have two POCOs: (1) Teacher and (2) Student.
When EF creates the corresponding tables you will end up with three tables: (1)Teachers, (2) Students and (3) an extra join table. The join table contains exactly two fields: a Teacher_ID and a Student_ID.
I was wondering if I had any chance to add an extra field to the join table, e.g. "Grade" (the grade a certain teacher gives a certain teacher)?
Currently I have no idea how to achieve this with only two POCOs.
So I guess all I can do is create a third POCO (for the join table) manually, am I right? That will certainly work, but then I am losing nice navigation properties like oneTeacher.Students.First(), etc. That is the main reason why I am still looking for another way.
That's correct, and does not only apply to Code-first. If you have extra fields in your joining table, you will have it mapped as an entity. And vice-versa, if you want an extra field in your joining table, you need to create a new entity and have zero-or-one-to many or one-to-many navigation properties to the Teacher and Student entities. In any case, you lose the comfort of accessing Teacher.Students and Student.Teachers and have to go via the intermediate entity.
Alternatively, you could think about modeling the DB structure differently and extracting the extra info into the Teacher or Student or a fourth entity. But that depends entirely on your scenario.
Yes, the join table cannot have a payload or you need to break it down to 2 one to many association which will result in creating a third entity to hold the PKs as well as the additional properties.
This is an idea I still haven't found time to try it out. Maybe you can keep your Student and Teacher classes as they are, and add a third POCO StudentGrade with properties Student, Teacher and Grade. Then you'll have to use the fluent API to make sure that both the many to many relation between Student and Teacher and the StudentGrade class map to the same table.
Hope that helps

Resources