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
VaderVader 

Dynamic Field Wrapper and Static Values

I have an object with two fields.  One that is a text field that will contain a task ID and another that will contain a user Id.  I have a wrapper (thanks to the very excellent tutorials done by Bob Buzzard) that will build input fields dynamically in order to associate a user (via lookup field) who wants to be informed about a task with the task id (stored in an 18 char text field since for some crazy reason in the salesforce universe you can't create a lookup from a customer object to Tasks...)  (i.e. there will be a many to one relationship)

 

The code below does a great job of inserting the values in the custom object for the user info, however I need to grab the task ID from the URL and either 

 

1) propagate the ID value in a hidden visualforce field then try to pick it up in the save event and apply it to the list object - OR -

2) just reference the code from the custom controller and not worry about the visualforce code at all and apply it to the list object during the save event.

 

Here is the Visualforce:

 

<apex:page controller="ManageListController" tabstyle="Task">
	<apex:form >
		<apex:pageBlock title="Add Informed Users">
		
			<apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">        
				<apex:column headerValue="Name">
					<apex:inputField value="{!wrapper.iUsr.Informed_User__c}"/>
				</apex:column> 								
				<apex:column headerValue="Action">
					<apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
						<apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/> 
					</apex:commandButton>
				</apex:column>
			</apex:pageBlockTable>
			
			<apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
				<apex:param name="addCount" value="1" assignTo="{!addCount}"/> 
			</apex:commandButton>
			<apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">
				<apex:param name="addCount" value="5" assignTo="{!addCount}"/> 
			</apex:commandButton>
			<apex:commandButton value="Save" action="{!save}"/>
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

 

Here is the code for the Apex controller:

 

public class ManageListController 
{
	public List<informedUserWrapper> wrappers {get; set;}
	public static Integer toDelIdent {get; set;}
	public static Integer addCount {get; set;}
	private Integer nextIdent=0;
	
		  
	public ManageListController()
	{
		wrappers=new List<informedUserWrapper>();
		for (Integer idx=0; idx<1; idx++)
		{
			wrappers.add(new informedUserWrapper(nextIdent++));
		}
	}
	


	public String getTaskId()
	{
		string taskId = ApexPages.currentPage().getParameters().get('retURL');
		return taskId;		
	} 
	
	
	
	public void delWrapper()
	{
		Integer toDelPos=-1;
		for (Integer idx=0; idx<wrappers.size(); idx++)
		{
			if (wrappers[idx].ident==toDelIdent)
			{
				toDelPos=idx;
			}
		}
	   
		if (-1!=toDelPos)
		{
			wrappers.remove(toDelPos);
		}
	}
	  
	  
	  
	public void addRows()
	{
		for (Integer idx=0; idx<addCount; idx++)
		{
			wrappers.add(new informedUserWrapper(nextIdent++));
		}
	}
	 
	 
	  
	public PageReference save()
	{
		List<Task_Informed_Users__c> iUsrs = new List<Task_Informed_Users__c>();		
				
		for (informedUserWrapper wrap : wrappers)
		{
			iUsrs.add(wrap.iUsr);
		
		}
	   
		insert iUsrs;
	   
		return new PageReference('/' + Schema.getGlobalDescribe().get('Task_Informed_Users__c').getDescribe().getKeyPrefix() + '/o');
	}
	 

	  
	  
	public class informedUserWrapper
	{
		public Task_Informed_Users__c iUsr {get; private set;}
		public Integer ident {get; private set;}
			  
		public informedUserWrapper(Integer inIdent)
		{
			ident=inIdent;			
			iUsr = new Task_Informed_Users__c();		
	  	}
	}
}