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
PeacePeace 

Display different default value in different list boxes

Hi,

 

I am new to Salesforce, I have a query,  Please look into following scenario:

I made a VF page containing 'n' number of listboxes.

Each list box shows all the field names from a CSV file, and the first field name remains the default selected value for each list box (ie. All the list boxes are having same default value)

 

Number of list boxes are equal to number of fields in CSV file.

 

I want each list box to have a different field name as default selected value

 

Can anyone please help ?

 

 

 

 

kranjankranjan

Hi Peace,

 

Can you please share your VF code and a sample csv you are using for reference and describe what you want to achieve.

 

Regards

PeacePeace

Hi,

 

Following VF page (UploadCSV )is used for browsing and uploading any CSV file

Select Object to import CSV Data
<apex:selectList required="true" multiselect="false" size="1" label="Type"  value="{!selectedValue}">

                   <apex:selectOptions value="{!Items}"/>

                   </apex:selectList>

          <br/>   

 Select CSV file

                  <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> &nbsp;&nbsp;<br/>

 

                 <apex:commandButton action="{!ReadFile}" value="Next" id="theButton" style="width:70px;"/>

               

 

Following is the VF page (Mapping) showing N number of Listboxes .

Number of list boxes on the left are equal to the number of fields (columns) in the CSV file uploaded.

          

                        <apex:repeat value="{!list}" rows="{!Number}" var="c">

                       

                                 <apex:selectList multiselect="false" size="1"  style="width:200px;" value="{!selectedValue1[c]}">

                               

                                <apex:selectOptions value="{!Header}"/>

       

                                </apex:selectList>

                                &nbsp;&nbsp;&nbsp;&nbsp;

                                 <apex:selectList multiselect="false" size="1"  style="width:300px;" value="{!selectedValue2[c]}" >

                              

 

                                <apex:selectOptions value="{!FieldList}"/>

                               

                                </apex:selectList>

                            

                                <br/>

                                                        

                               </apex:repeat>

                             

                                <br/>

                                                                              

                     <apex:commandButton action="{!Savetoobj}" value="Save to Object" id="theBtn23" style="width:100px;"/>          

                           

 you can take any CSV file ( for eg download and use the CSV file provided at http://support.vendhq.com/entries/21265746-Sample-CSV-file-to-upload-into-trial-account.  Just keep only 5 columns and delete the rest for simplicity)

 

Controller:

 


public with sharing class BMController {

 public string nameFile{get;set;}
    
    public string selectedValue{get;set;}
    public string[] selectedValue1{get;set;}
    public string[] selectedValue2{get;set;}
     public string selectedValue3{get;set;}
     

    public Blob contentFile{get;set;}
    List<SelectOption> options1 = new List<SelectOption>();
    List<String> stdObjectNames = new List<String>();
   
    List<SelectOption> options = new List<SelectOption>();
     List<SelectOption> fieldsName =new List<SelectOption>();
       

    String[] filelines = new String[]{};
    String[] HeaderXLS = new String[]{};

    List<Account> accstoupload;

     List<Integer> lst = new List<Integer>();
     Integer length;

/*displays 'n' times listbox i.e HeaderXLS times
     public List<Integer> getList() {
     for( Integer k = 0; k < length; k++) {

         lst.add(k);
         }
        return lst;
    }
    public Integer getNumber()
    {
    return length;
    }

/*returns all standard n custom objs of salesforce
   public List<SelectOption> getItems()

    {
    for ( Schema.SObjectType typ : Schema.getGlobalDescribe().values() )
    {
        String sobjName = String.valueOf(typ);
        if ( sobjName.contains('__c') || (!sobjName.contains('__c'))  )
        {
            stdObjectNames.add(sobjName);
            options1.add(new SelectOption(sobjName,sobjName));
           
        }
    }
    
       return options1;

     }
/*calling readfile on click on commandbutton next and inserting value in Object Account if value selected from 'Select object from csv is Account'
/* adds value in Header listbox
/*adds value in Fieldlist listbox

 
    public Pagereference ReadFile()
    {
        
        nameFile=contentFile.toString();
        filelines = nameFile.split('\n');
        accstoupload = new List<Account>();
       
         if(selectedValue == 'Account')
         {
        for (Integer i=1;i<filelines.size();i++)
        {
          
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
             
            Account a = new Account();
            a.Name = inputvalues[0];
            a.ShippingStreet = inputvalues[1];       
            a.ShippingCity = inputvalues[2];
            a.ShippingState = inputvalues[3];
            a.ShippingPostalCode = inputvalues[4];
            a.ShippingCountry = inputvalues[5];

            accstoupload.add(a);
        }
        try{
        insert accstoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
            ApexPages.addMessage(errormsg);
        }  
      }
              HeaderXLS = filelines[0].split(',');      
              length = HeaderXLS.size();  
              for(Integer j=0;j<HeaderXLS.size();j++)
              {
                options.add(new SelectOption(HeaderXLS[j],HeaderXLS[j]));
                
               }
                String objName = selectedValue ;
        SObjectType objToken = Schema.getGlobalDescribe().get(objName);
        DescribeSObjectResult objDef = objToken.getDescribe();
       
        Map<String, SObjectField> fields = objDef.fields.getMap();
        
        Set<String> fieldSet = fields.keySet();
       
       for(String s:fieldSet)
        {
            SObjectField fieldToken = fields.get(s);
            DescribeFieldResult selectedField = fieldToken.getDescribe();
            System.debug('FIELD ::: '+selectedField.getName());
            fieldsName.add(new SelectOption(selectedField.getName(),selectedField.getName()));
        }
          PageReference page2 = new PageReference('/apex/Mapping');
        page2.setRedirect(false);
       
         return page2;
    }
        
  /*displays values in both listboxes             
    
    public List<SelectOption> getHeader()
       {
       
        return options;
       }
       
     
    public List<SelectOption> getFieldList(){
       
        return fieldsName;
        }
   
    }