0

Using Ninox ChatGPT to create a search

Hi,

 

My first shot at creating a button to search a field (RTF) to find and mark results.  I have a field SEARCH for the search terms and the RTF field is NOTES.  The ; on the third line is coming up red underlined and ChatGPT simply can't fix it.  Any ideas?

let searchTerm := 'SEARCH';  
if searchTerm != null and searchTerm != "" then
  let highlightedText := replace(notes, searchTerm, "<mark>" + searchTerm + "</mark>");
  'NOTES' := highlightedText;
end;

14 replies

null
    • Ninox partner
    • RoSoft_Steven.1
    • 1 mth ago
    • Reported - view

    You should make the variable 'notes'  (no capitals) first so the code can recognize the variabele...

    let notes := NOTES;

    The field NOTES (with capitals) can then be changed...

    • Alan_Cooke
    • 1 mth ago
    • Reported - view

    I am missing something here.  Can you advise.

    let searchTerm := SEARCH;  
    let notes := notes;
    if searchTerm != null and searchTerm != "" then
      let highlightedText := replace(notes, searchTerm, "<mark>" + searchTerm + "</mark>");
      notes := highlightedText;
    end;

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       

      let searchTerm := SEARCH;
      let originalNotes := notes;
      
      if searchTerm != null and searchTerm != "" then
        let highlightedText := replace(originalNotes, searchTerm, "<mark>" + searchTerm + "</mark>");
      else
        originalNotes : highligheText;
      end;
      

      The error in your script seems to be related to how you're reassigning the notes variable. In Ninox, reassigning a variable like notes (which holds data from the database) can cause issues, especially since it already contains initial data.

      according to ChartGPT

    • John_Halls
    • 1 mth ago
    • Reported - view

    Hi  The error you are getting is to do with the fact that replace() does not work with rich text. You need to wrap the RTF field with raw(). Also, there is no need to assign any variables, so your code can become

    if SEARCH != null then
       NOTES := replace(raw(NOTES), SEARCH, "<mark>" + SEARCH + "</mark>")
    end
    

    Even the if statement could go if you wish

    Regards John

      • John_Halls
      • 1 mth ago
      • Reported - view

      In my small test of the above solution my text became more and more yellow as I performed successive searches. Can I suggest a way to undo any previous searches prior to a new search

      NOTES := replace(raw(NOTES), "<mark>", "");
      NOTES := replace(raw(NOTES), "</mark>", "");
      NOTES := replace(raw(NOTES), SEARCH, "<mark>" + SEARCH + "</mark>")
      

      Regards John

      • Alan_Cooke
      • 1 mth ago
      • Reported - view

       if SEARCH != null then
          notes := replace(raw(notes), SEARCH, "<mark>" + SEARCH + "</mark>")
      end

      Did not work for me at all.  Nothing was hilited.  The script is under the button and notes is a RTF field.  What am I missing?

      • szormpas
      • 1 mth ago
      • Reported - view

        

      Hi, I took a look at your code and it seems that it only works if you convert the RTF field into an HTML type (see the attached screenshot).

      I'm not sure if this is related to the initial issue with the RTF field not rendering HTML, as Ninox mentioned.

      HTML rendering in rich text field

      If you've been using HTML in the rich text field, you may notice changes in how it displays. We're working on bringing full HTML rendering capabilities in an upcoming 3.13.X patch. Until then, your HTML content might not appear as expected. We value your flexibility during this transition and are committed to supporting your workflow needs. Stay tuned for updates.

      • John_Halls
      • 1 mth ago
      • Reported - view

       Good spot, my test field was indeed of Field type HTML

      • Alan_Cooke
      • 1 mth ago
      • Reported - view

       Brilliant - works perfectly.  Now if I may be so bold could we explore improving this with another two options.
      1.  Reset to no high lighting.
      2.  A count of occurrences found (let's say we have a formula field named 'Count'.

      This could be a very useful tool.  I copy and paste Chat logs of job WhatsApp groups for instance.

      • John_Halls
      • 1 mth ago
      • Reported - view

       Reset to no highlights works with my code below plus an empty search field

      NOTES := replace(raw(NOTES), "<mark>", "");
      NOTES := replace(raw(NOTES), "</mark>", "");
      NOTES := replace(raw(NOTES), SEARCH, "<mark>" + SEARCH + "</mark>")
      
      • Alan_Cooke
      • 1 mth ago
      • Reported - view

       Cheers John!

      • John_Halls
      • 1 mth ago
      • Reported - view

       There's some meaty discussions on counting occurrences of text in a string on the forum but this seems to work, finding the length of the field and then replacing the search text with "" and seeing how much shorter the text is. Divide this by the length of the search text gives a count of occurrences

      let a := length(raw(NOTES));
      let b := length(replace(raw(NOTES), Search, ""));
      let c := a - b;
      let d := c / length(Search);
      d
      

      Regards John

      • Alan_Cooke
      • 1 mth ago
      • Reported - view

       Sorry to bother John.  I notice that the search is case specific in other words 'alan' won't find Alan or ALAN.  I tried using upper which worked only because it changed the entire NOTES field to uppercase.  Not something I want to do.  Is there a way to modify the script that it will take account of the 'case' whether proper, upper or neither?

    • John_Halls
    • 1 mth ago
    • Reported - view

    Not a problem  It might be tricky to catch every use-case but try this

    NOTES := replace(raw(NOTES), SEARCH, "<mark>" + SEARCH + "</mark>")
    NOTES := replace(raw(NOTES), lower(SEARCH), "<mark>" + lower(SEARCH) + "</mark>")
    NOTES := replace(raw(NOTES), upper(SEARCH), "<mark>" + upper(SEARCH) + "</mark>")
    NOTES := replace(raw(NOTES), capitalize(SEARCH), "<mark>" + capitalize(SEARCH) + "</mark>")
    

Content aside

  • Status Answered
  • 1 mth agoLast active
  • 14Replies
  • 108Views
  • 5 Following