How to retrieve objectId after saving it to the database?

Routing in my webApp is based on objectId. I want to create new data object and go to the object’s page right after hitting “save”. To do it, i need to know it’s objectId.
Is there way to retrieve objectId right after saving data object in the database?

Hi, Anton,

You need to have an “objectId” property in your class. This way it will be initialized with an actual objectId of the saved object and you shall be able to retrieve it.
Regards,
Sergey

Not sure, if I’ve understood you correctly.
What do you mean by “class”?
Do you mean, I should save data object with custom objectId?

Could you please provide a part of code where you create and save the new data object? It’ll be easier to help you if I see the code.

function Contact(args) {
   args = args || {};
  this.name = args.name || "";
  this.age = args.age || "";
  this.phone = args.phone || "";
  this.title = args.title || "";
}


var APPLICATION_ID = "YOUR-APP-ID",
   SECRET_KEY = "YOUR-SECRET-KEY",
   VERSION = "v1";   //default application version;
 
Backendless.initApp( APPLICATION_ID, SECRET_KEY, VERSION );
 
var contactObject = new Contact( {
   name: "James Bond",
   age: 45,
   phone: "1-800-JAMESBOND",
   title: "chief spying officer"
});
 
var savedContact = Backendless.Persistence.of( Contact ).save( contactObject );

This is similar to my code

After executing your code, try to inspect the value of savedContact.objectId.

I have the same question, but I save my objecto to backend using asynchronous method, so i do not have savedObject. How can achieve this?