0

I'm trying to pass an inputtext field value within my pageblocktable to my Apex controller so that I can update the record.

My VF:

apex:pageBlock title="Z Table">
<apex:pageBlockTable value="{!scar}" var="z" id="z">

            <apex:column headerValue="Q"> 
            <apex:inputText value="{!z.q}"/>

             </apex:column>
            <apex:column headerValue="Update">
        <apex:commandLink action="{!update}" rerender="z">
            Update
       <apex:param assignTo="{!conid}" value="{!z.id}" name="assignvalue"/>  
          </apex:commandLink>
             </apex:column>
            </apex:pageblocktable>        
</apex:pageBlock>  

Controller:

public String z {get; set;}
public String conid  {get; set;}


public void update() {



try{
             sd = [Select id, q__c from SD__c];

         for(SD__c s : sd)
        {

                if (s.id == conid) {
                   //get the z from VF page
                    s.q__c = z;
                }                
}

             update sd;

    } catch(DmlException e) {
     System.debug('The following exception has occurred: ' + e.getMessage());
    } 
  }

But it seems like the input text that I've entered on my VF page does not pass to the controller. It will be null if I were to call the update method.

  • I'm not sure what exactly your code is, it seems you are missing something here. But my first impression is, you should not Fetch record in update call, instead you should use for loop on your List of record to which the pageBlockTable has been binded to i.e. {!scar} and so your for loop should be ... for(SD__c s : scar).. – VarunC Feb 7 '15 at 19:58
  • hi @varunC The pageblocktable that its binded is used for display purposes. I would like to update based on my inputtext value and the record id that the person has chosen. How do I pass my inputtext value to apex controller? – agentx Feb 7 '15 at 20:15
  • The apex variable binding to the table means any change u gonna do in that variable is auto passed into corresponding apex class. – VarunC Feb 7 '15 at 21:16
  • Hi @VarunC, Thank you, But how do i do that? Which variable are u refering to? – agentx Feb 8 '15 at 9:53
  • <apex:inputText value="{!z.q}"/> ... this variable is and should be referenced through your pageBlockTable's "var" property attribute. I think you are too confused because you have used same variable everywhere in your code. "z" is a Property in your APEX class as well (i.e. => public String z {get; set;} )...and "z" is a pageBlockTable "ID" as well, and "z" is your pageBlock binding list's instance variable for each row data as well ... – VarunC Feb 8 '15 at 10:21
1

Lets try to understand this in this manner:

In simpler terms, Apex and Visualforce communication is all nothing but just the binding of variables. You don't need to do anything yourself. Just bind the variables and the values propagates from Visualforce to Apex.

Consider following example of Mass Edit:

APEX class:

public with sharing class myClass {
    public myClass() {
        dataRecords = [select Id,Name from myCustomObject__c];
    }
    //Getter & Setter here are important. This means you can GET records from this variable and ANY changes you do in VF page will get SET changed into this variable itself.
    public list<myCustomObject__c> dataRecords { get;set; } 

    public void saveData() {
        // this code below does not make anything for editing but you can certainly check if your changed values in input controls are carried onto this APEX call or not
        integer i=0;
        for (myCustomObject__c c : dataRecords) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Row ['+i+']: ID = '+c.Id + ' | Name = '+c.Name));
            i++;
        }

        // just call the update here and it means your records are saved with whatever data you modified in VF page Input Controls.
        update dataRecords;

    }
    public void saveSingleRecord() {
        string recId = ApexPages.currentpage().getParameters().get('myRecId');
        myCustomObject__c mySingleRec;
        for (myCustomObject__c c : dataRecords) {
            if (recId == c.Id) {
                mySingleRec = c;
                break;
            }
        }
        if (mySingleRec != null)
            update mySingleRec;
    }
}

Visualforce Page:

<apex:page controller="myClass">
<apex:form>
    <apex:pageBlock title="My Data Records" id="thePageBlock">
        <apex:pageMessages />
        <apex:actionStatus id="loadingStatus" startText="Please Wait. Processing..." />
        <apex:pageBlockButtons>
            <apex:commandButton value="Save All Data" action="{!saveData}" rerender="thePageBlock" status="loadingStatus"/>
        </apex:pageBlockButtons>
        <apex:pageBlockTable value="{!dataRecords}" var="rec" id="myTable">
            <apex:column headerValue="ID" value="{!rec.Id}" /> 
            <apex:column headerValue="Name"> 
                <apex:inputText value="{!rec.Name}" />
            </apex:column>
            <apex:column headerValue="Update">
                <apex:commandLink action="{!saveSingleRecord}" rerender="thePageBlock" status="loadingStatus">
                    Update
                    <apex:param value="{!rec.id}" name="myRecId" />  
                </apex:commandLink>
            </apex:column>
        </apex:pageblocktable>        
    </apex:pageBlock>
</apex:form>
</apex:page>

The value for Name (which is binded to an inputText in the dataTable is auto passed to your class variables).

Important thing to understand here is this that, we have binded a list of data records to dataTable so that List Variable holds your changed values.

Direct test link: https://vcdev-developer-edition.ap1.force.com/sample/samplemassedit

|improve this answer|||||
  • Hi! @varunc Thank you so much for your thorough explanation. For some weird reasons, the update function does not work! I dont understand why. But your code seems legit to me. But update does not work :( – agentx Feb 8 '15 at 16:59
  • Yes, I made some fault in code. instead of ReRender the myTable, the rerender should act on parent component i.e. pageBlock. I've corrected the code, it should work fine now. – VarunC Feb 8 '15 at 17:24
  • hmm still not updated.. Once rerenderd the table value changed but the record not updated when refreshed. – agentx Feb 8 '15 at 17:37
  • Well I posted my code right off the hook so I went in and created a page out of it. I found a problem with a closing tag so I updated full page code in my post above. And it does seem to be working here. For test case you can just change any textbox value and click either Save All or Update link in row, it does update the value. Just save and then for a Test simply reload your browser to see that your entered changes have been saved into database. – VarunC Feb 8 '15 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.