Introducing Managed Fields

Viewing 1 reply thread
  • Author
    Posts
  • February 21, 2023 at 12:17 AM #48976

    Sam Moffatt
    Participant

    Those of you who have been looking at my YouTube might have seen the video talking about Managed Fields and I’m finally ready to show it to the world. This is brand new, probably has a few bugs and definitely isn’t as battle tested as some of the other code I’ve written as I had a more immediate need to leverage it. It’s already up to 1.0.1 because I found a bug with the log feature (I forgot to include the record ID! Doh!) and also wanted to include my old setIfEmpty feature as a part of managed fields as well making it easy to only update fields whose values aren’t already set in addition to a couple of other options.

    Those curious might also be interested in the VOD of Twitch stream I did which has some more development included though has some distractions as well. You can see in real time just how slow building some of this code up is though I must apologise the audio quality ended up quite poor.

    A big part of Managed Fields are two accompanying forms in addition to the Managed Field script library. Check out the attachment to this post (you’ll need an account to download) and import it to your record alongside the original Script Manager. I’ve also started to add some Markdown documentation and you can see an example with the addToTableIfMissing. I’m slowly adding documentation for various things as I come back to using them and forget how they work so hopefully also sharing. At some point I hope to leverage the Markdown feature in Tap Forms to include these inside the database so the documentation is always available…even if you’re working on your Tap Forms document on the plane!

    Attachments:
    You must be logged in to view attached files.
    February 27, 2023 at 1:23 AM #49052

    Sam Moffatt
    Participant

    I wanted to do a demo of some of the functionality and two sample scripts with a sample form. The form is a shipping tracker with a tracking number, ship date, delivery date, confirmed date and notes field. There are some pre-canned managed field configurations set and two scripts. I’ve attached a Tap Forms 5 archive you can use to check out the structure.

    The first script, Confirm Received, uses a prompter to ask for a tracking number and if the record exists updates it to set the confirmed date to be the current date. This uses setIfEmpty on the confirmed date field to only update the field if it doesn’t have a value already. This allows you to “confirm” a package multiple times but only the first time will be stored. It looks like this:

    const PARENT_SCRIPT = form.name + "::" + scriptName;
    
    document.getFormNamed("Script Manager").runScriptNamed("Managed Fields");
    document.getFormNamed("Script Manager").runScriptNamed("getRecordFromFormWithKey");
    
    var tracking_number_id = 'fld-91af3b8b61d041db888921726f333a3b';
    var ship_date_id = 'fld-d38551668df3406589c8594921be0c86';
    var delivery_date_id = 'fld-c0a0d5647dc34126b6c82baabab69cf7';
    var confirmed_date_id = 'fld-fc04420bee174714940951aada52254d';
    var notes_id = 'fld-8effa85f9bc147538211a16cfd961de9';
    
    var callbackFunction = function(continued) {
    	if (!continued) {
    		return;
    	}
    	
    	if (!tracking_number) {
    		Utils.alertWithMessage("Missing tracking number", "Tracking number is required!");
    		return;
    	}
    	
    	console.log(tracking_number);
    	
    	let targetRecord = getRecordFromFormWithKey("Shipments", tracking_number_id, tracking_number, false);
    	if (!targetRecord) {
    		Utils.alertWithMessage("Unknown tracking number", "Item not found");
    		return;
    	}
    	
    	targetRecord.setManagedFieldValue(confirmed_date_id, new Date());
    	document.saveAllChanges();
    };
    
    var tracking_number;
    let prompter = Prompter.new();
    prompter.addParameter('Tracking Number', 'tracking_number', 'text')
    	.show('Confirm Package Received', callbackFunction);
    

    The second script, Handle Shipment Update, is a little longer and leverages some more prompter functionality to handle if a shipment is “shipped” or marked “delivered”. This workflow is useful for when someone sends a notification that the package shipped or there is a carrier delivery notification. It has some more prompter examples and demonstrated setManagedFieldValue to ensure that the field is updated properly:

    const PARENT_SCRIPT = form.name + "::" + scriptName;
    
    document.getFormNamed("Script Manager").runScriptNamed("Managed Fields");
    document.getFormNamed("Script Manager").runScriptNamed("getRecordFromFormWithKey");
    
    var tracking_number_id = 'fld-91af3b8b61d041db888921726f333a3b';
    var ship_date_id = 'fld-d38551668df3406589c8594921be0c86';
    var delivery_date_id = 'fld-c0a0d5647dc34126b6c82baabab69cf7';
    var confirmed_date_id = 'fld-fc04420bee174714940951aada52254d';
    var notes_id = 'fld-8effa85f9bc147538211a16cfd961de9';
    
    function confirmExecution(question) {
    	return new Promise(function(resolve, reject) {
    		let prompter = Prompter.new();
    		prompter.cancelButtonTitle = 'No';
    		prompter.continueButtonTitle = 'Yes';
    		prompter.show(question, ((status) => {
    			if (status ==  true) {
    				resolve('Yes');
    			} else {
    				reject(question + 'No');
    			}
    		}));
    	});
    }
    
    var callbackFunction = async function(continued) {
    	if (!continued) {
    		return;
    	}
    	
    	if (!tracking_number) {
    		Utils.alertWithMessage("Missing tracking number", "Tracking number is required!");
    		return;
    	}
    	
    	if (!event_type) {
    		Utils.alertWithMessage("Missing event type", "Please specify the event type to continue.");
    		return;
    	}
    	
    	let targetDate = new Date();
    	if (event_date) {
    		targetDate = new Date(event_date);
    	}
    	
    	console.log(tracking_number);
    	console.log(event_type);
    	console.log(event_date);
    	console.log(targetDate);
    	
    	let targetRecord = getRecordFromFormWithKey("Shipments", tracking_number_id, tracking_number, false);
    	if (!targetRecord) {
    		try {
    			await confirmExecution('Create new record?');
    		} catch (errorText) {
    			// user clicked 'No'
    			console.log('cancelled');
    			return;
    		}
    	
    		// user clicked 'yes'
    		// continue with function
    		console.log('continue from yes');
    		targetRecord = form.addNewRecord();
    		targetRecord.setManagedFieldValue(tracking_number_id, tracking_number);
    	}
    
    	switch (event_type) {
    		case "Shipped":
    			targetRecord.setManagedFieldValue(ship_date_id, targetDate);
    			break;
    		case "Delivered":
    			targetRecord.setManagedFieldValue(delivery_date_id, targetDate);
    			break;
    	}
    	
    	document.saveAllChanges();
    };
    
    var createNewRecord;
    var tracking_number;
    var event_type;
    var event_date;
    let prompter = Prompter.new();
    prompter.addParameter('Tracking Number', 'tracking_number', 'text')
    	.addParameter('Event Type', 'event_type', 'popup', ['Shipped', 'Delivered'])
    	.addParameter('Event Date', 'event_date', 'text')
    	.show('Handle Shipment Update', callbackFunction);
    
    • This reply was modified 1 year, 9 months ago by Sam Moffatt.
    Attachments:
    You must be logged in to view attached files.
Viewing 1 reply thread

You must be logged in to reply to this topic.