how to link fields when typing

Viewing 10 reply threads
  • Author
    Posts
  • July 8, 2020 at 10:21 AM #41218

    pierrot_rennes
    Participant

    Hi,
    I would like to be able to fill in a second field automatically based on the entry in another field.
    Is it possible in Tap Forms?
    Should we go through a script?
    For example, in field A, I enter a brand
    Field B automatically inquires with the corresponding country
    Thank you for your help

    July 8, 2020 at 9:29 PM #41229

    Sam Moffatt
    Participant

    I’m not sure it’s possible to do that in Tap Forms with the default UI however I think you could easily create a form script with two prompters that did that for you. The input from the first prompter could be used to setup the second prompter which then sets the appropriate fields.

    A while back I did a post with a couple of different options on how you can make it work.

    July 9, 2020 at 3:22 AM #41233

    pierrot_rennes
    Participant

    Hi Sam,
    Thank you for your answer
    I went to see your code.
    I’m looking for something simple, so I’m going to refine my search.
    I have two selection lists: Brands and Countries.
    For example, when I select the Ravensburger brand in field 1, field 2 displays the country France
    Is it possible to define a script where each correspondence for brands and countries would be defined
    Thank you

    July 9, 2020 at 6:40 PM #41243

    Sam Moffatt
    Participant

    The complexity comes in that you need to encode the knowledge so that the computer can present it. Then once you’ve encoded that knowledge you need to access it to present it back to the user. Unfortunately Tap Forms doesn’t directly have an implementation that could hide that complexity from you so that means we’re implementing the complexity to make it look simple.

    Let’s tackle the data modelling problem first for encoding which brands and which countries. The scripts I presented have a few different options, which is the most comfortable for you:

    1. form based: create a new form with two fields: one with the brand name and one with a comma separated list of countries (you could create a country pick list using the multi-value popover or checkbox options to make data entry easier)
    2. pick list based: create one pick list with a list of all of the brands in it and then for each brand create another pick list that defines the countries
    3. fully script based: encode everything into a single script file (probably a bad idea if you have a lot of data)

    I think creating a new form to store it is probably the best path forward all told. Let me know which one of those approaches of storing the mapping data of “brand” to “country” makes sense for you and we can move forward with getting this sorted.

    July 10, 2020 at 12:29 AM #41244

    Sam Moffatt
    Participant

    I re-read this and thought I might be overthinking things, if you want a simple 1:1 mapping something like this might work for you. Create a script field and then use a switch statement to pick the right value.

    Here’s an example of autocompleting a country based on a “state” field. I use a switch because I think it’s pretty readable though you could construct an array as well:

    function Autocomplete_Country() {
    	var issuing_state = record.getFieldValue('fld-34f6d74d34354f63889f63a705b7d15f');
    	var country = '';
    	
    	switch (issuing_state)
    	{
    		case 'Washington':
    		case 'Oregon':
    		case 'Texas':
    		case 'Nevada':
    		case 'Arizona':
    		case 'Florida':
    		case 'New York':
    		case 'California':
    			country = 'United States of America';
    			break;
    		case 'New South Wales':
    		case 'Victoria':
    		case 'South Australia':
    		case 'Tasmania':
    		case 'Western Australia':
    		case 'Northern Territory':
    		case 'Australian Capital Territory':
    		case 'Queensland':
    			country = 'Australia';
    			break;
    		case 'British Columbia':
    		case 'Alberta':
    			country = 'Canada';
    			break;
    	}
    	
    	return country;
    }
    
    Autocomplete_Country();

    Basically in the case lines you put each of the values you want to match and then set a variable (in my case country) to be the value you want. You can then return it and it’ll display.

    Here’s the same thing implemented as a map/dictionary:

    function Autocomplete_Country_Map() {
    	var issuing_state = record.getFieldValue('fld-34f6d74d34354f63889f63a705b7d15f');
    
    	var state_map = {
    		'Washington':  'United States of America',
    		'Oregon': 'United States of America',
    		'Texas': 'United States of America',
    		'Nevada': 'United States of America',
    		'Arizona': 'United States of America',
    		'Florida': 'United States of America',
    		'New York': 'United States of America',
    		'California': 'United States of America',
    		'New South Wales': 'Australia',
    		'Victoria': 'Australia',
    		'South Australia': 'Australia',
    		'Tasmania': 'Australia',
    		'Western Australia': 'Australia',
    		'Northern Territory': 'Australia',
    		'Australian Capital Territory': 'Australia',
    		'Queensland': 'Australia',
    		'British Columbia': 'Canada',
    		'Alberta': 'Canada'
    	}
    		
    	return state_map[issuing_state];
    }
    
    Autocomplete_Country_Map();
    

    It’s a little more concise though you’re writing down the country name each time so possible to get it wrong.

    If you’re not after a direct 1:1 mapping of values between two fields, then we can continue down the earlier pathway. If you’re only after 1:1 this would work for you.

    One last piece is if you want to make the field editable but still autocompleted, this script will handle that for you. In this case there are three fields: state, country and the script field. State and country are text fields whilst the script field is what we use to retain some state and set values. The script below handles this and includes a reference to itself to allow manual overrides to be preserved when autocomplete would overwrite:

    function Autocomplete_Country() {
    	var issuing_state = record.getFieldValue('fld-34f6d74d34354f63889f63a705b7d15f');
    	
    
    	var issuing_country_id = 'fld-108b77c0b088457d90671dd71edf3247';
    	var issuing_country = record.getFieldValue(issuing_country_id);
    
    	var autocomplete_country_id = 'fld-cf8a4c138a3a437991182f17000580d6';
    	var previous_country = record.getFieldValue(autocomplete_country_id);
    	
    	if (issuing_country && issuing_country != previous_country)
    	{
    		return previous_country;
    	}
    
    	var country = '';
    	
    	switch (issuing_state)
    	{
    		case 'Washington':
    		case 'Oregon':
    		case 'California':
    			country = 'United States of America';
    			break;
    		case 'New South Wales':
    		case 'Victoria':
    		case 'Queensland':
    			country = 'Australia';
    			break;
    		case 'British Columbia':
    		case 'Alberta':
    			country = 'Canada';
    			break;
    	}
    	
    	record.setFieldValue(issuing_country_id, country);
    	form.saveAllChanges();
    	return country;
    }
    
    Autocomplete_Country();
    July 10, 2020 at 10:00 AM #41247

    pierrot_rennes
    Participant

    Hi Sam,
    Oh you spent a little time doing this !! Well done !!!
    I will look at this quietly and try to apply this to my needs
    Thank you very much
    If I don’t understand, I will ask you
    Have a nice week end

    July 10, 2020 at 12:28 PM #41249

    Brendan
    Keymaster

    I know it’s not the same as what you’re doing, but I did once write a script that switched pick lists depending on the value selected from another Pick List. This script switches between a Canadian Provinces and US Sates Pick List depending on the value of the country selected.

    function Province_Pick_List_Switcher() {
    
    	var country_id = 'fld-de7985c881804154b037c68dc4c24414';
    	var country = record.getFieldValue(country_id);
    	var canadian_provinces_pick_list = document.getPickListNamed('Canadian Provinces');
    	var us_states_pick_list = document.getPickListNamed('US States');
    	var province_id = 'fld-b1458c16690744129ff145f7b9607c99';
    	var province = form.getFieldWithId(province_id);
    	
    	if (country == 'Canada') {
    		province.pickList = canadian_provinces_pick_list;
    		
    	} else if (country == 'United States') {
    		province.pickList = us_states_pick_list;
    		
    	} else {
    		province.pickList = null;
    	}
    	
    }
    
    Province_Pick_List_Switcher();

    It worked well, but only with the Single and Multi-Valued Pick Lists. Because those fetch their Pick List values when you display the popover as opposed to the Checkbox, Radio Button, Combo Box, etc., which pre-load all the values to display.

    July 10, 2020 at 9:59 PM #41254

    Sam Moffatt
    Participant

    Wouldn’t that break if you switched records without changing the country first? (assuming different countries between records)

    July 11, 2020 at 12:54 AM #41257

    Brendan
    Keymaster

    Oops. Actually you’re right. Because the script doesn’t get executed until you select from the first pick list. Darn.

    July 11, 2020 at 1:53 AM #41258

    pierrot_rennes
    Participant

    Hi,

    i just need two pick lists
    The first with brands
    The second, the countries
    If in the Brands field, I choose for example Ravensburger in the first list, it displays France in the Country field
    A brand equals a country
    Best

    July 11, 2020 at 4:58 PM #41263

    Sam Moffatt
    Participant

    You can use any of the scripts I wrote with pick lists. You just need to define the two pick lists in the Tap Forms preferences for your document and assign each of the text fields to the correct pick list. Make sure you use a single value select list type (e.g. single value popover, combobox, popup button or radio) because the multivalue varieties would mess up the mapping.

Viewing 10 reply threads

You must be logged in to reply to this topic.