Forum Discussion

WendyShoef's avatar
WendyShoef
Qrew Cadet
6 years ago

Javascript to find and replace and then update fields

I have a 2 part issue:

Using the [iol] method, I have a mod.js file and am calling it in my form.

Issue #1:

I am able to retrieve the data in some of the fields using 

var intro = _fid_15.value;

However, using var name = _fid_66.value; does not work.
Upon inspection, there isn't a field with id _fid_66 even though the data is being displayed. There is a label for="_fid_66" with a subsequent div following with the info. The div has an id of "tdf_63" (which I am assuming is the 63rd field on the form as the field next field is tdf_64 and the one after that is tdf_65).

Using getElementById().label does not work as there isn't an element with _fid_66.

We are in constant development right now and there is absolutely no guarantee that the fields will remain in the order they are in (in fact I can 100% assume not).

Question #1:
How do I get the value in this div into a variable without knowing the exact number on the form? 


Issue #2:

Ignoring issue #1, I have changed "var intro" and now want to put the new value back into the field. 

Using "var intro = _fid_15.value;" I then change the value. Then using "_fid_15.value = newValue" and saving the form (via saveAndKeepWorkingMenuOption.click();).

Updated value does not get saved.


Question #2:
What step am I missing to change and save the data?


21 Replies

  • Want to mention that _fid_15 is a field in the table associated with the form, _fid_66 is a related table field.
    Also fields that I will be changing will all be rich-text fields that will also need to be edited later by user.
  • Field gets changed in form when I run the script straight from load, but it fails when I run the same script in a function.
  • hhersch's avatar
    hhersch
    Qrew Captain
    Hi Wendy. What is the goal you are trying to achieve? What is the use case?
  • Overview:
    Template stored in a table with multiple languages and place holders for field values using rich text fields.
    Template copied with selected language.
    Run script to find and replace place holders with the values for the related case when user clicks a button.

    I have everything except pulling in the table field values for the elements without an html element ID of the table field id. 
    • hhersch's avatar
      hhersch
      Qrew Captain
      Thanks. Have you tried a Quick Base Automation to do this natively?
    • WendyShoef's avatar
      WendyShoef
      Qrew Cadet
      I can't see how an automation would work to find placeholders in the rich text field and replace them.

      I use automations and webhooks for much of  my workflow including copying the templates among other things.

      I'm just needing help with what I stated above... reading data from html elements that don't have a corresponding #_fid_.
    • hhersch's avatar
      hhersch
      Qrew Captain
      Hi Wendy. I don't know the details of the use-case, but Automations definitely can find and edit records. We also released _SearchAndReplace()_ as a formula function. Wherever possible, we would encourage using supported methodologies vs. image onload.
  • What is wrong with this formula?

    var text thisText = [Body];
    $thisText = SearchAndReplace( $thisText, "[PC]", [Case - PC Full Name]);
    $thisText
    • QuickBaseCoachD's avatar
      QuickBaseCoachD
      Qrew Captain
      Wendy, are  you saying that the formula has invalid syntax and won't save or does not work.  I have used upper and lower case in the formula variable names and they work fine.

       and btw, you can chain these formula variables so each one refers to the previous one, so that you can successively replace the fields.
    • WendyShoef's avatar
      WendyShoef
      Qrew Cadet
      I get an error.
      Inside the formula it says "Expecting text but found Boolean" 

      On save I get this error:

      There are extra characters beyond the end of the formula.


      var text thisText = [Body];
      $thisText = SearchAndReplace( $thisText, "[PC]", [Case - PC Full Name])

        $thisText

      I have tried it with and without a semi-colon after the ) on the line of $thisText. 

      FYI: I did just write a .js script file using the [iol] method to do all of the search and replace, format the dates, and then write the value back to the field. After a little bit of tinkering with your API, it all was pretty simple. I know you have answered a couple of my other questions (it's all related) and want to say thanks. 
    • QuickBaseCoachD's avatar
      QuickBaseCoachD
      Qrew Captain
      Sorry, I was being a bit stupid, but also you did not post your error message.

      var text thisText = [Body];

      var text PCReplace = SearchAndReplace( $thisText, "[PC]", [Case - PC Full Name]);

       $PCReplace