Script not looping over records in a table

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Script not looping over records in a table

Viewing 1 reply thread
  • Author
    Posts
  • February 12, 2021 at 6:56 AM #43429

    Victor Warner
    Participant

    At How to loop over records in table Daniel Leu provided a script so that it was possible to extract the contents of the table field into an another field through a script?

    I have tried to use the code another table.

    In the document attached there is a table field (called ‘Details on documents’) contains three records.

    Below it I have added a script field (‘Script for details on documents’) adapting the code provided by Daniel Leu.

    However, the code does not loop through the records in the table field but only deals with the first record, although I have properly used (I think) the code Daniel provided. In the script editor no error is shown when the script is run.

    The code is:

    function Type_Of_Documents() {
    
    	// Replace with your own code
    
    var details_on_documents = record.getFieldValue('fld-a86e811ce12443ba957a85da4dfafafe');
    
    const type_of_document_id = 'fld-68a7edd5bdc3485fb910ce1e9101f4e0';
    const no_id = 'fld-8531f5a64e994e7d9a4e66e066aabda8';
    const country_id = 'fld-603914f15c8b49e8b2318cbee5a9f46f';
    
    var txt = [];
    
    for (var n=0; n < details_on_documents.length; n++){
    var details_on_documents = details_on_documents[n];
    var type = details_on_documents.getFieldValue(type_of_document_id);
    var number = details_on_documents.getFieldValue(no_id);
    var country = details_on_documents.getFieldValue(country_id);
    
    txt.push((n+1) + ". type of document: " + type + ", number of copies: " + number + ", destnation country: " + country);
    
    }
    
    return txt.join(" - ");
    
    }
    
    Type_Of_Documents();
    

    Any help would gratefully be received.

    Attachments:
    You must be logged in to view attached files.
    February 12, 2021 at 9:34 AM #43431

    Daniel Leu
    Participant

    Hi Victor

    The issue is in following line where you redefine a variable:
    var details_on_documents = details_on_documents[n];

    I changed it to var details_on_document = details_on_documents[n]; (note the removed ‘s’) and updated all references. Now the script seems to work.

    function Type_Of_Documents() {
    
    	var details_on_documents = record.getFieldValue('fld-a86e811ce12443ba957a85da4dfafafe');
    	
    	const type_of_document_id = 'fld-68a7edd5bdc3485fb910ce1e9101f4e0';
    	const no_id = 'fld-8531f5a64e994e7d9a4e66e066aabda8';
    	const country_id = 'fld-603914f15c8b49e8b2318cbee5a9f46f';
    		
    	var txt = [];
    	
    	for (var n=0; n < details_on_documents.length; n++){
    		var details_on_document = details_on_documents[n];
    		var type = details_on_document.getFieldValue(type_of_document_id);
    		var number = details_on_document.getFieldValue(no_id);
    		var country = details_on_document.getFieldValue(country_id);
    		
    		txt.push((n+1) + ". type of documents: " + type + ", number of copies: " + number + ", destination country: " + country);
    	
    	}
    	
    	return txt.join(" - ");
    }
    
    Type_Of_Documents();
Viewing 1 reply thread

You must be logged in to reply to this topic.