How to return created recordID

I am using the base(’ ').create() function in a jquery script that processes my form inputs. I would like to be able to “return” the created recordID so that I can take additional actions before exiting the script.
But it appears that the recordID is not defined until the script finishes executing.

Here is the code for one of several approaches I have tried:

$(‘button#submit-deliver’).click(function() {

var data = $('form').serializeFormJSON();
	  
var newRec = createRecord(data);	

console.log('created record: ’ + newRec);
});

function createRecord(data) {

var Airtable = require(‘airtable’);
var base = new Airtable({apiKey: ‘keyLtUUC-----------’}).base(‘apptiYiTzsQcY1eNm’);
var newRec;

    base('Delivery Log').create([
   {
	 "fields": data 
   }
	], {typecast: true}, function(err, records) {
     if (err)
     {
       console.error(err);
       return;
     }
records.forEach(function (record) {
		console.log(record.getId());
	newRec = record.getId();
	return newRec;
    });
    }); 	   	

};

So in this case the console.log() inside the createRecord() function comes AFTER the console.log() command in the calling function.
Thanks, I bet this is pretty simple and hope to move up the learning curve just a little bit more after this!

To clarify it a little bit more, it’s really not jQuery specific, just Javascript.
I would like know during execution that the record was created successfully to:

  1. alert the user if there was a problem
  2. offer the option to create another record if it was successful, and/or
  3. use the recordID to update the records to add fields from within the script.

Any ideas?

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.