Using Business Logic Feature to limit number of objects in related tables on update of parent table

My Users table has a column pointing to a relation with another table for whose objects are inserted after calling Backendless.UserService.update on the Users table for the logged in User. Adding Event Handlers on the Data Tables category with a beforeUpdate timer does not seem to work, my guess is because the update is on the parent table(Users). I would like to limit the number of entries in the related table to say 1 entry, the existing entry being replaced each time the logged in user calls an update on the Users table targeting the 1:1 related table.

I am using Coderunner with IntelliJ to add the event handler in the Data Tables category which, as i have said before does not work.

Much appreciated.

Much appreciated.

Adding an event handler to the the Users table category results in the below. I specifically would like to get the number of objects in the related table from the below business logic generated code

public class GenericUserEventHandler extends com.backendless.servercode.extension.UserExtender
{
    
  @Async
  @Override
  public void beforeUpdate( RunnerContext context, HashMap userValues ) throws Exception {
    // add your code here
    System.out.println("Related table object inserted.........");


  }
    
}

I am able to get the println in the coderunner window.

Hello!

I’m not sure that I understand your case. So, you have table “Related”. There is a relation 1:1 from “Users” to “Related”. You want to have only one row in “Related” table and update it each time “Users” object is updated. And all your “Users” point the same “Related” object.
Am I right?

Alexandr, yes. That’s correct.

This is where am updating the Users table.

// I have saved current logged in user objectId in shared preferences
// so retrieving it 
String currentUserId = Utils.getCurrentUserId(getContext());
if (!currentUserId.equals("")) {
            Backendless.UserService.findById(currentUserId, new DefaultCallback<BackendlessUser>(getContext()) {
                @Override
                public void handleResponse(BackendlessUser currentUser) {
                    Related related = new Related();
                    related.eyeColor = mEyeColor.getText.toString;
                    related.hairColor = mHairColor.getText().toString();


                    currentUser.setProperty("related", related); // creating the Related table with a 1:1 relationship


                    Backendless.UserService.update(currentUser, new DefaultCallback<BackendlessUser>(getContext()) {
                        @Override
                        public void handleResponse(BackendlessUser response) {


                        }


                        @Override
                        public void handleFault(BackendlessFault fault) {
                        }
                    });


                    super.handleResponse(currentUser);
                }


                @Override
                public void handleFault(BackendlessFault fault) {
                }
            });

In order to prevent creating related object - retrieve parent object together with the related object. Use the following “findById” signature:

public void findById(String id, int relationsDepth, AsyncCallback<Map> responder)

Then inside responder check if the retrieved User already has “related” object. If it does - update it, otherwise create new one.
So, your code would look something like this:

Backendless.Data.of(BackendlessUser.class).findById("usersObjectId", 1, new AsyncCallback<BackendlessUser>{
  @Override
  public void handleResponse(BackendlessUser user) {
    Related related = user.getProperty("related");
    if(related == null)
      related = new Related();
    related.setPropertyOne("value");
    related.setPropertyTwo("value");
    user.setProperty("related", related);
    Backendless.UserService.update(user, new AsyncCallback<BackendlessUser>) {
    ........ and so on
  }
  // handling fault
});

Here you do not need to use any custom business logic. Please, try this approach.
regards,
Alex

I am able to check the existence of related object in the below line

Related related = user.getProperty("related"); // this returns an object

and create a new one if the below line returns true

if(related == null) // true or false

but unable to update the returned related object if the above line returns false.
To update it i am calling

Backendless.Persistence.of(Related.class).save(related, new AsyncCallback<Related>() {
@Override
public void handleResponse(Related response) {
}


}@Override
public void handleFault(BackendlessFault fault) {
}

the above creates a new object in the Related table without an objectId.

Maybe for case if “related” is null you’d retrieve the “Related” object directly from table “Related” using, for example, “findFirst” method?
As I understand, table “Related” contains only one object in your app, so it should work.

How to update the retrieved object?

Use “save” method. If object contains objectId - it would be updated, otherwise - created.

Bravo. Works perfect with save method. Thank you very much Alexandr.

Just a small related issue; one of my model classes that has been working fine creating 1:N relations with the Users table is suddenly giving me “wrong schema” error. I notice the returned class name mysteriously has a $1 appended at the end. What could i have broken? I know i am probably supposed to raise another ticket for this, but please please, just a pointer to what could have gone wrong. I have tried deleting and coding the class afresh but nothing, other classes in the app are working fine, and everything done the same.

It’s better to open a separate topic for this issue, it needs investigation.

Please, describe steps to reproduce and put there your application id.