0

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.

1 Answer 1

0

It looks like this line is duplicating the data:

DownloadFieldList =+ DownloadFieldList + trace;

Perhaps it should be:

DownloadFieldList = DownloadFieldList + trace;

It's hard to know for sure because I don't see DownloadFieldList defined in your example code. It's possible that this property is holding state somewhere else in the code. If so, you should re-initialize it at the beginning of your get block or make it a local variable.

1
  • Thanks for your response! It was to do with initialising the variable inside the get method instead of outside the class as I was doing. It is all working correctly now Commented Dec 7, 2016 at 9:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.