Search Results for 'form.getRecords'
-
Search Results
-
Topic: Script stopped working
I have a Script that used to work perfectly but has stopped working in recent versions of Tap Forms and can’t workout why.
The script is supposed to take a search term from clipboard, do a search, if one result is found bring up a screen displaying the “Comments” field and also copy it to the clipboard. If multiple results are found it presents them, you pick one and then it also displays the “Comments” field of the selected one and copies it to the clipboard. What has stopped this working? I’m not getting any errors in the Console and the variable references are correct.
var myForm = document.getFormNamed('Travel Database'); var records = myForm.getRecords(); var search_term = Utils.copyTextFromClipboard(); var result_count = 0; var results = []; var selected; function copy_comments( comments ) { Utils.copyTextToClipboard( comments ); } function multiple_results() { var joined = '--multiple_matches--'; var res; for (res of results) { joined = joined + res.location + '::' + res.comment; } copy_comments( joined ); } function search_records( haystack , needle ) { var location_id = 'fld-c55265c3f56b43feb423f5a198dffe6c'; var comment_id = 'fld-141d923e785148e3aec84576c746a4a4'; var rec; for (const rec of haystack) { if ( rec.getFieldValue( location_id ).toLowerCase().includes( needle.toLowerCase() ) ) { results.push( { location: rec.getFieldValue( location_id ) , comment: rec.getFieldValue( comment_id ) } ); result_count++; } } if( result_count == 0 ){ console.log( 'No results found!' ); }else if( result_count > 1 ){ multiple_results(); }else{ copy_comments( results[0].comment ); } } search_records( records , search_term );
Topic: Getting favicons
I’ve been using a form for storing bookmarks but text-only makes it hard to browse the list. Here’s a script that automatically fetches the favicon for each URL in a form. Obviously you would need to update with your own ids.
// https://stackoverflow.com/a/54947757/952123 const getHostname = (url) => { // run against regex const matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i); // extract hostname (will be null if no match is found) return matches && matches[1]; } function favIconURL(domain) { return
https://www.google.com/s2/favicons?domain=${domain}
; } function getFavIconURL(url) { return favIconURL(getHostname(url)); } function getFavicon(r) { const url = r.getFieldValue('fld-026ad10a88d74569a6d37b19aa7b77a6'); console.log(getFavIconURL(url)); r.addPhotoFromUrlToField(getFavIconURL(url), 'fld-42eeb21b65464cb39aa966772620acba', { "filename": "favicon", "prevent_duplicates": true}) } function getIfEmpty(r) { const thumbnail = r.getFieldValue('fld-42eeb21b65464cb39aa966772620acba'); if (thumbnail.length) return; return getFavicon(r); } form.getRecords().forEach(getIfEmpty); form.saveAllChanges();Topic: JavaScript API 5.3
Hi Brendan – I have a couple of questions. Have the following methods been deprecated.
form.getRecordsForSearchTerm()
andrecord.setRecordColor()
It doesn’t show up in the IntelliSense. Also, in the future will you being adding a feature to recalculate a form via the javascript API?Oh, I’m running version 5.3.11 (Build 962)
thanks,
rockyI am getting the following error:
02/05/2020, 18:24:01 / Time spent / add 0s to Time charged for (not empty)
add 0s to Time charged for (not empty): TypeError: value.replace is not a function. (In ‘value.replace(find_text, replace_with)’, ‘value.replace’ is undefined), line:(null)when running the following script
var time_charged_for_id = 'fld-755fd264b59b42e59c7254edf03ea281'; function findAndReplace(find_text, replace_with) { for (let rec of form.getRecords()){ let value = rec.getFieldValue(time_charged_for_id); if (value) { rec.setFieldValue(time_charged_for_id, value.replace(find_text, replace_with)); } } form.saveAllChanges(); return; } findAndReplace(/[a-zA-Z0-9]*/, '0');
I created a test database and it ran and did what is should. But in a working database it is causing the error. The field type is the same (a number field) in the test database and the working database.
The code was provided by Daniel Leu in the exchanges at https://www.tapforms.com/forums/topic/find-and-replace-script-to-replace-anything/.
Because I do not really have any understanding of JavaScript I cannot tell why it is not working.
I would grateful for any help in identifying what is wrong.
I found the find and replace script posted by Brendan at https://www.tapforms.com/forums/topic/find-and-replace-script/ very helpful, but would like some help in adapting it:
var field_id = 'fld-64b0b17a635f49b8b40867e45f8d24db'; function findAndReplace(find_text, replace_with) { var records = form.getRecords(); for (var index = 0, count = records.length; index < count; index++){ var rec = records[index]; var value = rec.getFieldValue(field_id); if (value == find_text) { value = replace_with; rec.setFieldValue(field_id, value); } } document.saveAllChanges(); return; } findAndReplace('Ford', 'Tesla');
specifically:
1. at present it searches for a specific item in a specific field. How is it possible to search for anything in that field and replace with specific text (so rather than ‘Ford’ in Brendan’s example, there might be Ford, Mercedes or BMW etc). I have tried adding a wildcard indicator (*), but it does not work.
2. A variation on 1. is where there is nothing in the specific field. Changing ‘Ford’ in Brendan’s example, to ” also does not work.
Is this possible with the script?
Any help would be gratefully received.