Backendless Support
 
Answered

Insert row in data table from custom business logic

How insert row in data table from custom business logic ?

Leave a Comment

Comments (1)

photo
1

Hi!

All methods from Android SDK available from custom server code.

Android SDK doc: http://backendless.com/documentation/data/android/data_saving_data_objects.htm

Here is example for you:

  1. @BackendlessEvent( "customEvent" )
  2. public class CustomEventEventHandler extends com.backendless.servercode.extension.CustomEventHandler
  3. {
  4. @Override
  5. public Map handleEvent( RunnerContext context, Map eventArgs )
  6. {
  7. String appId = context.getAppId().toString();
  8. Contact contact = new Contact();
  9. contact.setName( appId );
  10. contact.setAge( 147 );
  11. contact.setPhone( "777-777-777" );
  12. contact.setTitle( "Favorites" );
  13. Contact savedContact = Backendless.Persistence.save( contact );
  14. return Collections.emptyMap();
  15. }
  16. }

Or you can use added project:

1. Open attached project in IDEA

2. Select Bootstrap.java and set your app id and secret key (use secret key for code runner)

3. Build project

4. Run CodeRunner.sh

5. For example, run REST request to invoke handler:

  1. curl
  2. -H application-id:<your app id>
  3. -H secret-key:<rest secret key>
  4. -H Content-Type:application/json
  5. -H application-type:REST
  6. -X POST
  7. -v https://api.backendless.com/v1/servercode/events/customEvent -d '{}'

6. Check added Contact table on data page

Link to project: https://drive.google.com/file/d/0B0Q96QP8p-3UQmFRdDVKbWtuNkU/view?usp=sharing

Regards,

Kate.