+ Start a Discussion
Lantin MaryLantin Mary 

How to retain values on look up fields

I am working on a dynamic table, where i can add and delete rows using a command button. But when i delete a specific row, the values on the look up fields are cleared or assigned to a different row. Below are the code snippets. Thanks.
User-added image
<apex:variable value="{!0}" var="rowNum"/> 
            <apex:pageBlockSection columns="1" title="Adding Multiple Expense" collapsible="False">
            <apex:commandButton value="Add New" action="{!addNewRow}" rerender="ExpenseHead" immediate="true" />       
                <apex:pageBlockTable value="{!ExpenseList}" var="expItem"> 
                    <apex:column headerValue="User"> 
                        <apex:inputField value="{!expItem.User__c}" />
                    </apex:column>            
                    <apex:column headerValue="Category">                    
                       <apex:inputfield value="{!expItem.Category__c}"/>
                    </apex:column>
                    <apex:column headerValue="Amount">
                         <apex:inputField id="AmountId" value="{!expItem.Amount__c}"/>
                    </apex:column>
                    <apex:column headerValue="Use Day">
                        <apex:inputField value="{!expItem.Use_Day__c}"/>
                    </apex:column>
                    <apex:column headerValue="Status">
                        <apex:inputfield styleClass="RemoveNone" value="{!expItem.Status__c}"/>
                    </apex:column>
                    <apex:column headerValue="Done">
                        <apex:inputfield value="{!expItem.Done__c}"/>
                    </apex:column>
                    <apex:column headerValue="Project">
                        <apex:inputField value="{!expItem.Project__c}"/>
                    </apex:column>
                    <apex:column >
                         <apex:commandLink value="Delete" action="{!Deleterow}" rerender="ExpenseHead" immediate="true" > 
                             <apex:param value="{!rowNum}" name="rowToRemove" assignTo="{!rowToRemove}"/>
                         </apex:commandLink>
                         <apex:variable var="rowNum" value="{!rowNum + 1}"/>
                    </apex:column>            
                </apex:pageBlockTable>
            </apex:pageblockSection>
 
public class MultipleExpensesController{

    public Expense__c expense{get;set;}
    public Integer rowToRemove {get;set;}
    public List<Expense__c> ExpenseList{get;set;}
      
    public Expense__c setExpense(Expense__c ex){
        this.expense = ex;
        return expense;
    }
    
     public MultipleExpensesController(ApexPages.StandardController controller){
       expense = new Expense__c();
       ExpenseList = new List<Expense__c>();
       ExpenseList.add(expense);
    }

    public void deleteRow(){
      ExpenseList.remove(rowToRemove );
      system.debug(' index '+ rowToRemove );
    }
    
    public void addNewRow(){
       expense = new Expense__c();
       ExpenseList.add(expense);
    }
       
}

 
Mike.KatulkaMike.Katulka
Don't use the immediate=true in the commandLink when you are rerendering form elements.  That will perform an action WITHOUT sending the Form Data/View State.... aka the lookups... to the controller.

Link to docs (the help site seems to be down currently)
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_commandLink.htm

immediate=true is better for buttons like "Cancel" that don't need to send data to the controller.

You might have enabled this flag because of the required fields on the page and the typical error of it asking to fill all required fields before performing an action. It seems like a workaround, but it has it's cons.  Visualforce can be a tricky beast in regards to required fields/immediate=true/and actionRegions... but with some creativity you can get it done.

I'd suggest to just rule out this flag, fill all fields on the form in and try the delete row.

Hope this helps you get past the roadblock to come up with a good workaround.
Lantin MaryLantin Mary
Thanks. It seems like it was the cause of the problem. Thank you so much.