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
Joseph BaroneJoseph Barone 

Reset VF lookup fields when a new entry is created.

Hello,

I have a VF page that allows for multiple entries to be created on the same page.  Two of the fields I'm referencing are Parent- Child lookup fields that I've created as a picklist (one is Client and other is Matter).  Matter is dependent on Client.  

The picklists works great and provide the right details.  The issue I run into is when I go to add in a new record, the Picklists retain their values from the previous entry.  If I try to change the entries, it gives me validation error.  As soon as I change the values back to the previous line it works great.   I have a feeling it is tied to how I have the VF page created, but I'm not 100 % sure.  Any suggestions would be appreciated.

Thank you,

 
<apex:page Controller="AddmultipleTimes" sidebar="false" action="{!setInitials}">
    
    <apex:form >
         
    <apex:pageBlock mode="edit" id="title">
    <table width="100%" border="0">
         <tr>
         <td width="100" valign="top">
         
     <apex:pageBlock title="Change Timekeeper" mode="edit" id="initialchange">
        
          <table cellpadded="2" cellspacing="2">
          <tr>
         <apex:inputtext value="{!initials2}" />
         <apex:commandButton action="{!newTimeKeeper}" value=" Submit Initials " style="color:red" reRender="thetable, mainpage">
                    
            </apex:commandButton>
          </tr>
          </table>         
        
        </apex:pageBlock>
             </td>
        <td valign="top">
        <apex:pageblock id="mainpage" title="New Time Entries for {!initials}" >
        
             <apex:pageMessages />
         
              
            <apex:pageblocktable value="{!listNewtime}" var="lnt" id="thetable"> <apex:variable value="{!0}" var="cnt"/>
              
            
                
                <apex:column width="100px">
                <apex:commandButton action="{!removeLine}" value=" X " style="color:red" reRender="thetable">
            <apex:param name="TimeCount" assignTo="{!TimeCount}" value="{!cnt}"/>
            
            </apex:commandButton>
          </apex:column>
                <apex:column >
                <apex:commandButton action="{!copyValues}" value="Copy" reRender="thetable">
                <apex:param name="TimeCount2" assignTo="{!TimeCount2}" value="{!cnt}"/>
                </apex:commandButton>
                </apex:column>
           
                <apex:column headerValue="Entry">   
                <apex:outputText value="{!cnt}" />
               <apex:variable var="cnt" value="{!cnt+1}"/>       
                                       
                </apex:column>
       
        <apex:column headerValue="Date">
            <apex:inputField value="{!lnt.Date__c}"/>
        </apex:column>
        
        <apex:column headerValue="Client Code">
        <apex:inputField value="{!lnt.Client_Code__c}" />
        </apex:column>
        <apex:column width="100px" headerValue="Matter #">
        <apex:inputField value="{!lnt.Matter__c}" />
        </apex:column>
                
        <apex:column headerValue="Client Code -- Test">
   		<apex:selectList size="1" value="{!clientc}">
            <apex:selectoptions value="{!clientobj}"/>
            <apex:actionSupport event="onchange" reRender="MatterList"/>
            </apex:selectList>
          
         
        </apex:column>
        <apex:column width="100px" headerValue="Matter # -- Test">
        <apex:selectList size="1" value="{!matt}" id="MatterList">
            <apex:selectoptions value="{!mattobj}"/>
            <apex:actionSupport event="onchange" />
            </apex:selectList>
                         
 
        </apex:column>
         
        <apex:column width="100px" headerValue="Hours">
        <apex:inputField value="{!lnt.Hours_Worked__c}"  style="width: 100px"/>
        </apex:column>
        <apex:column width="100px" headerValue="Task">
        <apex:inputField value="{!lnt.Tasks__c}" style="width: 100px"/>
        </apex:column>          
        <apex:column width="1000px" headerValue="Narrative">
            <apex:inputField value="{!lnt.Narrative__c}" style="width: 1000px"/>
        </apex:column>
  
        
            </apex:pageblocktable>
      
            
        <apex:pageBlockButtons >
            <apex:commandbutton value="Add New Row" action="{!addTime}" rerender="thetable"/>
            <apex:commandButton value="Save Times" action="{!saveTime}"/>
            
            </apex:pageBlockButtons>
            </apex:pageblock>
             </td>
        </tr>
        </table>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
sfdcsushilsfdcsushil
whats the error you are getting exactly? I had got some picklist validation errors in the past when there was issue in picklist value and label. are you populating same thing in both value and label. 
Joseph BaroneJoseph Barone
Here is the error message - 
j_id0:j_id1:title:mainpage:thetable:0:MatterList: Validation Error: Value is not valid


Below is the Apex Code  that goes with the VF Page.  
public class AddmultipleTimes {
Time_Entry__c newtime = new Time_Entry__c();
    
    public list<Time_Entry__c>listNewtime{ get;set;}
    public Integer TimeCount {
        get;
        set;}
    public Integer TimeCount2{get;set;}
    Public String initials {get;set;}
    Public string initials2 {get;set;}
  
    Public List <User> currentuser {get;set;}
    
    Public string clientc{get;set;}
    Public string matt{get;set;}
    
    public list<selectoption> getclientobj()
    {
        list<selectoption> options=new list<selectoption>();
        list<Client_code__c> clientc = [select name from Client_code__c order by name];
        options.add(new selectoption('--Select Client--','--Select Client--'));
        for(Client_code__c c:clientc)
        {
            options.add(new selectoption(c.name,c.name));
        }
        return options;
    }
	
    public list<selectoption> getmattobj()
    {
        list<selectoption> options = new list<selectoption>();
        list<Matter_lookup__c> matt=[select name from Matter_lookup__c where Client_codes__r.name=:clientc];
        options.add(new selectoption('--Select Matter--','--Select Matter--'));
        for (Matter_lookup__c c:matt)
        {
            options.add(new selectoption(c.name,c.name));
        }
        return options;
    }
    
    public AddmultipleTimes()
    {
        listNewtime=new list<Time_Entry__c>();
        listNewtime.add(newtime);
        
    }
    

    public void addTime()
    {
       
        List <User> userid2 = [Select ID from User Where alias = :initials];    
        
        Time_Entry__c newEntry = new Time_Entry__c();
        newEntry.OwnerID=string.valueof(userid2.get(0).id);
  		listNewtime.add(newEntry);
  
       
               
    }
    
        public PageReference copyValues()
    {
      
        String indexVal = ApexPages.currentPage().getParameters().get('TimeCount2');
       
        TimeCount2 = integer.valueof(indexVal);
        TimeCount2 = TimeCount2 - 1;
        system.debug(TimeCount2);
 
        addtime();
      integer newholdtime=TimeCount2+1;
        listNewtime.get(newholdtime).Client_Code__c = listnewtime.get(timecount2).Client_Code__c;
        listNewtime.get(newholdtime).Matter__c = listnewtime.get(timecount2).Matter__c;
        return null;
        
    }
    
    public PageReference removeLine()
    { 
                
        system.debug('Clicked Remove Line');
        
       String indexVal = ApexPages.currentPage().getParameters().get('TimeCount');
        system.debug(indexVal);
        TimeCount = integer.valueof(indexVal);
        TimeCount = TimeCount - 1;
        system.debug('TimeCount2: '+ TimeCount);

    
        listNewTime.remove(TimeCount);
 
            return null;
    }
    
    public PageReference saveTime()
    {
        try {
           insert listNewtime; 
        }
        catch(System.DMLException e)
        {
            ApexPages.addMessages(e);
            return null;
        }
        
         return Page.Alltimessaved;
    }
    
    public PageReference newTimeKeeper (){
    
  
        List <User> userid = [Select ID from User Where alias = :initials2];
       
            if(userid.isEmpty())
            {
                
                ApexPages.Message myMsg=new ApexPages.Message(ApexPages.severity.Error, 'Initials not found!');
                ApexPages.addMessage(myMsg);
                return null;
            }
        else{
            
        
            for (integer i=0;i<listnewtime.size();i++)
            {
            Time_Entry__c narrativeupdate = new Time_Entry__c();
            narrativeupdate=listnewtime.get(i);
            narrativeupdate.OwnerID=string.valueof(userid.get(0).id);
            listnewtime.set(i,narrativeupdate);
            }
                  
       
           initials=initials2;     
 			   
    return null;
        }
	}
    
    Public PageReference setInitials (){
       
                currentuser=[Select Alias from User where Id=:userinfo.getuserid()];        
        initials = string.valueof(currentuser.get(0).alias);
        return null;
       
    }
}

 
sfdcsushilsfdcsushil
ok. I had similar issue because of size of text field i was putting in Value of picklist. So i would suggest you use Id of client in value and name in label. hopefully that should resolve your issue. 
Joseph BaroneJoseph Barone
Unfortunately, adjusting the selectoptions it (c.id, c.name) breaks the code.  The link between Client and Matter is the Name field of both objects, therefore it appears they both have to begin with (name, display).  Since this is required, I still get the same error as stated above.