Script error – what it means?

Viewing 9 reply threads
  • Author
    Posts
  • May 2, 2020 at 10:42 AM #40421

    Victor Warner
    Participant

    I 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.

    May 2, 2020 at 11:06 AM #40422

    Victor Warner
    Participant

    The above post needs clarification: the code only works on a text type field and not in a number type field. I would like to know how the script should be amended to work on a number type field

    May 2, 2020 at 10:47 PM #40430

    Sam Moffatt
    Participant

    The problem is that replace is a method of a string which in Javascript is an object. A number is a different type of object and doesn’t have a replace method because it doesn’t make sense.

    To help you along, are you expecting to replace a substring of numbers to a new value in your number field? To do so, you’d have to convert the number to a string (easy way is to use an empty string and add your variable to it, e.g. '' + myvar) and then replace would work fine because it’ll coerce the number into a string form.

    May 4, 2020 at 4:09 AM #40444

    Victor Warner
    Participant

    Sam,

    Thank you for the response.

    …are you expecting to replace a substring of numbers to a new value in your number field?

    Yes that is right.

    I can see there is a string function in JavaScript but because I do not know JavaScript, could you amend the code so that it is possible to search for one or more numbers? I would be very grateful.

    May 4, 2020 at 10:31 AM #40448

    Sam Moffatt
    Participant

    I think this might work for you a little more consistently.

    Change this line:

    let value = rec.getFieldValue(time_charged_for_id);
    

    to be:

    let value = '' + rec.getFieldValue(time_charged_for_id);
    

    That should ensure that value is always a string and if the field value is empty, it should still be falsey for the next check (an empty string doesn’t evaluate to true).

    May 11, 2020 at 6:04 AM #40540

    Victor Warner
    Participant

    Sam,

    That you very much for the new piece of code. The code now works as intended.

    Thank you again.

    Victor

    May 13, 2020 at 6:56 AM #40578

    Victor Warner
    Participant

    On running this script it works on all the records in a database.

    Running the script on a filtered set of records (through Find (Command + F) or an Advanced Search) does not limit its operation to the filtered set of records.

    Is it possible to make the script work only on a filtered set of records?

    May 13, 2020 at 8:56 AM #40581

    Sam Moffatt
    Participant

    There is a search.getRecords() to get records from the current search. It has a few methods, checkout the JavaScript API page for more details (towards the bottom of the page).

    May 25, 2020 at 8:33 AM #40684

    Victor Warner
    Participant

    Sam,

    Thank for the further reply,

    I eventually worked out how to use

    search.getREcords()

    , by replacing:

    for (let rec of form.getRecords()){

    with

    for (let rec of search.getRecords()){

    Just one follow-up question: Does the it only work with an advanced search and not an ordinary search (Command + F)? I have tried with an ordinary search but the script does not operate.

    May 25, 2020 at 12:37 PM #40690

    Brendan
    Keymaster

    Thats’ right. The search references the currently selected Saved Search object. It has no reference to a general search.

Viewing 9 reply threads

You must be logged in to reply to this topic.