I am passing a string created in my controller to a VF page through dynamic binding in order to trigger the download of a CSV with the desired fields generated from the string. my code is as follows (Most of the code here just creates the string, it is passed through the second method)
Controller:
public String DefinedCustomContent {
get {
String CustomContent = '';
List<Profile> profiles = [select Id, Name, UserLicenseId, UserType from Profile where Name = 'Contract Register Profile'];
Profile csGuestUserProfile = profiles[0];
ProSpend_Config__c csConfig = ProSpend_Config__c.getInstance(csGuestUserProfile.Id);
if(csConfig!=null)
{
string DownloadFields = null;
if(csConfig.Contract_Search_Detail_Page_Fields_1__c!=null)
DownloadFields += csConfig.Contract_Search_Detail_Page_Fields_1__c;
if(csConfig.Contract_Search_Detail_Page_Fields_2__c!=null)
DownloadFields += ','+csConfig.Contract_Search_Detail_Page_Fields_2__c;
//if(csConfig.Contract_Search_Detail_Page_Fields_3__c!=null)
// DownloadFields += ','+csConfig.Contract_Search_Detail_Page_Fields_3__c;
if(DownloadFields!='')
{
List<String> Fields = DownloadFields.split(',');
for(String Field : Fields)
{
List<String> FieldElements = Field.split('#');
if(FieldElements.size()==4 || FieldElements.size()==5)
{
//Fields to include
String trace = FieldElements[2].trim();
ExcelFields =+ ExcelFields + trace + ',';
trace = '"{!contract.proContract.' + trace + '}",';
DownloadFieldList =+ DownloadFieldList + trace;
}
}
}
}
//Clean Fields
DownloadFieldList= DownloadFieldList.substring(0,DownloadFieldList.length()-1);
DownloadFieldList= DownloadFieldList+ '\n';
CustomContent =+ DownloadFieldList;
return CustomContent;
}
set;
}
public Component.Apex.OutputText getFields() {
Component.Apex.OutputText detail= new Component.Apex.OutputText ();
if(DefinedCustomContent != ''){
detail.expressions.value = DefinedCustomContent ;
}
return detail;
}
VF PAGE:
<apex:page Controller="CS_DownloadController" readOnly="true" contentType="application/vnd.ms-excel#ContractsSearchData.csv">
<apex:repeat value="{!contracts}" var="contract">
<apex:dynamicComponent componentValue="{!Fields}"/>
</apex:repeat>
</apex:page>
My issue is that when I trigger the download I am receiving 2 versions of every record in the CSV.
If I manually insert the string onto the page the download works fine. However as the required headers may change I need this assignment to be dynamic.
If I put a test string at the front of the dynamic binding it will only paste that onto the front of the first duplicate row not the second so I know the issue is not with the repeat but the binding.
The variable {!contracts} is retruned from another (very long) method in the class and is a list of . I do not believe this is the issue either.