Find your content:

Search form

You are here

Picklist field values extracting to CSV format

 
Share
public class getSpecialitiesForSelection {

    public List<SelectOption> sOptions {get;set;}
    public List<String> myString1 {get;set;}
    public List<String> myString2 {get;set;}
    String fieldname;
    String objectname;

    public getSpecialitiesForSelection(){
        sOptions = new List<SelectOption>();
        myString1 = new List<String>();
        myString2 = new List<String>();
        Schema.DescribeFieldResult f = Schema.sObjectType.Account.fields.Specialty_1_vod__c;
        system.debug('Printing specialty values' + f);
        objectname ='Accounts';
        fieldname ='Specialty_1_vod__c';
        myString1.add(objectname);
        myString1.add(fieldname);
        for(Schema.PickListEntry p : f.getPicklistValues()){
            sOptions.add(new SelectOption(p.getValue(), p.getLabel()));
        }


        Schema.DescribeFieldResult f1 = Schema.sObjectType.Account.fields.Specialty_2_vod__c;
        objectname ='Accounts';
        fieldname = 'Specialty_2_vod__c';
        myString1.add(objectname);
        myString1.add(fieldname);
        for(Schema.PickListEntry p1 : f1.getPicklistValues()){
            sOptions.add(new SelectOption(p1.getValue(), p1.getLabel()));
        }

    }
}

Update

Above i modified the class and adding VF Page use - here teh problem is list is extracting twice, method is getting call two time, please let me know if something is wrong in below VF page

<apex:page controller="getSpecialitiesForSelection" cache="true" contentType="text/csv#Export.csv" language="en-US" >
"Object Name", "Field Name", "Picklist Value"
    <apex:repeat value="{!myString1}" var="a1" >
"{!a1}" 
        <apex:repeat value="{!myString2}" var="a2">
"", "{!a1}" 
            <apex:repeat value="{!sOptions}" var="a"> 
, , {!a.value}, {!a.label}
            </apex:repeat>
        </apex:repeat>
    </apex:repeat>
</apex:page>

Attribution to: user697

Possible Suggestion/Solution #1

Your code that builds the list of selectOptions is fine, but never gets called, your VF page accesses sOptions, but nothing has set that until getSpecialitiesForSection() has run, change your apex code to

public class  getSpecialitiesForSelection {

    public list<SelectOption> getsOptions() {
        List<SelectOption> sOptions = new List<SelectOption> ();
        Schema.DescribeFieldResult f = Schema.sObjectType.Account.fields.Industry;
        for(Schema.PickListEntry p : f.getPicklistValues()) {
            sOptions.add(new SelectOption(p.getValue(), p.getLabel()));
        }
        return sOptions;
    }
}

Attribution to: superfell
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2056

My Block Status

My Block Content