Backendless Support
 
Answered

Save object and its related children with one call backendless 4

I have an object "Job" with multiple objects relations, where it's not reasonable to save them one by one then add them to the job object since in any case of internet interruption, half the job would be saved and the other half would no be.

Job Object has:

-Employee Objects (many)

-Box Objects (many)

-Truck Object (one)

-Driver Object (one)

-Client Object (one)

I cannot save them box by box because usually, each Job has a minimum of 50 Box Object, so it would be a big hassle and too many requests for saving one Job.

I Googled and Googled and Googled, and found that in Backendless 3 I could have just saved the Job Object and it's related children would be automatically saved and added as the Job as related Objects. Unfortunately, in Backendless 4 this does not seem to be an option, please correct me if I'm wrong.

I'm asking for a workaround to do this, as I've seen the Business Logic section of Backendless and I believe this might be done if I create a codeless or API for it, the only problem is that I don't understand Business Logic section.

Appreciating your help.

Best Answer
photo

You're getting an error because "boxListToAdd" has not been initialized. Try this:

850deee396645115182ee6e749942e61

Leave a Comment

Comments (19)

photo
1

Hi, Omar.

In Backendless 4 you cannot save graph of objects. You should firstly save all you objects, and then set relations between them.

See example for Android https://backendless.com/docs/android/doc.html#data_relations_api_set_add_android

photo
1

Hi Oleg,

As I've mentioned before I've Googled and read a lot, and I know what is the solution, I was just asking for a sample or reference on how to do it right.

I've done more research on the issue and found really good videos on the Backendless channel on YouTube.

Below link has good videos on how to implement a codeless custom business logic:

> https://www.youtube.com/user/Backendless/search?query=codeless

Now using this codeless custom business logic, I'm creating an event handler before the creation of any Job Object where I save all its related objects and then add them to the related Job Object as a relation. With this approach, I have one request from the Android side making sure any internet interruption will not stop the process. Still, and unfortunately, I'm making a request to the server on every save request. So if I had a Job Object with a 100 Box that would be an API 101 request to the Backendless server, please correct me if I'm wrong.

Thank you

photo
1

An API call is counted as an API call whether it is made from a client app or from business logic (codeless or not).

photo
photo
1

Update:

Tested it, didn't work either. I cannot retrieve the submitted related objects to the server even with an event handler before object creation, value is null.

Unfortunately if this didn't work I'll have to use another mbaas.

photo
1

If you could share a screenshot for your codeless logic, I'd be happy to review.

photo
1

Happily, please find it attached.

I might be doing it wrong but after reading

this > https://backendless.com/docs/codeless/#codeless_data_api

and checking all the videos on YouTube, this is the best I could come up with. I understand that what I need is a little bit complicated comparing to what I know about codeless

photo
1

To help me with the context, could you please show what the "job" object looks like?

photo
1

Kindly find attached.

photo
1

What is your app ID, I will create a codeless API service in your app showing the direction you should take.

photo
1

app ID: F508073E-66F9-0CF3-FF13-3EFD95AC0D00

Really appreciating your help.

photo
1

I see a few problems in the data schema. Specifically:

1. Client has a relation to StoreBrand, while StoreBrand has a relation to Client.

2. Area has a relation to StoreBrand, while StoreBrand has a relation to Area.

Relations in Backendless are bidirectional. It means you can declare a relation only one side (in one of the tables), but query it on both sides.

This is not a show stopper now (for what I am doing to help you), but I would recommend cleaning it up.

Regards,

Mark

photo
1

I did that in first steps before reading all of your documentation, currently I only use the parent relation and was planning to delete the child to parent relations as I'm not using them and will not use them in the future.

Thank you for the notes.

photo
1

I created an API service in your app. It is called "JobService". The service has one method: "saveJob". Take a look at the implementation. Here's a screenshot:

5394f1f16e6f7ee1bc9c08be13c56dd1

photo
1

I've edited the table names to uppercase for each first letter, then tested it, worked with one problem.

It's adding the boxes except that this method is assigning only one Box to each Job where I want to assign all the saved Boxes to this job.

Also if you can guide me on how to assign a single Client Object also assigned to the Job as well.

I'm testing directly from my app, downloaded and implemented the code in Android Studio.

Each Job has

>Client

>StoreBrand

>Area

>Driver

>Truck

>List of Employee

>List of Boxes

Thank you

photo
1

It's adding the boxes except that this method is assigning only one Box to each Job where I want to assign all the saved Boxes to this job.

It should be easy to change - simply store created boxes in an array and then pass that array into the Create Relation block.

As for assigning a single object as a relation, it is the same block where for the children you specify a single object.

photo
1

I've tried the attached, creating a list is not as simple as you mentioned, it's not documented. I am guessing on how to do it.

If there's some guide I can read, please let me know.

I'm receiving an error (Cannot read property "push" of undefined)

photo
1

You're getting an error because "boxListToAdd" has not been initialized. Try this:

850deee396645115182ee6e749942e61

photo
1

Done.

Thanks a lot Mark, you made it possible.

photo
1

Glad I could help!

photo