Storing and retrieving Company ID from User object

My users are related to a Company, and the relation company is present on the User object. The company is vital for many operations, so I would like to have it stored conveniently for use in the app I am making. However, using the built in functions for getting current user when logged in does not give back any related data. I also don’t want to use more API calls than necessary, so I was hoping to find a way to get the objectId of the company to be stored in the Users object using a generated column. Using a formula like “IF (LENGTH(company.objectId)>0, company.objectId, NULL)” seems to give rather random objectIds though, so I guess that is not the way to go.

How should I go about doing this? Do I need to do a separate call to store the company info in the User object on the client side and maintain it if not present? Or are there other smarter methods of handling this?

Also - Have a nice Christmas :slight_smile: :christmas_tree:

Hello @Egil_Helland

  1. A user can only have 1 company? Is the relation type 1:1 or 1:N?
  2. You can’t use loadRelations? Overview - Backendless REST API Documentation
  3. Do you need to store the company’s objectId in a separate column?


Hi @Inna_Shkolnaya ,

  1. Yes, this is a 1:1 relation
  2. Have not found that to be part of the UIBuilder methods available. I am referring to the Get Current User component in UIBuilder.
    Vivaldi UI Builder - app-v1 - DynamicPlayground - Backendless - Vivaldi 001034@2x
  3. I don’t need it unless that is the only method for me to get this context be available for me.

What I am trying to do is this, with as few API calls as possible:

  1. Make sure that the user is logged in
  2. Store the curent user details and context in an object in Page Data and in Local Storage Data
  3. Provide correct context in subsequent API calls, to allow for efficient caching and data retrieval from the database

Unfortunately you cannot work with relation columns in generated column. I can suggest you to create an eventHandler setRelation, which will write the company objectId to the corresponding column in the Users table.