Thu 7 Feb 2013
How to add initial_filter for popup with a value taken from another field in SugarCRM
Posted by Marian Dobre under SugarCRM[3] Comments
In SugarCRM, a relate field can be added to EditView form using a popup from where you can select the desired entry.
In popup, you also have posibility to perform a search to filter the results from related module.
The nice part is that you can automatically send a filtering parameter using “initial_filter” from editviewdefs.php definition. So, for related field you have to add those lines:
'displayParams' => array(
'initial_filter' => "&account_name_advanced=John%20Doe",
)
That means that the popup it will open with “Account Name” prefilled with “John Doe” value. Be aware that target field is account_name but in “initial_value” the name is account_name_advanced.
This is great, but what if I want to send to pop-up a value of a field that has already been filled, not just a hardcoded value? Let suppose I have an invoice module and in EditView I have an “Acount Name” (related with Accounts) and “Payment responsible” (related with contacts). When I choose payment responsible I what to filter the results in popup by Account name. That makes sense.
To achieve this, in editviewdefs.php you must have:
array (
'name' => 'contact',
'displayParams' => array(
'initial_filter' => "&account_name_advanced=\" + document.getElementById(\"billing_account_name\").value + \"",
),
'label' => 'LBL_PAYMENT_RESPONSIBLE',
)
billing_account_name is actually the field name of “Account name”.
To understand how this works you can look at the HTML source and search the open_popup() function that is trigged by Contact button. Actually, the “initial_filter” value is a parameter in open_popup() javascript function.
So we can interfere in javascript and call a particular value from form(that we suppose is filled) using javascript function getElementById.
That’s all folks
March 28th, 2013 at 10:19 am
Thanks – that is a great technique that fills a gap in SugarCRM (and something that most advice on the SugarCRM forums seems to be just people guessing).
Once question: will the value need to be escaped, since it may contain quotes or back-slashes, ampersands etc. that would mess things up.
I would think it would need two levels of escaping:
1. To make it a URL-safe string, putting in entities for ampersands, and other special characters.
2. It probably does not need to be made JavaScript-safe, since the string is generated on-the-fly and passed into open_popup(), so the full string with the name in does not appear as code in the page.
Lastly, is there any way to trigger the search button automatically? This solution pre-populates the text field, but does not retrieve any data until you hit the “search” button on the pop-up.
Thanks for posting this
– Jason
March 28th, 2013 at 1:10 pm
[...] thanks to Marian Dobre for the final pieces to the puzzle in solving this little problem. [...]
October 25th, 2013 at 12:10 pm
Nice work.
It solved 70% of my problem. Rest of problem is upgrade safe way.
Looking forward to it.
Thanks…