Dynamic fields in module

Hello everyone, I explain what I would like to do:
I have two modules, in Module A I have a list that varies and increases of this type

Name id

France 1
Italy 2
USA 1
Switzerland 2

Now Module B is a subpanel of another module that has to be set according to Module A:
If the third module is referred to id 1, we will have as header:

France USA

if the third module is referred to id 2, we will have as header:

USA Switzerland

Do you have any suggestions of how to implement this idea?
Thanks

@fab

The one of some variants is:

  • create dropdown this id - 1, 2, …
  • use logichook after_save for module A and change dropdown this id.
  • add dropdown id to module B.
  • use custom/<module_name>/views/view.detail.php for create header for the third module (if you wrote about header of DetailView).

Hi @p.konetskiy,
I don’t know at the beginning how many fields I need, how can I create a dropdown that in one case can contain one value, in another 2 values or 3, 4…?

@fab

I don’t know your end target. I see some variants:

  • use special module for it and write custom function. look at the function get_user_array in the file include/utils.php, it create dropdown users in filter of each modules.
  • modify dynamicly dropdown in backend when a record is saving.
  • modify dynamicly dropdown in frontend.

I do not exclude that there could be a simpler option.

Sorry for only responding now, but in the meantime my PC stopped working and is now in service and I’m with a temporary one.
I try to explain better my target:
In the Module A (which is a subpanel of another module) I create a list

Now, I switch to a third module that contains the subpanel Module B, if this third module refers to an id_factory 1, I will see this subpanel

If instead the third module refers to an id_factory 2, I will see this subpanel

I hope I have been clearer. How can I implement such a scenario?

Any suggestions? It is important

@fab

It’s more easier then i think.

You should write logichook after_save for the third module. Write the function with content like:

    if (!empty($bean->id_factory)){
        $firstBean = BeanFactory::getBean('The_module_A');
        $beanModuleAs = $firstBean->get_full_list('name', "The_ModuleA.id_factory = $bean->id_factory");
        foreach ($beanModuleAs as $beanModuleA) {
            $beanModuleB= new ModuleB();
            $beanModuleB->field1=$beanModuleA->field1;
            ...
            $beanModuleB->fieldN=$beanModuleA->fieldN;
            $idB=$beanModuleB->save(false);
            $bean->load_relationship('link_the_third_module_to_the_moduleB');
            $bean->link_the_third_module_to_the_moduleB->add($idB);
        }
    }

I hope that you could understand logic and will be able to adapt the code to your realy situation.

Sounds very simple, I’ll try it and let you know

I tried the code you proposed, it is ok if I have already defined in Module B the fields via Studio, so I should have already created the fields field1… fieldN

$beanModuleB->field1=$beanModuleA->field1;
...
$beanModuleB->fieldN=$beanModuleA->fieldN;

and in this way I’m going to fill the existing fields with those of Module A.
So I should first create the fields field1… fieldN, but I don’t know how to do it in code, I couldn’t find anything on the net.
But I only know those fields after creating them in Module A. That’s why I think they have to be defined in Vardefs first, so I tried something like this:

$fieldProp['source'] = 'non-db';
						$fieldProp['type'] = 'text';
						$fieldProp['label'] = 'LBL_' . strtoupper($beanModuleA->name);
						$fieldProp['name'] = $beanModuleA->name;
						$dictionary['module_b']['fields'][$beanModuleA->name] = $fieldProp;

but it doesn’t work is there any way to do it? Also I think I should change the subpanel layout too, but again I don’t know how to get there from code

@fab

The fields (field1… fieldN) are example names only. You should use the fields name of your moduleA and moduleB. I wrote way only.

My logic was:

  1. Take the data from field id_factory of the third module.
  2. Find all record of moduleA whiche have the field id_factory equal the value of the third module.
  3. Create records in ModuleB with correct content and create relationship betwin ModuleB and the third module.

yes, I get that field1…it was an example, I just picked it up. it’s really your point 3 that I’m missing.
How do I create from code those fields that I only know about when I create them in Module A?
Something like this:
$beanModuleB= new ModuleB();
$beanModuleB->addfield(field1);

@fab

Sorry we don’t understand each other. I don’t suggest to create field dynamically. I think that you take correct records from ModuleA and copy the values of some fields to ModuleB. Main task is find required entries in ModuleA. You can create the fieldsin Studio.

That’s the point, I have to create fields dynamically because I don’t know before what fields I need, I know them only after inserting them in Module A, that’s why I wanted a way to create, via code, the fields. If you have a better idea to do what I need I’m listening.
Another way would be to create a “Type” field in Module B, which contains the values of the “Name” field in Module B:

I enter in Module A
USA
Italy
France

and in Module B in the Type field:
USA;Italy;France

but then I don’t know how to explode these values so that in the subpanel (Module B) has as header:

USA Italy France

I hope to have explained well

Translated with www.DeepL.com/Translator (free version)

@fab

Did you meean that you want to change the title - ModuleB replace to USA Italy France.?

As per the screenshots above, what I want is to enter the data in Module A and based on that create the fields in Module B. The ideas I’ve come up with are:

  1. create dynamic fields in Module B via code.
  2. create a field in Module B that contains the values of Module A:
    if in Module A I have
    Name id_factory
    US 1
    Italy 1
    France 2

In the Type field of Module B I will have:
USA;Italy (if the detail is referred to id_factory 1)
France (if the detail is referred to id_catory 2)

In both cases I don’t know how to do it.
If you have other suggestions…

@fab
Ooh, I understand you now. Create dynamically fields is very heavy for the system and not for SuiteCRM only.

Sorry I think that you change basically logic. You can use 2 fields:

  1. for name of countries which will be write as “USA;Italy”
  2. for code of countries (I don’t know logic of the code) which will be write as “A112 - 232”, “B221 - E34”, “A123 - 111”

You will have table for ModuleB:

NAME                  Code         N set       N no set           
USA;Italy           A112 - 232     10          20
USA;Italy           B221 - E34     100         40
USA;Italy           A123 - 111     50          90

Okay, so I’m dropping the dynamic fields.
This system is fine:

NAME                  Code         N set       N no set           
USA;Italy           A112 - 232     10          20
USA;Italy           B221 - E34     100         40
USA;Italy           A123 - 111     50          90

the problem is that in the subpanel, the fields need to be split up:


so that in each column can be inserted the required data

So the Name field of Module B must be split, it’s ok to insert the content in the Name field, but then, on screen they must appear on different columns, so that the data can be inserted, but formally only one field is used.
I don’t know if I can explain myself, I know it’s a bit convoluted, but I can’t do otherwise

@fab

Sorry I can’t help you because i think that you should change vision to the task. If data doesn’t have logical structure then you should change the rules to work with them. It’s my opinion only. I don’t know your business and I can’t suggest a variant.

Thanks anyway, if someone can give me a solution or alternative ideas are listening … if I find a solution I will post it

Excuse me @p.konetskiy, what if instead of creating dynamic fields, I create 3 fixed fields in Module B via Studio like Field1, Field2, Field3 and then change the subpanel header depending on id_factory of Module A?
That is:
Module A
Name id_factory
US 1
Italy 1

Module B
Field1 Field2

then I change via code the header of Module B which becomes:
Usa(Field1) Italy(Field2)

is it possible to do something like this? If yes, how?