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
Zac RosenbergZac Rosenberg 

Apex:CommandButton Passing Value from VF to Controller

I'm trying to create a visual force page with a table and a "request" button that will take the selected row and append it as a task.

Here's a photo of the table:
User-added image

When a user hits request, I want to create a task that includes the deal name and deal owner.

Here is the VF page:
<apex:page controller="A7_OppsCntrl" standardStylesheets="false" sidebar="false">   

    <apex:form>
        <table id= 'a7Table' class='display'>
            <thead>
                <tr>
                    <th>Deal Name</th>
                    <th>Deal Owner</th>
                    <th>Request</th>
                </tr>
            </thead>
           <apex:repeat value="{!A7Opps}" var="row">
               <tr>
                   <apex:repeat value="{!A7Opps[row]}" var="cell">
                       <td> {!cell} </td>
                   </apex:repeat>
                   <td>
                       <apex:commandButton action="{!send2Admin}" value="Request" title="Send Request to Admin" oncomplete="" reRender="block">
                           <apex:param value="{!A7Opps[row]}" assignTo="{!paramValue}"/>
                       </apex:commandButton>
                   </td>
               </tr>
           </apex:repeat>
         </table>
    </apex:form>
</apex:page>

Here is the controller:
 
Global With Sharing class A7_OppsCntrl {

    static string paramValue;

    public static void send2Admin(){

        user adminId = [SELECT id from user where name like 'Sir Admin' limit 1];
        string userId = toolbox.grabUserId(); //Grabs context user ID
        user userName = [SELECT name from user where id = :userId];

        task leadReq = new task();
        leadReq.OwnerId = adminId.id;
        leadReq.Subject = 'New Request';
        leadReq.Description = userName.name+' is requesting '+paramValue;
        insert(leadReq);
    }

The task is successfully inserted, but the paramValue comes up as null.

What am I doing incorrectly (probably a lot ;)? How can I get the row data to be appended to the description of the task?

I've tried following https://developer.salesforce.com/forums/ForumsMain?id=906F000000095uXIAQ

Didn't help.

Thanks!
Best Answer chosen by Zac Rosenberg
Zac RosenbergZac Rosenberg
was answered here:
http://salesforce.stackexchange.com/questions/73222/apexcommandbutton-sending-data-to-controller/73241?noredirect=1#comment95784_73241

Seems to be that I had to add a name to apex:param. So now it looks like:
 
<apex:param name="whatever" value="{!a7Opps[row]}" assignTo="{!paramValue}" />

Why did this fix it???

All Answers

Abhi_TripathiAbhi_Tripathi
Make your param value in Controller Public string paramValue { get; set; }
and pass the var in the param tag not the value like below.
 
<apex:param value="{!cell}" assignTo="{!paramValue}"/>
Zac RosenbergZac Rosenberg
If I change the paramValue in the controller to 
 
public string paramValue{get;set;}

the method "send2Admin()" can't access it.

I get "paramValue does not exist". I also tried this.paramValue, which also doesn't work.
Pankaj_GanwaniPankaj_Ganwani
Hi,

I think you will have to make the paramValue as instance variable instead of static variable. Can we make it instance public variable with getter and setter properties and make the method as public void send2Admin()?
Zac RosenbergZac Rosenberg
So doing this allows for the value to be referenced - good. But the value is still coming back as 'null' in the task. 

Here is the updated code:
public String paramValue {get; set;}
    //static string paramValue;
    public void send2Sherry(){
  		
        user sherryId = [SELECT id from user where name like 'Zac Rosenberg' limit 1];
        string userId = toolbox.grabUserId();
        user userName = [SELECT name from user where id = :userId];
        
        task leadReq = new task();
        leadReq.OwnerId = sherryId.id;
        leadReq.Subject = 'New Lead Request';
        leadReq.Description = userName.name+' is requesting '+ this.paramValue;
        insert(leadReq);
        
    }
Not sure if it would help to see what is in the A7_Opps mapping:
 
public static Map<string, list<string>> getA7Opps(){

        List<List<String>> parsedCSV = new List<List<String>>();
        Map<string, list<string>> A7Opps = new Map<string, list<string>>();
        Map<string, list<string>> userA7Opps = new Map<string, list<string>>();

        String fileString = getCSVFromDocs('A7Opps');
        parsedCSV = toolbox.csv2List(fileString, '\\|');
        for (List<String> row : parsedCSV){
          A7Opps.put(row[0], row);
        }

        list<string> targetIDs = new list<string>();
        string userId = toolbox.grabUserId(); 
        targetIDs.add(userId);
        list<deal__c> dealOriginator = [SELECT name, Deal_number__c, owner.id, owner.name 
                                        FROM deal__c 
                                        WHERE owner.id in :targetIDs AND Deal_number__c in :A7Opps.keySet()];

        for(deal__c deal:dealOriginator){
            A7Opps.get(deal.Deal_Number__c).add(deal.name);
            A7Opps.get(deal.Deal_Number__c).add(deal.owner.name);
            userA7Opps.put(deal.Deal_Number__c, A7Opps.get(deal.Deal_Number__c));   
        }
        return userA7Opps;
    }

Maybe the problem is in the value being !A7_Opps[row]? That would reference a list of string...
 
Zac RosenbergZac Rosenberg
was answered here:
http://salesforce.stackexchange.com/questions/73222/apexcommandbutton-sending-data-to-controller/73241?noredirect=1#comment95784_73241

Seems to be that I had to add a name to apex:param. So now it looks like:
 
<apex:param name="whatever" value="{!a7Opps[row]}" assignTo="{!paramValue}" />

Why did this fix it???
This was selected as the best answer