Hi to all,
I am trying to implement a simple "Lead Source/Interest" capture. When a user clicks a button on my landing page, they are redirected to a registration page with a URL parameter, for example: mywebsite.com/registro?servicio=AgentesIA
The Goal: I want the "Subject" (Asunto) field of the Odoo Website Form to automatically fill with the value "AgentesIA" upon page load.
The Problem: I have tried several JavaScript approaches (using DOMContentLoaded, setInterval to wait for the element, and searching by name, id, and class), but none seem to work reliably.
The field ID often appears as #Field or a generic string that seems to change.
Even when the script finds the input and changes the value, the data is sometimes not captured when the form is submitted unless there is a manual "input" or "blur" event.
The Odoo form seems to be protected or rendered in a way that standard document.querySelector sometimes misses it during the initial load.
Current Script Snippet:
(function() {
const params = new URLSearchParams(window.location.search);
let service = params.get('servicio');
if (service) {
let attempts = 0;
const monitor = setInterval(function() {
let field = document.querySelector("input[name='name']") ||
document.querySelector("input[id*='Field']");
if (field) {
field.value = service;
field.dispatchEvent(new Event('input', { bubbles: true }));
field.dispatchEvent(new Event('change', { bubbles: true }));
clearInterval(monitor);
}
if (attempts > 100) clearInterval(monitor);
attempts++;
}, 100);
}
})();
Question: Is there a "standard" or more "Odoo-friendly" way to achieve this? Should I be using a specific Odoo JS Class or a different selector to ensure the form controller recognizes the injected value?
Any help or pointing to the right documentation would be greatly appreciated!
Thanks in advance!