Blog  |  Support  |  Forums

Search Results for 'form.getRecords'

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Search Search Results for 'form.getRecords'

Viewing 15 results - 91 through 105 (of 146 total)
  • Author
    Search Results
  • #42842
    Brendan
    Keymaster

    Hi Chris,

    You’re right. It’s not working right for checkmark field types. I’m working on fixing that. In fact, I just fixed it (for the next update).

    In the meantime, you can write a Form Script to accomplish the same thing that loops through the records, looking for the checkmark field value and turning it off if it’s on.

    Are you familiar with scripts in Tap Forms yet? Here’s a sample bit of code that will do that for you, but you’d have to put your own Checkmark field’s ID in it:

    function Toggle_Checkmark() {
       var records = form.getRecords();
       var check_mark_id = 'fld-f14ccd51913b4348b2c32d9ac4eaf7f2';
    
       for (var index = 0, count = records.length; index < count; index++){
    	// do something;
    	var record = records[index];
    	var value = record.getFieldValue(check_mark_id);
    	if (value == true) {
    		record.setFieldValue(check_mark_id, 0);
    	}
       }
       form.saveAllChanges();
    }
    
    Toggle_Checkmark();

    Thanks,

    Brendan

    Sam Moffatt
    Participant

    If you were to aim to optimise the script, I’d probably want to have a map of PDF name to Tap Forms record ID. You can do a retrieve of a record by key/record ID via the script so if you precomputed a map of these keys or used the record ID import mode for Tap Forms to import the records then it’ll be a single retrieve for retrieving the record (form_record_id is the field in the CSV file which if present will be prepended with a rec- prefix and allow you to use form.getRecordWithId). The horse has likely already bolted for this one but something to consider for future projects, so let’s go ahead with the other suggestion: PDF attachment name in a script field.

    If you have the PDF attachment name in a script field, you could use the getRecordFromFormWithKey function in my script manager. The method signature looks like this:

    getRecordFromFormWithKey(formName, keyFieldId, keyValue, createIfMissing = true, earlyTermination = true, alwaysScanOnMiss = false)
    

    It creates in Javascript an index on a first pass and then does looks up from there (set earlyTermination to false and alwaysScanOnMiss to true). When you’re in a tight loop this should improve performance by doing the record loop once to build the index and then you can do a similar sort of roughly constant time retrieval for matching records. It’s still two loops but you remove the nesting and replace it with a more optimised data structure.

    First step is to create a new script field in your Contracts form and in it you want to add in the code we use to grab the filename from the PDF:

    record.getFieldValue('fld-e870153bd0a647c9823ca998c213d1fd')[0].filename;
    

    You’ll need to tick the “Update records when saving” option when creating that to populate the new field in all of your record. Essentially we’re just copying the filename as a new field. At this point you might also want to setup a search to look for records that have this field empty because that seems likely a bug. You’ll also need the field ID from the form panel (underneath the “Description” field) which I refer to later as “fld-scriptfieldidhere” that you’ll need to change.

    Using the code you provided as a basis, something like this should get you some of the way there:

    document.getFormNamed('Script Manager').runScriptNamed('getRecordFromFormWithKey');
    
    function Update_Records() {
    
        var csvForm = document.getFormNamed("csv");
    
        // Contracts
        var keyword_id = 'fld-a2126cbe512646e9ba144fb5a90b06dc';
        var pdf_id = 'fld-3ea77ccc18604174b722ece105965c44';
    
        // get use fixed field Id
        var csvPdf_id = 'fld-4bcd0f1fba5c4178bb8d10a112b17489';
        var csvKeyword_id = cvsForm.getFieldNamed('Keyword').getId();
    
        // Loop over all csv entries
        for (entry of csvForm.getRecords()) {
            // get pdf file name of CSV record
            var csvPdfName = entry.getFieldValue(csvPdf_id);
    
            // replace spaces with underscores
            csvPdfName = csvPdfName.replace('/ /g', '_');
            console.log("Processing: " + csvPdfName);
    
            contract = getRecordFromFormWithKey("Contracts", "fld-scriptfieldidhere", csvPdfName, false, false, true);
    
            if (!contract) {
                console.log('Unable to find matching PDF record: ' + csvPdfName);
                continue;
            }
    
            console.log("Found match: " + csvPdfName);
    
            // Update contract record
            var type_of_agreement_id = 'fld-6af499ed439143b297828643341d939b'
            contract.setFieldValue(type_of_agreement, 'Licensing Agreement')
    
            // This should “replace” keyword or keywords in a selected record
            var agreement_keywords_id = 'fld-a2126cbe512646e9ba144fb5a90b06dc';
            contract.setFieldValue(agreement_keywords_id, entry.getFieldValue(csvKeyword_id));
        }
        document.saveAllChanges();
    
        return 'done';
    }
    
    Update_Records();
    

    We’ve still got the outer loop that iterates over the incoming CSV form, the “Contracts” loop is removed and replaced with the call to getRecordsFromFormWithKey which internally does a loop for you but also builds an index in memory that should make subsequent accesses quicker. Watch your memory usage in Activity Monitor but you’ve got 64GB of RAM so we should be fine. The script is finding the record for you so the code that lived inside your inner for loop moves up a level though there is a check to see if we got a record back. I did some minor changes to set the keyword based on the CSV form as an example of how I think that to work.

    One thing I’ve done in scripts like this is put in a field for when it was processed to be able to skip it and also a counter to limit how many records are processed. This is useful for debugging scripts and being able to progressively execute it to get an idea of it:

    let minTime = new Date().getTime() - 86400000; // one day
    let processed = 0;
    let skipped = 0;
    
    for (...) {
    
        let previousDate = entry.getFieldValue('fld-previousdateidhere');
        if (previousDate && previousDate.getTime() > minTime) {
            skipped++;
            continue;
        }
    
        entry.setFieldValue('fld-previousdateidhere', new Date());
    
        processed++;
    
        if (processed > 100) {
            break;
        }
    
        // your existing for loop logic here
    }
    

    This uses an extra field and updates when the script is run to set it that way the next time you run the script, it skips past records it’s already “processed” so to speak and moves onto the next batch of 100. Instead of handling the date logic in code, you could also tie this into a saved search as well so that Tap Forms only gives you the candidate records.

    Brent S
    Participant

    This is how I tried to fix, but still have the two loops:

    function Update_Records() {

    var csvForm = document.getFormNamed(“csv”);
    var contractsForm = document.getFormNamed(“Contracts”);

    // Contracts
    var keyword_id = ‘fld-a2126cbe512646e9ba144fb5a90b06dc’;
    var pdf_id = ‘fld-3ea77ccc18604174b722ece105965c44’;

    // get use fixed field Id
    var csvPdf_id = ‘fld-4bcd0f1fba5c4178bb8d10a112b17489’;

    // Loop over all csv entries
    for (entry of csvForm.getRecords()){
    // get pdf file name of CSV record
    var csvPdfName = entry.getFieldValue(csvPdf_id);

    // replace spaces with underscores
    csvPdfName = csvPdfName.replace(‘/ /g’, ‘_’);
    console.log(“Processing: ” + csvPdfName);

    // Loop over all contract records
    for (contract of contractsForm.getRecords()){
    if (!contract.getFieldValue(pdf_id) || contract.getFieldValue(pdf_id).length == 0) {
    console.log(‘Record is missing PDF: ‘ + contract.getUrl());
    continue;
    }

    // this assumes that there is only one PDF per contract!
    var pdfName = contract.getFieldValue(pdf_id)[0].filename;

    if (csvPdfName == pdfName){
    console.log(“Found match: ” + pdfName);

    // Update contract record
    var type_of_agreement_id = ‘fld-6af499ed439143b297828643341d939b’
    record.setFieldValue (type_of_agreement, ‘Licensing Agreement’)

    // This should “replace” keyword or keywords in a selected record
    var agreement_keywords_id = ‘fld-a2126cbe512646e9ba144fb5a90b06dc’;
    record.setFieldValue (agreement_keywords_id, ‘Vaccine, Animal, Bacterial’)

    break;
    }

    }
    document.saveAllChanges();

    }

    return ‘done’;
    }

    Update_Records();

    Daniel Leu
    Participant

    Brent, great to hear that it worked, finally!

    Is there a way you can select the 1200 file records with the advanced search? If that’s the case you can select the saved search and just run the script with a little change:

    Before:
    for (contract of contractsForm.getRecords()){

    After:
    for (contract of search.getRecords()){

    It is strange to get the ’empty records’ notice although there should be a file associated with every record. That’s something I would look into.

    Fetching the file record might be what takes a bit of time. If you run this script frequently, it might be worth a try to save the PDF filename in a field and then just use this field in the comparison.

    Oh, don’t forget to backup your database, specially with so many entries!

    I might add a little sanity check as well to verify that a record was really found:

    		var success = 0;
    		// Loop over all contract records
    		for (contract of contractsForm.getRecords()){
    			if (!contract.getFieldValue(pdf_id) || contract.getFieldValue(pdf_id).length == 0) {
    				console.log('Record is missing PDF: ' + contract.getUrl());
    				continue;
    			}
    
    			// this assumes that there is only one PDF per contract!
    			var pdfName = contract.getFieldValue(pdf_id)[0].filename;
    			
    			if (csvPdfName == pdfName){
    				console.log("Found match: " + pdfName);
    				success = 1;
    				
    				// Update contract record
    				addKeyword(contract, keyword_id, entry.getFieldValue(csvKeyword_id));
    			
    				break;
    			}
    		
    		}
    		if (!success) {
    			console.log("No match found: " + csvPdfName, "#FF0000");
    		}
    Daniel Leu
    Participant

    Your code is a bit messed up with two contract loops…

    unction addKeyword(rec, kw_id, kw){
    	var keywords = rec.getFieldValue(kw_id);
    	
    	if (keywords == undefined || keywords == ""){
    		rec.setFieldValue(kw_id, kw);
    	} else {
    		rec.setFieldValue(kw_id, keywords + ',' + kw);
    	}
    }
    
    function Update_Records() {
    
    	var csvForm = document.getFormNamed("csv");
    	var contractsForm = document.getFormNamed("Contracts");
    	
    	// recipes
    	var keyword_id = 'fld-a2126cbe512646e9ba144fb5a90b06dc';
    	var pdf_id = 'fld-3ea77ccc18604174b722ece105965c44';
    
    	// get csv field Ids
    //	var csvPdf_id = csvForm.getFieldNamed('PDF').getId();
    //	var csvKeyword_id = csvForm.getFieldNamed('Keyword').getId();
    
    	// get use fixed field Ids!!!!!!!!!!!!
    	var csvPdf_id = 'fld-0cbd22a473cd4c58aa1dc47341a7157b';
    	var csvKeyword_id = 'fld-53de1c4c633a4da6a4d0213158b5ef0a';
    
    	// Loop over all csv entries
    	for (entry of csvForm.getRecords()){
    		// get pdf file name of CSV record
    		var csvPdfName = entry.getFieldValue(csvPdf_id);
    
    		// replace spaces with underscores
    		csvPdfName = csvPdfName.replace('/ /g', '_');
    		console.log("Processing: " + csvPdfName);
    		
    		// Loop over all contract records
    		for (contract of contractsForm.getRecords()){
    			if (!contract.getFieldValue(pdf_id) || contract.getFieldValue(pdf_id).length == 0) {
    				console.log('Record is missing PDF: ' + contract.getUrl());
    				continue;
    			}
    
    			// this assumes that there is only one PDF per contract!
    			var pdfName = contract.getFieldValue(pdf_id)[0].filename;
    			
    			if (csvPdfName == pdfName){
    				console.log("Found match: " + pdfName);
    				
    				// Update contract record
    				addKeyword(contract, keyword_id, entry.getFieldValue(csvKeyword_id));
    			
    				break;
    			}
    		
    		}
    		document.saveAllChanges();
    	
    	}
    	
    	return 'done';
    }
    
    Update_Records();
    Sam Moffatt
    Participant

    I’d also to check that there is a file attachment and skip ones that don’t:

    		// Loop over all contract records
    		for (contract of contractsForm.getRecords()){
    			// this assumes that there is only one PDF per recipe!
    			var pdfName = contract.getFieldValue(pdf_id)[0].filename;
    

    to:

    
    		// Loop over all contract records
    		for (contract of contractsForm.getRecords()){
    			if (!contract.getFieldValue(pdf_id) || contract.getFieldValue(pdf_id).length == 0) {
    				console.log('Record is missing PDF: ' + contract.getUrl());
    				continue;
    			}
    			// this assumes that there is only one PDF per recipe!
    			var pdfName = contract.getFieldValue(pdf_id)[0].filename;
    

    That should check there is an attachment and validate it’s not set plus log a message with a clickable link in the console. If that ends up being everything then that’s not great but it wouldn’t bomb part way through.

    There are 35,747 entries (PDF’s), so it is only one field, but there are many entries.

    Many records or a single record with multiple PDFs?

    Re feedback: The last few videos hit half an hour and I need to work on getting the content back in the 10 minute range. I ended up throwing a bunch of stuff in one of the videos which when I went back should have probably ended up broken out into a few videos (ended up putting in chapter links for at least one of them). I’m trying to keep them as continuous takes to make sure I don’t miss any steps along the way though sometimes that leads to mild confusion as it’s also not scripted/practiced. Sometimes seeing something small and seemingly unimportant is the hook you need to understand. Thanks for the feedback!

    Daniel Leu
    Participant

    In hindsight, it would have been better if you had described your problem as is and not made up this hypothetical recipe case….

    As you might have noticed, all CSV form related fields use the a csv prefix to make it clear where this field resided. And yes, my script is a from script that is part of the contracts form. My assumption was that the CSV form is deleted after use and recreated with a new tagging set. So it makes sense that the script has a more permanent location.

    Here is the updated script. Hope this works.

    function addKeyword(rec, kw_id, kw){
    	var keywords = rec.getFieldValue(kw_id);
    	
    	if (keywords == undefined || keywords == ""){
    		rec.setFieldValue(kw_id, kw);
    	} else {
    		rec.setFieldValue(kw_id, keywords + ',' + kw);
    	}
    }
    
    function Update_Records() {
    
    	var csvForm = document.getFormNamed("csv");
    	var contractsForm = document.getFormNamed("Contracts");
    	
    	// recipes
    	var keyword_id = 'fld-a2126cbe512646e9ba144fb5a90b06dc';
    	var pdf_id = 'fld-3ea77ccc18604174b722ece105965c44';
    
    	// get csv field Ids
    //	var csvPdf_id = csvForm.getFieldNamed('PDF').getId();
    //	var csvKeyword_id = cvsForm.getFieldNamed('Keyword').getId();
    
    	// get use fixed field Ids!!!!!!!!!!!!
    	var csvPdf_id = 'fld-0cbd22a473cd4c58aa1dc47341a7157b';
    	var csvKeyword_id = 'fld-53de1c4c633a4da6a4d0213158b5ef0a';
    
    	// Loop over all csv entries
    	for (entry of cvsForm.getRecords()){
    		// get pdf file name of CVS record
    		var cvsPdfName = entry.getFieldValue(csvPdf_id);
    
    		// replace spaces with underscores
    		cvsPdfName = cvsPdfName.replace('/ /g', '_');
    		console.log("Processing: " + cvsPdfName);
    		
    		// Loop over all contract records
    		for (contract of contractsForm.getRecords()){
    			// this assumes that there is only one PDF per recipe!
    			var pdfName = contract.getFieldValue(pdf_id)[0].filename;
    			
    			if (cvsPdfName == pdfName){
    				console.log("Found match: " + pdfName);
    				
    				// Update contract record
    				addKeyword(contract, keyword_id, entry.getFieldValue(csvKeyword_id));
    			
    				break;
    			}
    		
    		}
    		document.saveAllChanges();
    	
    	}
    	
    	return 'done';
    }
    
    Update_Records();
    
    Sam Moffatt
    Participant

    A few more questions if I may. I have tried to figure things out by watching some of Sam’s videos etc., but I am a little stuck.

    Curious for feedback, were any of them helpful? What was not helpful and what would improve it? What was helpful and how was it helpful? I had thought about doing another simple intro one on the next few snippets which includes functions but haven’t done it. I tried another video on some other topics but wasn’t happy with how it turned out.

    So first, from my reading it looks like I need to use a “Form Level Script” as opposed to a “Field Level Script” as I am updating existing records.

    I think my rule of thumb is that field scripts are meant to act on the contents of the record as those contents change whilst form scripts are more one off or bulk action changes. You can use them somewhat interchangeably and the question is do you want this thing to run all of the time or is it something you want to control when it runs?

    I only have two forms, my main database and the cvs data I want to input. Is it correct that I must place the Form Level Script in my main database and not in the cvs form? (That seems to be the only way I can get the correct fld-hash numbers that I need into the script).

    While it might not be intuitive you can access any form and any field from scripts (fields or forms) in any form within a document. The editor won’t show you the fields that aren’t directly linked to the form that you’re in but you can use the editor to go to other forms and get their field ID’s. You can also get the field ID from below the description box in the field editor UI.

    You can use the code button to wrap your code with backticks (e.g. `code here`) to make it a little easier to read (also ran it through a formatter since the post didn’t have any indentation):

    function addKeyword(rec, kw_id, kw) {
    
    	var keywords = rec.getFieldValue(kw_id);
    
    	if (keywords == undefined || keywords == "") {
    		rec.setFieldValue(kw_id, kw);
    	} else {
    		rec.setFieldValue(kw_id, keywords + ',' + kw);
    	}
    }
    
    function Update_Records() {
    
    	var csvForm = document.getFormNamed("csv");
    	var ContractsForm = document.getFormNamed("Contracts");
    
    	// Contracts
    	var pdffilepath_7_id = 'fld-3ea77ccc18604174b722ece105965c44';
    	var license_category_id = 'fld-d63ab1856e554efebe62b9f1826c388d';
    	var license_subcategory_id = 'fld-f0c47c7e6ad244fe955e2c24cb448590';
    	var agreement_keywords_id = 'fld-a2126cbe512646e9ba144fb5a90b06dc';
    
    	// get csv field Ids
    	var pdffilepath_7 = cvsForm.getFieldNamed('PDF').getId();
    	var license_category = cvsForm.getFieldNamed('Category').getId(); var license_subcategory = cvsForm.getFieldNamed('Subcategory').getId();
    	var agreement_keywords = cvsForm.getFieldNamed('Keyword').getId();
    
    	// Loop over all csv entries
    	// So this tells my Contracts database to look at the cvs form?
    	for (entry of cvsForm.getRecords()) {
    
    		// get pdf file name
    		// is it looking for another fld-hash here?
    		var pdfName = entry.getFieldValue(csvPdf_id);
    
    		// replace spaces with underscores
    		pdfName = pdfName.replace('/ /g', '_');
    		console.log("Processing: " + pdfName);
    
    		// Loop over all LICENSEdb records
    		for (Contracts of Contracts.getRecords()) {
    
    			// this assumes that there is only one PDF per Contract
    			// where have I defined (pdf_id) before? Do I need to ?
    			var filename = Contract.getFieldValue(pdf_id)[0].filename;
    
    			if (pdfName == filename) {
    				console.log("Found match: " + filename);
    
    				// Update Contracts record
    				addsubcategory(Contracts, subcategory_id, entry.getFieldValue(csvKeyword_id));
    
    				// Update Contracts record
    				addKeyword(Contracts, keyword_id, entry.getFieldValue(csvSubcategory_id));
    
    				// Set category
    				Contracts.setFieldValue(category_id, entry.getFieldValue(csvCategory_id))
    
    				break;
    			}
    		}
    	}
    	document.saveAllChanges();
    
    	return 'done';
    }
    
    Update_Records();
    

    First thing I noticed when I put it through a formatter was that there seemed to be a missing bracket, I’ve added an extra one before the document.saveAllChanges() line and everything balanced out. I used Visual Studio Code to reformat it but I also found an online JavaScript beautifier as well.

    Next, much of the script makes sense to me, and other parts seem somewhat circular. Could you look what I have done and see where I messed up? …because I think I did a good job on that : )

    I need the script to look up each record that aligns with the PDF file name in the cvs file, and then update the records in the main database form called “Contracts”

    Now I see you added an extra call:

    addsubcategory (Contracts, subcategory_id, entry.getFieldValue(csvKeyword_id));
    

    What this change is doing is trying to call a function here called addsubcategory but that function doesn’t exist. I think you’re trying to copy the addKeyword line for a new field and if you look at the very top of the script there is a function addKeyword which defines or creates the function. If what you’re trying to do is create a comma separated list of sub categories but in a different field, then I don’t think you need to do a new function, I think you can just call addKeyword with the sub category field ID:

    addKeyword (Contracts, subcategory_id, entry.getFieldValue(csvKeyword_id));
    

    Try fixing the curly braces and changing addsubcategory over to addkeyword as above and see if that gets you a little closer.

    Brent S
    Participant

    Hi Daniel,

    A few more questions if I may. I have tried to figure things out by watching some of Sam’s videos etc., but I am a little stuck.

    So first, from my reading it looks like I need to use a “Form Level Script” as opposed to a “Field Level Script” as I am updating existing records.

    I only have two forms, my main database and the cvs data I want to input. Is it correct that I must place the Form Level Script in my main database and not in the cvs form? (That seems to be the only way I can get the correct fld-hash numbers that I need into the script).

    Next, much of the script makes sense to me, and other parts seem somewhat circular. Could you look what I have done and see where I messed up? …because I think I did a good job on that : )

    I need the script to look up each record that aligns with the PDF file name in the cvs file, and then update the records in the main database form called “Contracts”

    I have attached a few screen shots in case that helps with context.

    Thanks again.
    Brent

    ——————-

    function addKeyword(rec, kw_id, kw) {

    var keywords = rec.getFieldValue(kw_id);

    if (keywords == undefined || keywords == “”){
    rec.setFieldValue(kw_id, kw);
    } else {
    rec.setFieldValue(kw_id, keywords + ‘,’ + kw);
    }
    }

    function Update_Records() {

    var csvForm = document.getFormNamed(“csv”);
    var ContractsForm = document.getFormNamed(“Contracts”);

    // Contracts
    var pdffilepath_7_id = ‘fld-3ea77ccc18604174b722ece105965c44’;
    var license_category_id = ‘fld-d63ab1856e554efebe62b9f1826c388d’;
    var license_subcategory_id = ‘fld-f0c47c7e6ad244fe955e2c24cb448590’;
    var agreement_keywords_id = ‘fld-a2126cbe512646e9ba144fb5a90b06dc’;

    // get csv field Ids
    var pdffilepath_7 = cvsForm.getFieldNamed(‘PDF’).getId();
    var license_category = cvsForm.getFieldNamed(‘Category’).getId(); var license_subcategory = cvsForm.getFieldNamed(‘Subcategory’).getId();
    var agreement_keywords = cvsForm.getFieldNamed(‘Keyword’).getId();

    // Loop over all csv entries
    // So this tells my Contracts database to look at the cvs form?
    for (entry of cvsForm.getRecords()){

    // get pdf file name
    // is it looking for another fld-hash here?
    var pdfName = entry.getFieldValue(csvPdf_id);

    // replace spaces with underscores
    pdfName = pdfName.replace(‘/ /g’, ‘_’);
    console.log(“Processing: ” + pdfName);

    // Loop over all LICENSEdb records
    for (Contracts of Contracts.getRecords()){

    // this assumes that there is only one PDF per Contract
    // where have I defined (pdf_id) before? Do I need to ?
    var filename = Contract.getFieldValue(pdf_id)[0].filename;

    if (pdfName == filename){
    console.log(“Found match: ” + filename);

    // Update Contracts record
    addsubcategory (Contracts, subcategory_id, entry.getFieldValue(csvKeyword_id));

    // Update Contracts record
    addKeyword(Contracts, keyword_id, entry.getFieldValue(csvSubcategory_id));

    // Set category
    Contracts .setFieldValue(category_id, entry.getFieldValue(csvCategory_id))

    break;
    }

    }
    document.saveAllChanges();

    return ‘done’;
    }

    Update_Records();

    Attachments:
    You must be logged in to view attached files.
    Daniel Leu
    Participant

    You can import a csv file into a separate form and then use a script to process all records and update your reference records accordingly. Then you can assign values of meal choices accordingly and update your keywords field as well.

    But this seems to be a rather tedious job if you want to update all 50k food recipes in your Tap Forms database this way. I would start with looking into software that converts your PDFs into text files. Then you can add the content of your text files into fields inside Tap Forms. These fields are searchable, which makes it much easier to categorize your recipes. But yeah, it might be a challenge to convert your PDFs, specially if they were scanned and the quality is poor.

    But if you want to go the CSV route, I would create a spreadsheet with columns for PDF, Category, and Keyword. Here is my example:

    function addKeyword(rec, kw_id, kw){
    	var keywords = rec.getFieldValue(kw_id);
    	
    	if (keywords == undefined || keywords == ""){
    		rec.setFieldValue(kw_id, kw);
    	} else {
    		rec.setFieldValue(kw_id, keywords + ',' + kw);
    	}
    }
    
    function Update_Records() {
    
    	var cvsForm = document.getFormNamed("csv");
    	var recipesForm = document.getFormNamed("Recipes");
    	
    	// recipes
    	var category_id = 'fld-9828d73d445a4746a8dba4c0dac89716';
    	var keyword_id = 'fld-073ee12d29174c2eb64f7cf91f5bb383';
    	var pdf_id = 'fld-e870153bd0a647c9823ca998c213d1fd';
    
    	
    	// get csv field Ids
    	var csvPdf_id = cvsForm.getFieldNamed('PDF').getId();
    	var csvCategory_id = cvsForm.getFieldNamed('Category').getId();	var csvKeyword_id = cvsForm.getFieldNamed('Keyword').getId();	
    	// Loop over all csv entries
    	for (entry of cvsForm.getRecords()){
    		// get pdf file name
    		var pdfName = entry.getFieldValue(csvPdf_id);
    		// replace spaces with underscores
    		pdfName = pdfName.replace('/ /g', '_');
    		console.log("Processing: " + pdfName);
    		
    		// Loop over all recipes records
    		for (recipe of recipesForm.getRecords()){
    			// this assumes that there is only one PDF per recipe!
    			var filename = recipe.getFieldValue(pdf_id)[0].filename;
    			
    			if (pdfName == filename){
    				console.log("Found match: " + filename);
    				
    				// Update recipe record
    				addKeyword(recipe, keyword_id, entry.getFieldValue(csvKeyword_id));
    				
    				// Set category
    				recipe.setFieldValue(category_id, entry.getFieldValue(csvCategory_id))
    			
    				break;
    			}
    		
    		}
    		document.saveAllChanges();
    	
    	}
    	
    	return 'done';
    }
    
    Update_Records();

    The first row of my spreadsheet are the field labels: PDF, Category, Keyword. They are used to create the field names when creating a new form called csv upon importing your csv. The script is part of your Recipes form.

    This script assumes that the keywords are comma separated. Keywords are added to your record if new ones are found. The category field is overwritten!

    In order for the script to work, you need to update category_id, keyword_id, and pdf_id according to your recipes form.

    Hope this helps! Happy cooking!

    #42499
    Brecht DHeere
    Participant

    I think I found it. There maybe other (simpler) approaches but it works.

    ….
    async function myFunction() {
    try {
    await confirmExecution(‘Record verwijderen?’);
    } catch (errorText) {
    // user clicked ‘No’
    console.log(‘cancelled’);
    return;
    }
    // user clicked ‘yes’
    // continue with function
    // main code here

    var records = form.getRecords();
    var field_id = ‘fld-7a3920ab535745e8a78213b49a8b13d9’;
    var naam = record.getFieldValue(field_id);

    for (var index = 0, count = records.length; index < count; index++){
    var aRecord = records[index];
    var field_value = aRecord.getFieldValue(field_id);
    if (field_value == naam) {
    form.deleteRecord(aRecord);
    }
    }
    form.saveAllChanges();
    }
    myFunction();

    #42122

    In reply to: Script stopped working

    Sam Moffatt
    Participant

    Oh that’s an annoyance of the forum, it turns the backtick character into <code> and </code> respectively even though it’s inside a script context already. In Javascript the backticks are used to create template literals which allow you to embed variables inside the string. Not particularly necessary in this case but something I’m in the habit of doing.

    If you’re looking for the record that is problematic, you can use record.getUrl() for the record that fails the test and console.log it to find it.

    Here’s my copy of the script, I took out the template string so hopefully the forum doesn’t munge it and it’s been updated to point to different fields and field names (title and subtitle from the Books form in the document I use for the YouTube videos):

    var myForm = document.getFormNamed('Book');
    var records = myForm.getRecords();
    var search_term = Utils.copyTextFromClipboard();
    var result_count = 0;
    var results = [];
    var results_comments = [];
    var selected;
    
    function copy_comments( comments ) {
        Utils.copyTextToClipboard( comments );
    }
    
    function copy_result_multiple( comments ) {
    
        if ( comments == true ) {
            console.log( 'Index:' + results.indexOf( selected ) );
            console.log( results_comments[ results.indexOf( selected ) ] );
            copy_comments( results_comments[ results.indexOf( selected ) ] );
        } else {
            console.log( 'Cancelled' );
        }
    
    }
    
    function multiple_results( all_results ) {
    
        let prompter = Prompter.new();
        prompter.cancelButtonTitle = 'cancel';
        prompter.continueButtonTitle = 'Copy Comment';
        prompter.addParameter('Select Result ', 'selected', 'popup', all_results)
        .show('Multiple Results Found', copy_result_multiple );
    
    }
    
    function search_records( haystack , needle ) {
    	var title_id = 'fld-59b7b2dfa5c843afb969e74df4ad111c';
    	var subtitle_id = 'fld-b7d482d0db5d4554ad0948f19394f441';
        var rec;
    
        for (rec of haystack) {
        	var title = rec.getFieldValue( title_id );
        	
        	if (title && title.includes( needle ) ) {
                results.push( rec.getFieldValue( title_id ) );
                results_comments.push( rec.getFieldValue( subtitle_id ) );
                result_count++;
            } else {
            	if (!title) {
            		console.log("Empty field: " + rec.getUrl());
            	}
            }
    
        }
    
        if( result_count == 0 ){
            Utils.alertWithMessage("No Results", "No results were found for the search term: " + needle);
        }else if( result_count > 1 ){
            //multiple results
            multiple_results( results );
        }else{
            //single result
            copy_comments( results[0] );
        }
    
    }
    
    search_records( records , search_term );
    #42099

    In reply to: Script stopped working

    David Gold
    Participant

    I forgot to say I have separate Scripts for the iOS version and the Mac version of Tap Forms given some iOS limitations. The one above is the iOS version and below is the Mac version.

    var myForm = document.getFormNamed('Travel Database');
    var records = myForm.getRecords();
    var search_term = Utils.copyTextFromClipboard();
    var result_count = 0;
    var results = [];
    var results_comments = [];
    var selected;
    
    function copy_comments( comments ) {
        Utils.copyTextToClipboard( comments );
    }
    
    function copy_result_multiple( comments ) {
    
        if ( comments == true ) {
            console.log( 'Index:' + results.indexOf( selected ) );
            console.log( results_comments[ results.indexOf( selected ) ] );
            copy_comments( results_comments[ results.indexOf( selected ) ] );
        } else {
            console.log( 'Cancelled' );
        }
    
    }
    
    function multiple_results( all_results ) {
    
        let prompter = Prompter.new();
        prompter.cancelButtonTitle = 'cancel';
        prompter.continueButtonTitle = 'Copy Comment';
        prompter.addParameter('Select Result ', 'selected', 'popup', all_results)
        .show('Multiple Results Found', copy_result_multiple );
    
    }
    
    function search_records( haystack , needle ) {
    
        var location_id = 'fld-cf720b6ab4314f0bb5f47bc9bd61f0a9';
        var comment_id = 'fld-0096b55f012d4d4fb34a95f784951b55';
        var rec;
    
        for (rec of haystack) {
            if ( rec.getFieldValue( location_id ).includes( needle ) ) {
                results.push( rec.getFieldValue( location_id ) );
                results_comments.push( rec.getFieldValue( comment_id ) );
                result_count++;
            }
    
        }
    
        if( result_count == 0 ){
            console.log( 'No results found!' );
        }else if( result_count > 1 ){
            //multiple results
            multiple_results( results );
        }else{
            //single result
            copy_comments( results[0] );
        }
    
    }
    
    search_records( records , search_term );
    #42098
    David Gold
    Participant

    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 );
    #41846

    Topic: Getting favicons

    in forum Script Talk
    cf
    Participant

    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();
    
Viewing 15 results - 91 through 105 (of 146 total)
 
Apple, the Apple logo, iPad, iPhone, and iPod touch are trademarks of Apple Inc., registered in the U.S. and other countries. App Store is a service mark of Apple Inc.