function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Néstor Velázquez SánchezNéstor Velázquez Sánchez 

How receive a POST in custom controller?

I need your help with a new doubt with a custom controller.
In my VisualForce page I have to SelectList, these SelectList could change their name periodically (they are dynamic) in some times I have only one, but sometime I have 10 or more.
Then my SelectList have a name like:
<select name="{!var.Name_{!var.Id}" id="{!var.Name_{!var.Id}">
  <apex:repeat var="opt" value="options">
   <option value="{!opt.Id}>{!opt.Name}</option>
  </apex:repeat>
  </select>


This SelectList could be repeat N times, the form looks like the attachment image.

Multiple form inputs and hidden inputs, dynamic

How I can recieve this values in the custom controller to insert in my object Custom__c.

It's important considering that the code

ApexPages.currentPage().getParameters().get('input')

 is useless there, I have a lot input fields with distinct names,and not all the time will be the same name.


Thanks.

 

 

Glyn Anderson 3Glyn Anderson 3
Néstor,

This is a big question; but I'll start by suggesting that you use the Visualforce components that implement select/option picklists.  Hopefully, that will get you most of the way there.

<pre>
<apex:selectList value="{!var.Name_{!var.Id}}" id="{!var.Name_{!var.Id}}" size="1">
    <apex:repeat var="opt" value="{!options}">
        <apex:selectOption itemValue="{!opt.Id}" itemLabel="{!opt.Name}"/>
    </apex:repeat>
</apex:selectList>

<!-- You could also to do this, with changes to your controller. -->
<apex:selectList value="{!var.Name_{!var.Id}}" id="{!var.Name_{!var.Id}}" size="1">
    <apex:selectOptions value="{!options}"/>
</apex:selectList>
</pre>
Néstor Velázquez SánchezNéstor Velázquez Sánchez
Hi, Mr. Glyn, thanks for your answer, I have a basic and simple doubt: What is the place in the data fields from form are received to processed individually?, I don't know something similar like an array $_POST in PHP language. where dataForm = $_POST['theForm']; and each data could be process individually.
Basically I don't understanding the concept FORM > DESTINY in salesforce.
Néstor Velázquez SánchezNéstor Velázquez Sánchez
I try use that, but . . . 



Invalid selectOptions found. Use SelectOption type in Apex. 
 

 

Glyn Anderson 3Glyn Anderson 3
If you use the first version (lines 1-5 of my post), then you can have your own list of objects with 'Id' and 'Name' members.  This should be the easier way to adapt to the code you have already.  It will be as simple as this:  (My example assumes you have a custom object or custom setting called Option__c.)

<pre>
public List<Option__c> options  { get; private set; }
{
    options = [SELECT Id, Name from Option__c];
}
</pre>

The second method (lines 8-10) of my post) will require you to use the Apex class, SelectOption, in your controller.  It's a little more complicated, but not much:

<pre>
public List<SelectOption> options  { get; private set; }
{
    options = new List<SelectOption>();
    for ( Option__c option : [SELECT Id, Name from Option__c] )
    {
        options.add( new SelectOption( option.Id, option.Name ) );
    }
}
</pre>
Néstor Velázquez SánchezNéstor Velázquez Sánchez

Hi, I try to use the code that you provide me, but it's not useful for my proposities, the options for this list must will be dynamic, I am use an IF statement for "draw" the option according the case in code lines previous.   

If I used  this code: 
 

<apex:selectList value="{!asset.Actividad__c}" id="Activo__c" size="1">
       <apex:selectOption itemLabel="-- SELECT ONE  --" itemValue=""></apex:selectOption>
       <apex:selectOptions value="{!options}"></apex:selectOptions>
</apex:selectList>

In this case the options is an optinos sting like this: 
 
[System.SelectOption[value="ANiD", label="Pablo - Ballet", disabled="false"], System.SelectOption[value="ANiD", label="Pablo - Canto", disabled="false"], System.SelectOption[value="ANiD", label="Luis El pato - Canto", disabled="false"], System.SelectOption[value="ANiD", label="Hugo El pato - Canto", disabled="false"], System.SelectOption[value="ANiD", label="Paco El pato - Canto", disabled="false"], System.SelectOption[value="ANiD", label="BAILARIN CANTANTE - Capacidad de expresión", disabled="false"], System.SelectOption[value="ANiD", label="Sandra aparicio - Canto", disabled="false"], System.SelectOption[value="ANiD", label="CANTANTE BAILARIN - Canto", disabled="false"], System.SelectOption[value="ANiD", label="BAILARIN SALTARIN - Capacidad de expresión", disabled="false"]]


And i Can't compare with other elements for draw the only one option that I need, thats useless for me. EXAMPLE I NEED ONLY THIS ELEMENT BUT THE SECOND PART: BAILARIN SALTARIN - Capacidad de expresión, I also need other hidden fields that will be compared for make the update and insert into custom objects, but I can't get the elements using POST GET or whatever.

Néstor Velázquez SánchezNéstor Velázquez Sánchez
Is worst yet, the button "submit" not had sending nothing, the LOG is EMPTY, even the elements System.debug('FOO'); do not was displayed in LOG screen. 
:'( 
Néstor Velázquez SánchezNéstor Velázquez Sánchez

I answer to myself . . .  

 

All the variables must be  declared in the upper part of the class. . . 

 

public  with sharing class bookingController
{
    public String Activo {get; set;}
    public String Id {get; set;}
    
    public String jsonSalida { get; set; }
    
    public Date fInicio {get; set;}
    public Date fFin	{get; set;}
    
    public List<OpportunityLineItem> opportunityProducts  										{get; set;}
    /* For the list */
    public List<SelectOption> options  { get;  set; }

.
.
.

Once upon that the variables are declareted it's necesary populate this and create a new instances about this in the constructor of class: 
 
public bookingDisponibleController() {
            opportunityProducts = [SELECT Id, Name FROM OpportunityLineItem WHERE OpportunityId =: idOpportunity];

. 
.
.


And the list for the options 

 

for(Object__c r: theMotherList)
{
       options.add(new SelectOption (r.myField__c, r.otherField__c));
}
 

Finally I need create a WRAPPER CLASS for the select with this element i can use the list elements. like a  Map<idELement, ListElemnts>
 

public class Sustitute {
        public Object__c relacion 	{get; set;}
        public String substitute						{get; set;}
        
        public Sustitute()
        {
            relation = new Object__c();
        }
        public Sustitute(Object__c rpa) {
            relation = rpa;
        }
    }
In the visual force I was created the SelectList with options, then in the controller 
 
public PageReference saveNewBooking()
    {

    /* USE THE rpa element for the select items. */
            List<Sustitute> tmp = mapAssetsProduct.get( key );
            
            
            
            for( Sustitute rpa: tmp )
            {

            }
}

Thanks a lot for read and try to help.