find and replace script

Viewing 2 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');
    • This reply was modified 3 weeks, 1 day ago by Brendan.
    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.

Viewing 2 reply threads

You must be logged in to reply to this topic.