popupdefs static/enforced criteria

Hi everyone,

Iā€™ve been trying to make the popup window which appears for linked fields to have set criteria ā€œfor example make it so end user can only pick records with the status of activeā€

I realised the whereClauses key is ignored if searchdefs is either not empty or is present.
so instead I have read into how the searchdefs section works,
tried using customCode key like


....
'status' => array (
    'name' => 'status',
    'customCode' => '<input type="hidden" name="product_status_c" value="Active"/>'
)
....

doesnā€™t seem as if this is taken into account so my last attempt I made the type an enum and added in a function which always returns

array ('active' => 'Active')

after this change I can see that the only option to pick from for status is active but it is not selected by default and it looks like it can be cleared.

what is the correct way to enforce certain criteria in a popup window?

After a deep dive into suitecrm I have now figured out how to implement this!

So I was mistaken in thinking that this is implemented within the popup def to the target module.
It is instead within the vardefs of the current record.

I believe the terminology for this feature is: initial filters

so in the field definition for the relate field in which you will need to set a displayParams array with a key value pair of initial_filter => ā€˜&field_name=value&another_field_name=another_valueā€™

I believe the popup window shares functionality with the advanced filters from a list view and so any field name you want to set filters on needs ā€˜_advancedā€™ appended on the end

so the full example

lets say the name of the field I wish to filter on is called status and the value I wish to have set is Active I would put into my edit



$dictionary[$module_name]['fields']['relate_field_name']['displayParams'] = [
    'initial_filter' => "&status_advanced=Active"
];

if the field is in the popup defs you will see the values already set in the filters and the initial list is filtered.
If you do not want the end user to be able to change the filter, you can remove the field from the popup and it will still work

Bonus knowledge :woohoo:

some modules stray from the norm like AOS_Quotes and AOS_Products_Quotes(line items)

The line item UI is not generated via smarty and instead is created by js and unfortunately the layout and the way the buttons behave is essentially hard coded and ignores custom displayParams within the vardefs

but all popups are opened by a call to the function ā€˜open_popupā€™ and the line items are no exception,

using line items as an example you can find the call to open_popup within <suitecrm_dir>/modules/AOS_Products_Quotes/line_items.js around line 281 within the openProductPopup function.

the fourth parameter is for initial_filters. when generated by smarty the calls to open_popup are bound to the parameters set in vardef but in this case you just change the empty string in this call with what you would have put into the var def

and so the call changes from

open_popup('AOS_Products', 800, 850, '', true, true, popupRequestData);

to


open_popup('AOS_Products', 800, 850, '&product_status_c_advanced=Active', true, true, popupRequestData);
1 Like

I wasnā€™t the first to discover this it seems link

Thanks, this is a nice piece of information to have.

Just a question - you say this is in the vardefs, but you probably meant it is supposed to go in the editviewdefs, right?

you are most likely right there. sorry I wrote out my tutorial with a bit too much excitement,

also today I realised if someone does not include the field in the popup defs and the user changes the filter parameters it will knock out the initial filter that was set.
to get around this add the field as an enum with only 1 option and have it selected by default.

if the field is not an enum normally, this does not matter.
Doing this trick will ensure that the value has to be selected as it is the only option.

such a setup would look a little like this



function p_return_active() {
   '<option value="Active" selected>Active</option>';
}

...
    'searchdefs' => array (
...
        'product_status_c' =>
        array (
            'name' => 'product_status_c',
            'type' => 'enum',
            'function' => array (
                'name' => 'p_return_active',
                'returns' => 'html',
                'arguments' => array ()
            )
        ),
...
1 Like

You seem to have got furthest with this so if you could give me a bit more details on how to achieve this.

  • I have Meetings and Products modules linked with One-to-Many.
  • When Iā€™m in Product detailed view I would like to attach Meeting by selecting them from Meetings subpanel (Create/Select Button)
  • I click on select and popup search appears. Now I would like it to set initial_filter to show only Meetings from Contacts that are related to that Product.

I have added a line to /custom/Extension/modules/AOS_Products/Ext/Layoutdefs/aos_products_meetings_1_AOS_Products.php

    array (
      'widget_class' => 'SubPanelTopSelectButton',
      'mode' => 'MultiSelect',
      'initial_filter_fields' => array('contacts_aos_products_1_name' => 'parent_name_advanced'),
    ),

This does populate the relate field name in the search but not the related to drop down. Any Idea how to go around this.

I have multiple values for the same field

 array (
          0 =>
          array (
            'name' =>'csr_c',
            'studio' =>  'visible',
            'label' =>  'LBL_CSR',
			  'displayParams' => 
      array (
	  
	   'initial_filter' => '&id_advanced='.$id,
         ),		
          ),
          1 => '',
        ),

Here $id contain multiple value which is coming in the array.
If $id has only 1 value then its working perfect but when it contain more then one value then it is not working.

I would try something like:
ā€˜initial_filterā€™ => '&id_advanced=' . $id1 . '&id_advanced=' . $id2 . '&id_advanced=' . $id3

Hey MadMart, Thanks for the answering my question but sorry to say it didnā€™t helped me.

loop through all your ids and set a id_advanced parameter for each

Hi @MadMart,

Can you please guide me on how we can add initial_filter for detail and list view?

I tried the below code in metadata/detailviewdefs.php and listviewdefs.php but it is not working, it is only working with editviewdef.php

ā€˜displayParamsā€™ =>
array (
ā€˜initial_filterā€™ => ā€˜&user_kind_c_advanced=Employeeā€™,
),

I Solved the issue of the initial filter getting kicked out with a ā€˜whereStatementā€™=> ā€œSQL Criteria hereā€,

custom\modules\<Parent Module>\metadata\popupdefs.php

     global $current_user;

     $this_Table = 'contacts_cstm';

     $popupMeta = array(
      // Custom Code
      'whereStatement'=> $this_Table.".wec_c = '1'",
      //

Thanks for getting me in the right direction with this post

1 Like

where did you add the function p_return_active()?