Support Topics Documentation Slack YouTube Blog

Bulk Create

During a bulk create operation, what happens if one of the records violates a unique constraint in the table? Does the operation stop mid way without completion?
On a different note, while creating a record in a table, if the record already exists and thus violates a unique constraint can control be transfered to updating that same record? Can you point me to sample code that demonstrates this?

Hi @Nkekere_Tommy_Minimann ,

In case if some of records violates DB constrain entire bulk create uperation will not pass.

Regards, Andriy

Can you point me to sample bulk create API code?

If you want to create or update record in table then you can use save operation. It will check if record has objectId field and then create or update it.

If you mean “upsert” operation which will do “insert … on duplicate key update …” query - currently it is not possible but we are planning to add it in one of our future releases.

If you want something else - please give more detailed example of your scenario.

Regards, Andriy

//This is my code for bulk create but the code is being highlighted red to indicate syntax error. I cant figure out what’s wrong.

List<Map<String, Object>> contacts = new ArrayList<>();

    Map<String, Object> contact = new HashMap<>();

                contact.put( "Name", "Tommy" );
                contact.put( "PhoneNumber", "0803335643" );
                contacts.add( contact );

	    contact.put( "Name", "James" );
                contact.put( "PhoneNumber", "0803345321" );
                contacts.add( contact );
            
   
    Backendless.Data.of(Contact.class).save(contacts, new AsyncCallback<List<Contact>>() {


        @Override
        public void handleResponse(List<Contact> response) {

        }

        @Override
        public void handleFault(BackendlessFault fault) {

        }
    });

Hello @Nkekere_Tommy_Minimann,

what error do you see?

save(ng.antigram.CloudDB.Contact, com.backendless.async.callback.AsyncCallback<ng.antigram.CloudDB.Contact>) in IDataStore cannot be applied to
(java.util.List<java.util.Map<java.lang.String.java.lang.Object>>anonymous com.backendless.async.callback<java.util.list<ng.antigram.CloudDB.Contact>>)

@Nkekere_Tommy_Minimann

you have mixed map driven approach and class based. You should change

    Map<String, Object> contact = new HashMap<>();

to

Contact contact = new Contact()

I have changed my code to this, but the error still persists
//This is my code for bulk create but the code is being highlighted red to indicate syntax error. I cant figure out what’s wrong.

    List<Contact> contacts = new ArrayList<>();
    Contact contact = new Contact();
    contacts.add( contact );                
   
    Backendless.Data.of(Contact.class).save(contacts, new AsyncCallback<List<Contact>>() {


        @Override
        public void handleResponse(List<Contact> response) {

        }

        @Override
        public void handleFault(BackendlessFault fault) {

        }
    });

Hello @Nkekere_Tommy_Minimann

For Bulk Create use create method, for your example Non-Blocking API with
CUSTOM CLASS approach - public <E> void Backendless.Data.of( E ).create( List<E> objects, AsyncCallback<List<String>> responder ):

    List<Contact> contacts = new ArrayList<>();
    Contact contact = new Contact();
    contacts.add( contact );

    Backendless.Data.of( Contact.class ).create( contacts, new AsyncCallback<List<String>>() {
      @Override
      public void handleResponse( List<String> response ) {
      }

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

For more information about Bulk Create see documentation Saving Multiple Objects

Solved. Thank you