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 5 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.

    January 28, 2022 at 5:22 AM #46483

    Chris Ju
    Participant

    Thanks again, Sam… This is my working result. Maybe it will help someone:

    (for lines where only IDs changed i used a placeholder “// …”)

    
    
    function Dupliziere_AR() {
    // Feld-Ids AR	
    	var ar_art_id = 'fld-25f5d93a42b84534940ba3b320787120';
    	// ...
    	var text_endnote_id = 'fld-e6ee53a372a647659ca8a00db41863d2';
    	
    // Zu kopierende Feldwerte der alten AR für neue AR
    	var ar_art = record.getFieldValue('fld-25f5d93a42b84534940ba3b320787120');
    	// ...
    	var text_endnote = record.getFieldValue('fld-e6ee53a372a647659ca8a00db41863d2');
    
    // Feld-Ids AR-Posten
    	var p_id = 'fld-59deac8c932245ca81925170193f62f3';
    	// ...
    	var ust_7_id = 'fld-1017e45c224947cc8a2b3a6d7e258399';
    
    // Neue AR
    	var neue_ar = form.addNewRecord();
    	var neue_ar_id = neue_ar.getId();
    	console.log('Neue AR Record-Id: ' + neue_ar_id);
    
    // Link AR_Posten
    	var ar_posten_id = 'fld-265897a72a6f467c9185cfeae6c31d57';
    	var ar_posten = record.getFieldValue(ar_posten_id);
    
    	for (var index = 0, count = ar_posten.length; index < count; index++) { // for Beginn
    	
    		var p = ar_posten[index].getFieldValue(p_id);
        	// ...
    		var ust_7 = ar_posten[index].getFieldValue(ust_7_id);
    				var new_ar_posten = neue_ar.addNewRecordToField(ar_posten_id);
    		new_ar_posten.setFieldValue(p_id, ar_posten[index].getFieldValue(p_id));
      		// ...
      		new_ar_posten.setFieldValue(ust_7_id, ar_posten[index].getFieldValue(ust_7_id));
     		var fieldsToCopy = [p_id, gegenstand_id, vv_nr_id, vergütungstatbestand_id, wert_id, ansatz_a_id, satz_a_id, quote_id, sonder_ust_id, ust_19_id, ust_7_id];
    		for (fieldId of fieldsToCopy) {
    		new_ar_posten.setFieldValue(fieldId, ar_posten[index].getFieldValue(fieldId));
    		}
    		  
         	document.saveAllChanges();
    		
    	} // for Ende
    
    // Neue AR Feldwerte setzen // nicht nach oben verschieben, da sonst keine neuen AR-Posten erstellt werden!
    	neue_ar.setFieldValues({
    				[ar_art_id]: ar_art,
    				// ...
    				[text_endnote_id]: text_endnote
    				});
    	document.saveAllChanges();
    	console.log('Feldwerte neue AR gesetzt!')
    	
    	return;
    	
    	document.saveAllChanges();
    	
    }
    
    Dupliziere_AR();
    
    
    January 28, 2022 at 10:40 AM #46485

    Sam Moffatt
    Participant

    Awesome news that you got this figured out! Congratulations :)

Viewing 5 reply threads

You must be logged in to reply to this topic.