How to write script to combine fields in linked record

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk How to write script to combine fields in linked record

Viewing 4 reply threads
  • Author
    Posts
  • April 7, 2023 at 2:47 AM #49296

    Victor Warner
    Participant

    I would like to make a script that takes the content of a field from linked records of another form and combine them together.

    For example, see attached Tap Form database

    In Form 1 there is a link to Form 2

    So I would like to create a script which:

    1. combines the fields for the 4 linked records
    2. combines them together with commas after each but adds numbers before each field (eg “1. certificate of good standing, 2. Power of attorney, 3. statutory declaration, 4. travel consent document’)
    3. copies the result to clipboard and writes the combined fields into the All documents field in Form 1

    Help on how to do this would be gratefully received.

    Attachments:
    You must be logged in to view attached files.
    April 7, 2023 at 11:05 AM #49299

    Brendan
    Keymaster

    Hi Victor,

    On the Script Editor, if you select a field from your Link to Form field and then double-click on the “Child Records Loop” code Snippet, Tap Forms will write the code for you to loop through all your child records, extracting out the value from the selected field.

    Now you can use the JavaScript push() function to combine all the values from that field into an Array. Then after the loop, use the JavaScript join() function to join them together with a comma separator.

    Then use the Tap Forms Utils.copyTextToClipboard() function to copy the value to the clipboard.

    Then set the value on the field in your Form 1 using record.setFieldValue(field_id, joinedDocs);

    Here’s a start for you:

    function recordsLoop() {
    
    	var forms_id = 'fld-8c147256f1534336aa960e509a8b826b';
    	var name_of_document_id = 'fld-03b7ec42c07542ea8cfc23d4809ef367';
    	var forms = record.getFieldValue(forms_id);
    
    	var allDocs = [];
    	
    	for (var index = 0, count = forms.length; index < count; index++){
         	var name_of_document = forms[index].getFieldValue(name_of_document_id);
    		if (name_of_document) {
    			allDocs.push(name_of_document);
    			
    		}
    	}
    	
    	var joinedDocs = allDocs.join(",\r");
    
    	
    	return joinedDocs;
    }
    
    recordsLoop();

    It literally took 30 seconds because Tap Forms did most of the work for me writing all that code just by double-clicking the Child Records Loop snippet. All I did was defined the allDocs array, then pushed the name_of_document value to the array inside the loop. Then joined and returned the joined value.

    April 9, 2023 at 1:15 AM #49303

    Victor Warner
    Participant

    Brendan,

    Thank you for the code and the explanation. However, if I place the

    record.setFieldValue(field_id, joinedDocs);

    within the recordsLoop() nothing is written to the current record and if outside the loop I get an error message. Also the same is true for Utils.copyTextToClipboard().

    My revised code:

    function recordsLoop() {
    
    var all_documents_id = 'fld-b130f329a7ae46f3908d190699822044';
    
    	var forms_id = 'fld-8c147256f1534336aa960e509a8b826b';
    	var name_of_document_id = 'fld-03b7ec42c07542ea8cfc23d4809ef367';
    	var forms = record.getFieldValue(forms_id);
    
    	var allDocs = [];
    	
    	for (var index = 0, count = forms.length; index < count; index++){
         	var name_of_document = forms[index].getFieldValue(name_of_document_id);
    		if (name_of_document) {
    			allDocs.push(name_of_document);
    			
    		}
    	}
    	
    //	var joinedDocs = allDocs.join(",\r");
    	var joinedDocs = allDocs.join(", ");
    
    	
    	return joinedDocs;
    	record.setFieldValue(all_documents_id, joinedDocs);
    
    document.saveAllChanges;
    Utils.copyTextToClipboard(joinedDocs);
    
    }
    
    recordsLoop();
    
    
    • This reply was modified 1 year ago by Brendan.
    Attachments:
    You must be logged in to view attached files.
    April 9, 2023 at 2:21 AM #49309

    Brendan
    Keymaster

    document.saveAllChanges; is a function, so should be called with parenthesis like document.saveAllChanges();

    That may be what’s causing your issue.

    But also, you’re returning out of the function before you run those last 3 lines of code. So they’ll never get run. They need to go before the return joinedDocs; statement.

    April 9, 2023 at 3:51 PM #49310

    Victor Warner
    Participant

    Brendan,

    Thank you. Issue resolved.

    I would like to add one addition to number the combined values, e.g. “1. certificate of good standing, 2. Power of attorney, 3. statutory declaration, 4. travel consent document” with the following field script:

    function recordsLoop() {
    
    var all_documents_id = 'fld-b130f329a7ae46f3908d190699822044';
    var script_id = 'fld-bb67d87c0f044b2cb2745ba9e3fde81f';
    
    	var forms_id = 'fld-8c147256f1534336aa960e509a8b826b';
    	var name_of_document_id = 'fld-03b7ec42c07542ea8cfc23d4809ef367';
    	var forms = record.getFieldValue(forms_id);
    
    	var allDocs = [];
    	
    	for (var index = 0, count = forms.length; index < count; index++){
         	var name_of_document = forms[index].getFieldValue(name_of_document_id);
    		if (name_of_document) {
    			allDocs.push((index+1) + ". " + name_of_document);
    			
    		}
    	}
    	
    return joinedDocs = allDocs.join(", ");
    
    console.log(joinedDocs);	
    
    }
    
    recordsLoop();
    

    However, where there is only one value I would like that single value not to have a value, e.g. rather than “1. power of attorney” but rather just “power of attorney”.

    How can the code be amended to permit this?

Viewing 4 reply threads

You must be logged in to reply to this topic.