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
Lee YakiwchukLee Yakiwchuk 

Dynamic FieldSet not saving records/fields correctly

Hi,

We have a modified Edit All for our Opportunity Products.  There is a column on the Edit All page that contains a number of dropdown fields that are the configuration for the Product on that line.  We dynamically assign the Field Set to the product and display the config fields for it.  This all displays fine and the dropdowns work for the user.  However, when we save it will use the last fieldset as a template for saving.

For example:

Product 1 has Config A, B & C
Product 2 has Config B & C

When saved Product 2 saves fine.
Product 1 saves Config A into B and Config B in C and discards Config C altogether.

This makes sense as the last field set the page knows about is Product 2's.  Has anyone run into this and gotten it to remember Product 1's FieldSet so it binds the values to the correct field?

Here is the code we are using to display the code in a single column of the Edit All table:

<apex:repeat value="{!$ObjectType.OpportunityLineItem.FieldSets[layout]}"  var="f" >
                        <div style="margin-bottom:1em">
                        <span id="myLabel" class="label" style="align:left"> {!f.fieldPath} </span>
                        <apex:inputField value="{!oppLItem[f]}" required="{!OR(f.required, f.dbrequired)}" onchange="foo(this);"/><br/>
                          </div>
</apex:repeat>

Thanks!
Lee
Best Answer chosen by Lee Yakiwchuk
Jorge OrtegaJorge Ortega
Hi Lee,

<apex:variable does not work as you could expect when it is under an <apex:repeat

Try this
...
<apex:repeat value="{!$ObjectType.OpportunityLineItem.FieldSets[oppLItem.PriceBookEntry.product2.Configuration_Layout__c]}" var="f" >
...


 

All Answers

Jorge OrtegaJorge Ortega
I worked with fieldsets in a likely way, <apex:repeat worked fine and every field was correctly assigned.

I suggest you to ensure that fields are being correctly shown, create data with workbench (or other) with differents values for every field and test.

I made an apex component that do the magic, here is some of the component's code
<apex:attribute name="dataList" description="Row Collection" type="Object" required="true" />
<apex:attribute name="objectType" description="Object Name" type="String" required="true" />
<apex:attribute name="fieldSetName" description="Name of the field set to edit" type="String" required="true" />
...
<apex:dataTable value="{!$ObjectType[objectType].fieldsets[fieldSetName]}" var="field" styleClass="{!cssSelector} positionDataTable {!region}"
                    cellpadding="3" cellspacing="0" >
...
<apex:repeat value="{!dataList}" var="rowItem">
        <apex:column headerClass="{!headerClass}" headerValue="{!rowItem['Month__c']} {!rowItem['Year__c']} " >
            <span title="{!rowItem['Month__c']} {!rowItem['Year__c']}: {!field.Label}">
               <apex:outputField styleClass="{!field}_{!IF(rowItem[field] <= 0,0,1)}_span" value="{!rowItem[field]}" label="{!rowItem['Month__c']}: {!field.Label}"/>
            </span>
        </apex:column>
</apex:repeat>
....

 
Lee YakiwchukLee Yakiwchuk
Hi Jorge,

We have the correct fields as they display the inital values without issue.  However when we save them they all get reassigned to the wrong fields.  All of the saved fields get saved to the mapped fields for the fieldset Product 2 uses instead of the fieldset for Product 1.

Thanks,
Lee
Jorge OrtegaJorge Ortega
Hi Lee,

Could you post the complete loop over line items?
Lee YakiwchukLee Yakiwchuk
Hi Jorge,

Here is the code, I removed the columns to make it a bit shorter.
 
<table>
	<tr>
		<td>Product</td>
		<td>Service Location</td>
		<td>Service Location End Point</td>
		<td>Quantity</td>
		<td>NRI($)</td>
		<td>Existing RGU</td>
		<td>Config</td>
		<td>Price</td>
	</tr>
							
	<apex:repeat id="myTable" value="{!myLineItems}" var="oppLItem" rendered="{!mylineitems != '' && myLineItems.size > 0}">
			<tr>
				<td>
					...The columns....
				</td>
				<td>
					<apex:variable var="layout" value="{!oppLItem.PriceBookEntry.product2.Configuration_Layout__c}"/>
					<apex:outputPanel id="myPanel" styleClass="myClass requiredBlock">
						<apex:repeat value="{!$ObjectType.OpportunityLineItem.FieldSets[layout]}"  var="f" >                   
							<div style="margin-bottom:1em">                     
								<span id="myLabel" class="label" style="align:left"> {!f.label} </span>
								<apex:inputField value="{!oppLItem[f]}" required="{!OR(f.required, f.dbrequired)}" onchange="foo(this);"/><br/>
							</div>
						</apex:repeat>
					</apex:outputPanel>
				</td>
				<td>
					<b>List Price:&nbsp;</b><apex:outputField id="thePrice" value="{!oppLItem.PriceBookEntry.UnitPrice}" />
					<apex:inputField id="thePrice2" value="{!oppLItem.UnitPrice}" required="True" />
				</td>
			</tr>  
	</apex:repeat>
</table>

Thanks,
Lee
Jorge OrtegaJorge Ortega
Hi Lee,

<apex:variable does not work as you could expect when it is under an <apex:repeat

Try this
...
<apex:repeat value="{!$ObjectType.OpportunityLineItem.FieldSets[oppLItem.PriceBookEntry.product2.Configuration_Layout__c]}" var="f" >
...


 
This was selected as the best answer
Lee YakiwchukLee Yakiwchuk
You sir, are a genius!!  It all works now.  Thank you very much!