41
Views
9
Comments
Solved
Update data in Entity A from Data in Entity B
Question
Application Type
Reactive
Service Studio Version
11.55.4 (Build 63890)

Hi

I have been developing apps in Outsystems for a few years, but I still consider myself a beginner.  I have 1 entity for devices (Racelinks) and 1 entity for people (Competitors).  There is a 1 to many relationship, 1 user can have many devices.  For each new event I upload user information.  I am trying to get my app to allocate 1 device to each user.  The server action I created currently allocates all devices to one user. Can anyone point me in the right direction?

2020-11-10 23-58-16
Raphael Ranieri
 
MVP
Solution

Hey @Derek Fleming ,

The problem could be on the assign you are doing.
Are you using:

GetRacelinks.List.Current.Racelink.AllocatedTo = GetCompetitors.List[GetRacelinks.List.CurrentRowNumber].Competitor.Id  

(CORRECT)

OR


GetRacelinks.List.Current.Racelink.AllocatedTo = GetCompetitors.List.Current.Competitor.Id   

(INCORRECT)


If you are using the latter, the competitor will always be the same as it is not being iterated.

The value should be:

GetCompetitors.List[GetRacelinks.List.CurrentRowNumber].Competitor.Id   

Also, the start index on the loop should be 0.
So it always start from the beggining.

Doest it makes sense? 

Best Regards, 

RR :) 

2020-07-21 19-28-50
Rajat Agrawal
Champion

Hi @Derek Fleming 

If you want to assign one device to each users firstly apply loop on Competitors after that get one by one device and assign it into Competitors entity and update it .

Like below 

GetRaceLinks.List[GetCompetitors.List.CurrentRowNumber].DeviceId

Regards ,

Rajat

UserImage.jpg
Derek Fleming

Thanks for the reply Rajat.  I'm not sure that will work, maybe my data model is the wrong way round.  The foreign key for this relationship is in the  Racelink entity

UserImage.jpg
Derek Fleming

Hi Rajat

I tried this in the foreach Record list attribute
GetRacelinks.List[GetCompetitors.List.CurrentRowNumber].Racelink.Id

I received this error message
'Record List' data type required instead of 'Racelink Identifier'.


2021-11-12 04-59-31
Manikandan Sambasivam

Hi,

You need to iterate over both GetRacelinks.List and GetCompetitors.List simultaneously, ensuring you assign each Racelink to one Competitor. If you have more devices than users, you may want to cycle through the users repeatedly.

UserImage.jpg
Derek Fleming

Thank you for the reply. Yes I agree, my head tells me that's what I need to do.  Just not sure how to go about it in Outsystems.

2020-11-10 23-58-16
Raphael Ranieri
 
MVP

Hey @Derek Fleming,

I'm not sure if I completely understand your use case.

However, assuming that Racelinks don’t need to be validated to check if they are already allocated, and that you will always have enough devices for everyone, here is one possible approach:

  1. First, get a list of all competitors.
  2. Use the length of the competitors list as a limit to fetch only as many devices as needed.
    Example: If you have 10 competitors, you will need only 10 non-allocated devices for your logic.
  3. Then, loop through your DevicesList and use the index to get the correct user, avoiding the need to loop through both lists.
    Example:

    GetRacelinks.List.Current.Racelink.AllocatedTo = GetCompetitors.List[GetRacelinks .List.CurrentRowNumber].Competitor.Id  

  4. Finally, update the Racelink record.

This is one way to solve it.

If you want to optimize it even further, you could use advanced SQL to perform the operation in a single transaction, avoiding multiple updates.

It might also be worth to revisit the data model as you said previously to ensure you have the correct modeling for your needs.

Let me know if it helps,
Best Regards,
RR :)

UserImage.jpg
Derek Fleming

Hi Raphael

I think I am getting close, thank you for your reply and help.   I know have the App allocating the correct number of Racelinks but they are still being allocated to 1 user.  I am obviously doing something wrong but I'm not sure what it is.  Could it be to do with the Source attribute in the Update operation?

Here is screen shots to give you more context

Data Model

Logic

Result  (User data is dummy data)

2020-11-10 23-58-16
Raphael Ranieri
 
MVP
Solution

Hey @Derek Fleming ,

The problem could be on the assign you are doing.
Are you using:

GetRacelinks.List.Current.Racelink.AllocatedTo = GetCompetitors.List[GetRacelinks.List.CurrentRowNumber].Competitor.Id  

(CORRECT)

OR


GetRacelinks.List.Current.Racelink.AllocatedTo = GetCompetitors.List.Current.Competitor.Id   

(INCORRECT)


If you are using the latter, the competitor will always be the same as it is not being iterated.

The value should be:

GetCompetitors.List[GetRacelinks.List.CurrentRowNumber].Competitor.Id   

Also, the start index on the loop should be 0.
So it always start from the beggining.

Doest it makes sense? 

Best Regards, 

RR :) 

UserImage.jpg
Derek Fleming

Hi RR

Thank you so much, as a result of your help and advice I was able to get this working perfectly.  TY TY TY


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.