Prompter popup option with key/value pairs

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Prompter popup option with key/value pairs

Viewing 7 reply threads
  • Author
    Posts
  • February 20, 2020 at 8:29 AM #39593

    Bernhard
    Participant

    Hi all,

    first – I’d like it very much how fast I’m able to develop database applications using Tap Forms.

    In my current application I want to have the user choose records by their title field using a Prompter popup. In the Prompter callback I want to access the choosen record by form.getRecordWithId(). Because I only get the title in the callback I have to use form.getSearchNamed() and search.getRecords() to get access to the choosen record.

    Now my question is: Would it be possible to change the Prompter class so it will take key/value pairs as options and return the choosen key/value pair in the callback?

    Thanks a lot,
    Bernhard

    February 20, 2020 at 1:29 PM #39603

    Brendan
    Keymaster

    Hi Bernhard,

    I’m not entirely sure how that would work for your situation other than to program Tap Forms specifically to allow a string / record ID key/value mapping. But then how does Tap Forms know to relate your title field to the record ID? Generic key/value mapping is one thing, but when it relates specifically to fetching a record, it’s tricky.

    However, in the latest beta update I added a new search API that lets you do a generic text search on your records. Like typing into the main Search field. You can now do form.getRecordsForSearchTerm();. You’ll get back an array of records. If there’s only 1 result, you know you’ve got an exact match. Or you can just pull off the record from the first element of the array to get the first occurrence. And if you pass in double-quotes in your search, then Tap Forms will do an exact match search, but the result will still be an array of records. Just one record in that case.

    Thanks,

    Brendan

    February 20, 2020 at 2:07 PM #39605

    Sam Moffatt
    Participant

    Depending on how you’re composing the strings to put in the field to begin with, you can build the inverted index yourself and then use that to back reference the original document ID with the result you get back from the prompter. I’ll dig a little longer to see if I can find an example of code that does something like that.

    February 21, 2020 at 8:00 AM #39609

    Bernhard
    Participant

    Sounds good – thanks for the advice!
    For the record, here is how it works:

    
    var customerRecordIds = [];
    
    // Callback function of the Prompter call.
    function chooseCustomerCallback() {
    	createInvoice(customerRecordIds[choosenTitle]);
    }
    
    // Do something with the record after the user choose one.
    function createInvoice(recordId) {
    	var customerRecord = form.getRecordWithId(recordId);	
    	...
    }
    
    // The entry function of the script
    function Erstelle_Rechnung() {	
    	var titleFieldId = 'fld-aa817f3bd885458883d3e25802fd4037';
     	var customersForm = document.getFormNamed('Kunden');
     	var customers = customersForm.getRecords();
    	var companyTitles = [];
    	
    	for (var index = 0, count = customers.length; index < count; index++) {
    		const title = customers[index].getFieldValue(titleFieldId);
    		companyTitles.push(title);
    		// Remember the Record ID of this customer record to be used later.
    		customerRecordIds[title] = customers[index].getId();
    	}
    
    	var choosenTitle;
    
    	let prompter = Prompter.new();
    	prompter.addParameter('Kunde', 'choosenTitle', 'popup', companyTitles)
    		.show('Message prompt', chooseCustomerCallback);
    
    	return null;
    }
    
    Erstelle_Rechnung();
    
    February 21, 2020 at 8:03 AM #39611

    Bernhard
    Participant

    @Sam: Sounds good – thanks for the advice!
    For the record, here is how it works:

    
    var customerRecordIds = [];
    
    // Callback function of the Prompter call.
    function chooseCustomerCallback() {
    	createInvoice(customerRecordIds[choosenTitle]);
    }
    
    // Do something with the record after the user choose one.
    function createInvoice(recordId) {
    	var customerRecord = form.getRecordWithId(recordId);	
    	...
    }
    
    // The entry function of the script
    function Erstelle_Rechnung() {	
    	var titleFieldId = 'fld-aa817f3bd885458883d3e25802fd4037';
     	var customersForm = document.getFormNamed('Kunden');
     	var customers = customersForm.getRecords();
    	var companyTitles = [];
    	
    	for (var index = 0, count = customers.length; index < count; index++) {
    		const title = customers[index].getFieldValue(titleFieldId);
    		companyTitles.push(title);
    		// Remember the Record ID of this customer record to be used later.
    		customerRecordIds[title] = customers[index].getId();
    	}
    
    	var choosenTitle;
    
    	let prompter = Prompter.new();
    	prompter.addParameter('Kunde', 'choosenTitle', 'popup', companyTitles)
    		.show('Message prompt', chooseCustomerCallback);
    
    	return null;
    }
    
    Erstelle_Rechnung();
    
    February 21, 2020 at 8:09 AM #39612

    Bernhard
    Participant

    @Brendan: I think of something like the behaviour of the HTML SELECT tag:

    
    <option value="key1">Value 1</option>
    <option value="key2">Value 3</option>
    ...
    

    For the Prompter class that means smething like:

    
    var options = [
    { key: key1, value: 'Value 1' },
    { key: key2, value: 'Value 2' },
    ];
    
    prompter.addParameter('Name', 'name', 'popup', options)
    		.show('Message prompt', chooseCustomerCallback);
    

    Or instead of changing the options structure an additional parameter to prompter.addParameter() for just the keys may be the better idea.

    February 23, 2020 at 12:52 AM #39620

    Brendan
    Keymaster

    Hi Bernhard,

    Yes, that definitely makes sense in the way you’ve explained it for the key/value options. However, that still doesn’t get you to a record ID unless you happen to know what the internal record IDs are that you could put into the keys in the dictionary.

    February 23, 2020 at 1:30 AM #39623

    Bernhard
    Participant

    Hi Brendan, you are right – one have to know the record IDs. For that you already provided the record.getId() method so no trouble here :-)

Viewing 7 reply threads

You must be logged in to reply to this topic.