Adding current user to a TABLE Groups

Hi, I’m trying to add users to a group using some kind of “unique value”, for example, my friend creates a group and I want to join it, he will need to send me a Code or URL so I can join it.

I started by creating a Table Groups on Backendless, I added a column “Users” to it, which uses my current Users, but I’m having problems at adding users on, I’ve tried a inviteID that corresponds to a uniqueValue, and I’ve tried the ObjectID, and I’m not having success with neither.

I’ll show some code below:

btnJoinGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(etGroupId.getText().toString().isEmpty()){
Toast.makeText(FirstGroup.this, “Invite EMPTY!”, Toast.LENGTH_SHORT).show();
}
else{
String groupId = etGroupId.getText().toString().trim();
Backendless.Data.of(“Groups”).findById(groupId, new AsyncCallback<Map>() {
@Override
public void handleResponse(Map savedUser) {
Map user = (Map) Backendless.UserService.CurrentUser();
Backendless.Persistence.save(user, new AsyncCallback<Map>() {
@Override
public void handleResponse(Map response) {
Toast.makeText(FirstGroup.this, “Success!!”, Toast.LENGTH_SHORT).show();
}

@Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(FirstGroup.this, “ERROR!”, Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(FirstGroup.this, “ERROU LOGO!”, Toast.LENGTH_SHORT).show();
}
});

}
}
});

}

Thank you for any help you guys are amazing :wink:
Alexandre Coutinho

Hi Alexandre,

Here’s the first problem that I see:

Backendless.Data.of(“Groups”).findById(groupId, new AsyncCallback<Map>() {
@Override
public void handleResponse(Map savedUser) {

Notice that you make a request to the Groups data table, and in the handleResponse you call the argument savedUser. This is wrong, since the response you get back would be a Map representing a group object.

Then in the same handleResponse callback, you do the following:

Map user = (Map) Backendless.UserService.CurrentUser();
Backendless.Persistence.save(user, new AsyncCallback<Map>() {

Well, that will do absolutely nothing useful. You take the currently logged in user and you just save it back into the database. What is that for?

Cheers,
Mark

I was trying to save the current user on the group that corresponds to the input code, I’m still having a lot of trouble here. The current user should be able to enter a given code (that corresponds to a group) and join that group.

Thanks Mark!
Alex

Okay, Alex. What would be the design at the data level? Do you understand how you could use table relations here?

Hi Mark,
No I don’t think I know how to use table relations…

Hi, Alexandre. If you want to associate current user with some group, existing in the corresponding table, you should use one of the next methods:

Backendless.Data.of( E ).addRelation( 
                                       E parentObject,
                                       String relationColumnName,
                                       Collection<T> children,
                                       AsyncCallback;Preformatted text<Integer> callback );

Backendless.Data.of( "TABLE-NAME" ).addRelation( 
                                                 Map parentObject,
                                                 String relationColumnName,
                                                 Collection<Map> children,
                                                 AsyncCallback<Integer> callback );

More information in SDK Documentation.

Feel free to ask more questions.

1 Like

Hey andrii thanks for the answer! I did a pause and yesterday got back to it, I think what you asked me to do is what I need to do, but I’m having trouble executing it, how can I get the objectId of a group on the groups table?

Hi, Alexandre. To find objectId you can use the next method:

Backendless.Data.of( Contact.class ).find( queryBuilder, 
                                           new AsyncCallback<List<Contact>>(){
  @Override
  public void handleResponse( List<Contact> foundContacts )
  {
  }
  @Override
  public void handleFault( BackendlessFault fault )
  {
  }
});

Find more in Documentation.

Regards, Andrii.

Hi Andrii, on that documentation I can’t find anything related to objectId, should I create a “whereClause” equals to “objectId”?

Thank you so much,
Alexandre

You can find the group by “name”, for example, or any other field.
Set whereClause: name = 'foo', use it for find() method and you’ll get an object, which will have ‘objectId’ field. Use this objectId for setting relations.

Regards, Andrii.

I just want to use the “group” object I’ve created in the database, I want to get this new created “group” and get it’s objectId, how can I save this objectId into a String? I’m having trouble accessing this new “group” after creating it so I’m unable to get an instance of its objectId.

I’ve tried some stuff like…
String groupObjectId = Backendless.Data.of("Groups").findLast().get("objectId").toString();
and…
String groupObjectId = (String) lastGroup.get("objectId");
and I’m always getting a null return.

Hi! Your code should work, please, give your AppId

FindLast() method right after saving a new ‘Group’ object is not a kind of approach you can rely on. First of all if this code goes right after async call for saving an object - findFirst() can be called before ‘Group’ object is actually saved in database.

As Mark recommended in one of your previous topics - use logic for getting objectId in handleResponse block of

Backendless.Data.of("Group").save(map, new AsyncCallback<Map>(){
      @Override
      public void handleResponse( Map response )
      {
          response.get("objectId");
      }
      @Override
      public void handleFault( BackendlessFault fault )
      {
      }
    });

Anton

With that code, how do I use the objectId as an instance String?

String objectId = response.get( "objectId" )