How to add more than one record at a time using a Form script

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk How to add more than one record at a time using a Form script

Viewing 3 reply threads
  • Author
    Posts
  • May 15, 2021 at 2:42 AM #44402

    Victor Warner
    Participant

    I would like to add more than 1 record to a Form at a time.

    For one record I have the following form script:

    var date_id = 'fld-55a1678dc7b847f6be303140ad4779a3';
    var type_of_disbursement_id = 'fld-1214396547f84c9b8fcccac7917e0147';
    var unit_price_id = 'fld-8af21853bb8949bbbac92a1919fb5597';
    var number_id = 'fld-0616ad94503b4bd1b87bd4a1b0ce9d44';
    
    var date = new Date();
    var typedoc = "apostille";
    var unitprice = "30";
    var number = "1";
    
    let newRecord = form.addNewRecord();
    
    newRecord.setFieldValue(date_id,date);
    newRecord.setFieldValue(type_of_disbursement_id,typedoc);
    newRecord.setFieldValue(unit_price_id,unitprice);
    newRecord.setFieldValue(number_id,number);
    
    document.saveAllChanges(); 

    But then if I also then wished another record using the same Form script to add:

    var date = new Date();
    var typedoc = "legalisation service";
    var unitprice = "24";
    var number = "1";
    
    newRecord.setFieldValue(date_id,date);
    newRecord.setFieldValue(type_of_disbursement_id,typedoc);
    newRecord.setFieldValue(unit_price_id,unitprice);
    newRecord.setFieldValue(number_id,number);
    
    document.saveAllChanges();

    What command do I need to use to create another record (as using

    let newRecord = form.addNewRecord();

    again results in an error)?

    May 15, 2021 at 4:37 PM #44407

    Sam Moffatt
    Participant

    The error is probably because let (or var) flags a declaration of a variable and you’re getting an error that you’re redefining a variable in the same scope or something like that.

    The first way of solving this is the second time you do the addNewRecord, you just drop the let prefix. If that’s your error then we should be good with this. Personally I’d avoid this because you’re re-using a variable pointing to a record which means that you lose access to the pointer or reference to that record. If that isn’t important to you then this is probably the easiest solution.

    The second is that newRecord is just a name that has meaning to us as humans. You could use a different name like let apostilleRecord = form.addNewRecord() and then later let legalisationRecord = form.addNewRecord(). This keeps them distinct and makes it easy to realise if you’ve made a mistake that you’re assigning something to the wrong variable (why am I setting the apsotilleRecord to be “legalisation services”?). If I’m going on to use these records, perhaps to link to another record (e.g. record.addRecordToField), then this is likely the approach I’d take (though in that case I’d use let newRecord = record.addNewRecordToField(linkToFormFieldId) because it’ll automatically create the link for me).

    The third approach would be to put this in a loop if we don’t need the reference to the field. This has the downside of the first situation where you lose the reference to the record but it also has a little more flexibility if you don’t need that reference. The advantage of this would be that you can add to the array the records you want to create and create them as necessary. A refactor might look like this:

    var date_id = 'fld-55a1678dc7b847f6be303140ad4779a3';
    var type_of_disbursement_id = 'fld-1214396547f84c9b8fcccac7917e0147';
    var unit_price_id = 'fld-8af21853bb8949bbbac92a1919fb5597';
    var number_id = 'fld-0616ad94503b4bd1b87bd4a1b0ce9d44';
    
    let entries = [{
            [date_id]: new Date(),
            [type_of_disbursement_id]: "apostille",
            [unit_price_id]: "30",
            [number_id]: "1"
        },
        {
            [date_id]: new Date(),
            [type_of_disbursement_id]: "legalisation service",
            [unit_price_id]: "24",
            [number_id]: "1"
        }
    ];
    
    for (let entry of entries) {
        let newRecord = form.addNewRecord();
        newRecord.setFieldValues(entry);
    }
    
    document.saveAllChanges();
    

    We set the field ID’s at the top, same as your script but we next create an array of entries. We’re hard coding them here which makes the loop a little pointless but it shows how we add stuff to a loop to create items. The square brackets (e.g. []) when creating a variable tells Javascript this is an array of elements. The {} syntax when creating a variable tells Javascript that this is an object, we’re going to use that a little later to set the record values. The inner part should look slightly familiar as what you were doing before via setFieldValue. In our usage objects can be thought of key/value pairs of entries. In this case the “key” is the field ID and we’re using the square bracket syntax here (e.g. [date_id]) to tell Javascript to use the value of date_id which is fld-55a1678dc7b847f6be303140ad4779a3. This makes things a little neater by removing an intermediate variable because we’re directly putting in the mapping. This example creates two objects (the comma in the middle after the } tells Javascript that another object should be expected), one for the “apostille” and one for the “legalisation service”.

    The next bit of code is a for loop that runs the same code multiple times for each values in the array. The first time through the loop the value of the variable entry is the “apostille” object and then the second time through it’s the value of the “legalisation service” object. Inside the loop (the curly braces in this context is creating a block, confusing right?) we’re running the same code which is creating a new record (form.addNewRecord) and then I use setFieldValues with the object we created to set all of the field values in a single step.

    The last line remains the same, we only need to do it once to tell Tap Forms to persist everything.

    Hopefully that helps you out with some ideas and solutions for moving forward.

    May 16, 2021 at 11:08 AM #44410

    Victor Warner
    Participant

    Sam,

    Thank you very much. Either the second or third approach work fine for my very simple needs.

    One last question – how would be possible to create the records as linked records from another form (for example, in the attached database – in the Orders form linked to the disbursements form)?

    Attachments:
    You must be logged in to view attached files.
    May 16, 2021 at 12:39 PM #44414

    Brendan
    Keymaster

    Hi Victor,

    The record.addRecordToField(some_record, linked_field_id); is the function you want to use for that if you have an existing record you want to link to a relationship. Or if you don’t have a record yet, you can use the function var new_record = record.addNewRecordToField(field_id);. Then you can update the values on the newly created record.

    Thanks,

    Brendan

Viewing 3 reply threads

You must be logged in to reply to this topic.