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

Retain the outputstring after changing the data in "selectlist" box



I have two dropdown boxes in my visualforce page namely "ListOfObjects and ListOfFields".

ListOfObjects: displays all the objects in the salesforce

ListOfFields : Displays all the fields belong to the selected object in the 'ListOfObjects' drop down box.


Once I select the field from ListOfFields it will fetch the metadata of that particular field and keeps on concatinating/Appending to the finalString (a variable)  based on the number of  fields selected.

How to make the variable finaltring to retain the previous String also once the objectname is changed in ListOfObjects box.


ex: first time if I select object Account and its fileds id and name , the finalstring contains the metadata of id and name fields,. Next if I select object Contact and field createddate within contact, then my finalString should display metadata of id,name,createddate.


Here is my code...


<apex:page controller="ListObjects" >    

    <apex:messages />

 <apex:pageBlock >
        <apex:form >
            <apex:ActionFunction name="loadField" reRender="fields"  status="myStatus,result" action="{!loadNewFieldSet}">
            <apex:ActionFunction name="DescribeSelectedFields" reRender="fieldsLabel,result"  status="myStatus" action="{!LoadFieldsLabel}">
                <apex:SelectList value="{!selectedObject}" size="1" multiselect="false"  label="ListOfObjects" onchange="loadField();"  id="ObjId" style="width:100%" >
                    <apex:SelectOptions value="{!Names}">

                <apex:SelectList value="{!FieldValues}" size="10" multiselect="true" label="ListOfFields" onclick="DescribeSelectedFields();" id="fields" style="width:100%">
                    <apex:SelectOptions value="{!fieldsList}">
             <apex:outputlabel id="result">



public  class ListObjects

   /* public PageReference CheckObjExists1() {
         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Your Message'));
        return null;
    } */

     public ListObjects()
          fieldsList = new List<SelectOption>();
      public string value1 {get;set;}
  //  List<Schema.DescribeFieldResult>  allesults ;
      public List<SelectOption> fieldsList {get;set;}
      public List<SelectOption> DescfieldsLabel { get; set; }
      public String FieldLabels { get; set; }
      public String[] FieldValues { get; set; }
      public String selectedObject { get; set; }
      string finalstring = 'label,type,length,visibleLines';     
     public List<SelectOption> getNames()
            List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();     
            List<SelectOption> options= new List<SelectOption>();
                for(Schema.SObjectType f : gd)
                       options.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getName()));
                       //  System.debug(gd);
           return options;
     public  void loadNewFieldSet()
         Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(selectedObject).getDescribe().fields.getMap();
         fieldsList  = new list<SelectOption>();       
            for(String s : objectFields.keySet())
                  fieldsList .add( new SelectOption(s,s));
     public void LoadFieldsLabel()
         Map<String, SobjectField> fieldMap = Schema.getGlobalDescribe().get(selectedObject).getDescribe().fields.getMap();
        // DescfieldsLabel  = new list<SelectOption>();
         List<Schema.DescribeFieldResult>  fresults = new List<Schema.DescribeFieldResult>();
         string header = 'fullName,label,type,length,visibleLines \n';
         string finalstring = header;     
               for(String f:FieldValues)                 
                    fresults.add( fieldMap.get(f).getDescribe());             
                    String name = fieldMap.get(f).getDescribe().getName();         
                    String label = fieldMap.get(f).getDescribe().getLabel();
                    DisplayType fieldtype = fieldMap.get(f).getDescribe().getType();
                    Integer length = fieldMap.get(f).getDescribe().getLength();
                    string recordString =   name + ',' +label + ',' + fieldtype + ',' + length + '\n';
                   // String newlinestr = recordString;
                 finalstring = finalstring + recordString;          
                // displaySelectedFieldsMetadata(recordString);
                value1 = finalstring ;                     
       /* public void displaySelectedFieldsMetadata(String recordString)
                  finalstring = finalstring + recordString;
                  value1 = finalstring ;            
            }    */    


Pls help..

Thanks in advance :)



Remove these two lines from the LoadFieldsLabel() method:


    string header = 'fullName,label,type,length,visibleLines \n';
    string finalstring = header;


The local version of 'finalstring' is hiding the class member version, so it is not being updated.  The local version goes away when the method is finished, along with the useful information.  If you remove the local 'finalstring' variable, the method will update the 'finalstring' member, and your field metadata will accumulate.


If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!


-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator



Hi Glyn,


Thanks for the response.

I have tried that earlier also but it is giving some other output which is not expected.


ex:1 :  if , First I select id field, it will display metadata of id

      second : select name field , it will display metadata of id twice, metadata of name once

       then if I select billing city field, It will display the metadata of id thrice, metadata of name twice, metadata of billingcity    once.

Actual output displaying : fullname, label,type,length,visibleLines Id,Account ID,ID,18 Id,Account ID,ID,18 Name,Account Name,STRING,255 BillingCity,Billing City,STRING,40 Id,Account ID,ID,18 Name,Account Name,STRING,255


   i.e : expected output : fullname,label,type,length,visibleLines BillingCity,Billing City,STRING,40 Id,Account ID,ID,18 Name,Account Name,STRING,255


text marked in red is repeating string.       



   2 : Also if I deselect any field , instead of removing the metadata of that field from final string it will display some other string (which is cashed at the end)