Combine multiple values from child Link to Form fields into a single value

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Combine multiple values from child Link to Form fields into a single value

Viewing 0 reply threads
  • Author
    Posts
  • July 21, 2019 at 10:46 AM #35994

    Sam Moffatt
    Participant

    When I moved the shipment tracking numbers out of the “Orders” form into their own dedicated form called “Shipments”, I ran into a problem: using TapForms’ built in barcode scanner would return the shipment record not the order record. To work around this, I use a script field to aggregate all of the tracking numbers together into the “Orders” record in a field creatively named “Aggregate Shipping Tracking Numbers”.

    This script iterates through child records and is based on the “child records loop” snippet that comes out of the box with TapForms. It uses the logger for fencing the output so that it’s easy to track down.

    It returns a space concatenated result and if you look in your console, you’ll see output like this:

    ==================================================================
    Start "Aggregate Shipping Tracking Numbers (Orders)" script execution at Sun Jul 21 2019 10:45:41 GMT-0700 (PDT)
    ==================================================================
    
    Sun Jul 21 2019 10:45:41 GMT-0700 (PDT)	Adding tracking number: EN012345678JP
    Sun Jul 21 2019 10:45:41 GMT-0700 (PDT)	Combined tracking numbers: EN012345678JP
    
    ==================================================================
    End "Aggregate Shipping Tracking Numbers (Orders)" script execution at Sun Jul 21 2019 10:45:41 GMT-0700 (PDT)
    ==================================================================
    

    Here’s the script, you’ll need the logger module I’ve mentioned previously.

    document.getFormNamed('Script Manager').runScriptNamed('Logger');
    
    function recordsLoop() {
       var retval = [];
    	var shipments = record.getFieldValue('fld-db2fcdb4d79c466ea09671c47d2ae645');
    	for (var index = 0, count = shipments.length; index < count; index++){
         	var tracking_number = shipments[index].getFieldValue('fld-7a29242731d9451092c92d8586dbc94a');
         	logger.logMessage(<code>Adding tracking number: ${tracking_number}</code>);
    
    		if (tracking_number) {
    			retval.push(tracking_number);
    		}
    	}
    	return retval.join(' ');
    }
    
    logger.consoleHeader('Aggregate Shipping Tracking Numbers', 'Orders');
    result = recordsLoop();
    logger.logMessage('Combined tracking numbers: ' + result);
    logger.consoleFooter('Aggregate Shipping Tracking Numbers', 'Orders');
    result;
    
Viewing 0 reply threads

You must be logged in to reply to this topic.