find and replace script

Viewing 9 reply threads
  • Author
    Posts
  • March 9, 2020 at 10:44 AM #39807

    matt coxon
    Participant

    Hi Does anyone have a script that will do find and replace for text fields. I can use the advanced find and replace, but I would need to do it daily as it’s an import. I want to do it in tapforms and not manipulate the csv file before hand.

    Thanks very much

    Matt

    March 10, 2020 at 12:39 AM #39818

    Brendan
    Keymaster

    The Basic Loop Snippet will get you part of the way. But here’s a more comprehensive example:

    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');
    March 10, 2020 at 1:05 PM #39822

    Sam Moffatt
    Participant

    If you’re doing an import flow you might want to add a field with a flag on it to control if you already did the search/replace on the record already. Depending on what your search/replace looks for, running it more than once might be problematic for you.

    Another trick you might be interested in is creating a saved search using “date created” to specify when you import the records to ensure that you only select and modify the records that were recently created. A slight change to Brendan’s script is to change:

    var records = form.getRecords();
    

    To something like:

    var records = search.getRecords();
    

    You’ll need to be in the search for the search object to exist but then it’ll only apply the script to the currently searched records.

    November 3, 2021 at 4:32 PM #45633

    Miguel Centeno
    Participant

    Very useful script but how can I find and replace a specific word in a paragraph?

    November 3, 2021 at 11:23 PM #45634

    Brendan
    Keymaster

    You’d just need to use the JavaScript replaceAll() function for that.

    str = str.replaceAll('Blue', 'Red');

    November 4, 2021 at 3:44 AM #45639

    Miguel Centeno
    Participant

    Hi Brendan, I’m afraid my programming skills are too basic, can you please help me with the complete script for that?

    November 6, 2021 at 1:12 AM #45644

    Brendan
    Keymaster

    Can you post what script you have so far? Then I can see what you need to do to accomplish what you want.

    November 6, 2021 at 4:50 AM #45648

    Miguel Centeno
    Participant

    I was looking to the above script you posted (#post-39818) but I have no idea where str = str.replaceAll(‘Blue’, ‘Red’); is supposed to be placed.

    My goal is to have a Run Script button to change part of a URL, from time to time.

    I’m doing some genealogical research using a particular Chrome extension viewer, which is more user friendly than the original viewer of the archive site. The downside is the extension changes the original URL. So, in the end of the day, after pasting a lot of URLs to TF, I wish to replace a part of those URLs, so I can have the original ones. Basically, to find “viewer” and replace it with “ViewerForm.aspx”:

    https://digitarq.arquivos.pt/viewer?id=4805309&FileID=PT-ADLSB-PRQ-PFAR02-004-M4_m0027.jpg
    https://digitarq.arquivos.pt/ViewerForm.aspx?id=4805309&FileID=PT-ADLSB-PRQ-PFAR02-004-M4_m0027.jpg

    Attachments:
    You must be logged in to view attached files.
    November 7, 2021 at 4:55 PM #45658

    Brendan
    Keymaster

    Ok, so then it would be like this:

    var field_id = 'fld-807fc883160d41ea802f26b7b0bd5af5';
    
    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 website_url = rec.getFieldValue(field_id);
    		if (website_url != undefined) {
    		   website_url = website_url.replaceAll(find_text, replace_with);
    		   rec.setFieldValue(field_id, website_url);
    		}
    	}
    
    	document.saveAllChanges();
    	
    	return;
    }
    
    findAndReplace('viewer', 'ViewerForm.aspx');

    You have to use your own value for field_id.

    November 8, 2021 at 3:51 PM #45665

    Miguel Centeno
    Participant

    That’s it. Many thanks Brendan!

Viewing 9 reply threads

You must be logged in to reply to this topic.