New record with child form records from current selected record

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk New record with child form records from current selected record

Viewing 3 reply threads
  • Author
    Posts
  • March 27, 2021 at 1:31 AM #43941

    Chris Ju
    Participant

    Hi,

    I’ve been trying to write a java script for some time without success, because my skills are limited (i’m still learning ;-)).

    The

    My path / goal can be described as follows:

    1. A record (“current_record”) is currently selected in form “AR”. A new record (“new_record”) should be created in the form “AR”.

    2. The new record (“new_record”) should contain all (or selected) linked form (“AR-Posten”) records (“linkedform_records_of_current_record”) of the currently selected record (“current_record”).

    3. AND: Data from only CERTAIN fields should be taken from the linked form records (“linkedform_records_of_current_record”) and filled in the fields of the new child records.

    My code (only an exerpt) looks like this:

    // linked form AR-Posten id
    var ar_posten_id = ‘fld-265897a72a6f467c9185cfeae6c31d57’;
    var ar_posten = record.getFieldValue(ar_posten_id);

    // field ids from linked form records "AR-Posten", only two for example
    var p_id = 'fld-59deac8c932245ca81925170193f62f3';
    var quote_id = 'fld-c00267497ef94d70b6778329cff05d0b';
    
    // add new record to AR
    var new_ar = form.addNewRecord();
    
    for (var index = 0, count = ar_posten.length; index < count; index++){
    	var p = ar_posten[index].getFieldValue(p_id);
    	var quote = ar_posten[index].getFieldValue(quote_id);
    ...

    My question is:

    How can i add/link new child records (“AR-Posten”) to the new record (“AR”) and fill each with the field data from “child_records_of_current_record”.

    Actually there is no function in the java script API with which one can duplicate records and at the same time all linked child form records. The fields (ids) for the current record and the linked form records (only for the first stage) to be duplicated should be determined in the function.

    I hope my explanation is understandable.

    Thanks in advance
    Chris

    March 27, 2021 at 5:45 PM #43949

    Sam Moffatt
    Participant

    You’re on the right track with the loop and also correct there isn’t a function that does exactly what you need. Inside your ar_posten, you’ll want to add a new record to the linked field based on the new_ar record you created:

    for (var index = 0, count = ar_posten.length; index < count; index++){
      var new_ar_posten = new_ar.addNewRecordToField(ar_posten_id);
    

    Then you need to basically setFieldValue each of the fields you want to set with an equivalent getFieldValue:

      new_ar_posten.setFieldValue(p_id, ar_posten[index].getFieldValue(p_id));
      new_ar_posten.setFieldValue(quote_id, ar_posten[index].getFieldValue(quote_id));
    

    There are a couple of ways you could optimise it but I think the simple brute force mapping is probably the easiest to get started with. One of the better approaches would to have a list of the field ID’s and loop through them:

    var fieldsToCopy = [p_id, quote_id];
    for (fieldId of fieldsToCopy) {
      new_ar_posten.setFieldValue(fieldId, ar_posten[index].getFieldValue(fieldId));
    }
    

    Then you just add the field ID’s to the list to copy them across. You could directly put field ID’s in there directly (e.g. var fieldsToCopy = ['fld-59deac8c932245ca81925170193f62f3','fld-c00267497ef94d70b6778329cff05d0b'] or keep the variables that Tap Forms creates for you to make it easier to read and update.

    Hopefully that helps, you’re a good chunk of the way there.

    March 28, 2021 at 11:31 AM #43954

    Chris Ju
    Participant

    Thanks Sam, your hints helps me very much. Will try to get it working at the next weekend. Will publish the results here, if i will be successful.

    March 28, 2021 at 2:27 PM #43957

    Sam Moffatt
    Participant

    One last piece before I forget, don’t forget to do a document.saveAllChanges() once you’re done otherwise it might not stick.

Viewing 3 reply threads

You must be logged in to reply to this topic.