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
neckrneckr 

Field Set Labels

I created custom text fields to use as labels to get around the 40 character field constraint.  I want to display this associated fields as labels using field sets.  I created two field sets, one with the fields and one with the inputtext fields as labels.

 

My initial thought was two use to repeat functions but it loops around the 2nd field set too many times. Any thoughts on how I can achieve my objective?

 

 

 <apex:form >  
       <apex:pageBlock title="Without Field Set Use">  
       <apex:pageBlockSection title="Dynamic Object">  
           
          <apex:repeat value="{!$ObjectType.Field_Holder_1__c.FieldSets.ST00192_Labels}" var="fieldlabelAPIName">
        <apex:repeat value="{!$ObjectType.Field_Holder_1__c.FieldSets.ST00192}" var="fieldAPIName">
         
             <apex:pageblockSectionItem >
                 <apex:outputfield value="{!sObjectToBind[fieldlabelAPIName]}"/> 
                 <apex:inputField value="{!sObjectToBind[fieldAPIName]}"/>
             </apex:pageblockSectionItem>
           </apex:repeat>
                 </apex:repeat> 
       </apex:pageBlockSection>  
       </apex:pageBlock>  
   </apex:form>  

 

 

Shashikant SharmaShashikant Sharma

This is not the way to do 

Create A MAP

 

Map<FieldAPIName , Label> mapgetLabel  = new Map<FieldAPIName , Label> ();

 

use this map like

 

 

<apex:form >  
       <apex:pageBlock title="Without Field Set Use">  
       <apex:pageBlockSection title="Dynamic Object">  
           
          
        <apex:repeat value="{!$ObjectType.Field_Holder_1__c.FieldSets.ST00192}" var="fieldAPIName">
         
             <apex:pageblockSectionItem >
                 <apex:outputfield value="{!mapgetLabel[fieldlabelAPIName]}"/> 
                 <apex:inputField value="{!sObjectToBind[fieldAPIName]}"/>
             </apex:pageblockSectionItem>
           </apex:repeat>
          
       </apex:pageBlockSection>  
       </apex:pageBlock>  
   </apex:form>  

 

 

This will do it for you

 

neckrneckr

Thanks for your help. Can you help with the controller logic to populate the map. I started with something like this, but uncertain on how to link the two field sets in one map.

 

 

public Map<String, String> mapgetLabel {
 
 get{if (mapgetLabel == NULL){
 
      mapgetLabel = new Map<String , String> ();
      
// for (String fieldAPIName : fieldObject.FieldSets.ST00192){ 
   
      
// for (String fieldAPIlabelName :fieldObject.FieldSets.ST00192_Labels){ 



   // uncertain how to link the fieldsets to end up with this type of //populated map mapgetLabel.put(fieldAPIName,fieldAPIlabelName);
      }
      
}
  }
 return mapgetLabel;
 }
 
 set;
 }

 

 

 

 

 

 

neckrneckr

Any possible help with the controller code?

Shashikant SharmaShashikant Sharma

DO like this , use describe method

 

Add a public property

 

 

public Map<String , String> mapFieldList {get;set;}

 Add below code in constructor to get label , please add your labels instead of getting it from field describe as I have shown in example. You have to use label.yuorlabelname in value of map according to field api name.

 

 

 

Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();  
        //In this example I have hard coded the object name  
        Schema.sObjectType sObjType = globalDescription.get('Field_Holder_1__c');  
          
        sObjectToBind = sObjType.newSObject();  
        Schema.DescribeSObjectResult r1 = sObjType.getDescribe();  
          
        mapFieldList = r1.fields.getMap();  
         mapgetLabel  = new Map<FieldAPIName , string> ();        
        for(Schema.SObjectField field : mapFieldList.values())  
            {  
                Schema.DescribeFieldResult fieldResult = field.getDescribe();  
                mapgetLabel.put(fieldResult.getName() , fieldResult.getLabel());  
                    
            } 

 

Use mapgetLabel in VFP 

 

One more way could be check API name in IF condition in VFP in repeat of field set and set value of label accordingly 

This is easier also

 

 

<apex:form >  
       <apex:pageBlock title="Without Field Set Use">  
       <apex:pageBlockSection title="Dynamic Object">  
           
          <apex:repeat value="{!$ObjectType.Field_Holder_1__c.FieldSets.ST00192_Labels}" var="fieldlabelAPIName">
        <apex:repeat value="{!$ObjectType.Field_Holder_1__c.FieldSets.ST00192}" var="fieldAPIName">
         
             <apex:pageblockSectionItem >
                 <apex:outputlabel value="{!CASE(IF(ISNULL(fieldAPIName) , '' , fieldAPIName) , 'field1_APIName' , $Label.field1_Label , 'field2_APIName' , $Label.field2_Label ,  'No Label Found')}" />  
                 <apex:inputField value="{!sObjectToBind[fieldAPIName]}"/>
             </apex:pageblockSectionItem>
           </apex:repeat>
                 </apex:repeat> 
       </apex:pageBlockSection>  
       </apex:pageBlock>  
   </apex:form>  

 

 

 

 

 

neckrneckr

I am beginning to wrap my head around your suggested code, thanks for your help. 

 

However my Map isn't being populated as it is not identifying the key (field set fields). 

 

I also have a specific naming convention for the associated label field that I am trying to use to keep everything dynamic. 

 

Just to clarify, my asscoaited label fields are text fields with default read only values to be used as the labels.

 

I am not clear on how to use label.yourlabelname.  I highlighted the areas in the code that address comments above.

 

 

<apex:page standardcontroller="Cus_Service_Request__c" extensions="CusServiceRequestController">  
    
   <apex:form >  
       <apex:pageBlock title="Service Request Beta">  
       <apex:pageBlockSection title="Dynamic Object" columns="1">  
          
          <apex:repeat value="{!$ObjectType.CSR_Holder_1__c.FieldSets.Test_Field_Set}" var="field">
         
            <apex:pageblockSectionItem > 
               <apex:outputfield value="{!mapgetLabel[field]}"/>
                 <apex:inputfield value="{!fieldobject[field]}"/>
             </apex:pageblockSectionItem> 
           </apex:repeat>
       
     <apex:commandButton value="Save" action="{!Save}"/>
     
       </apex:pageBlockSection>  
       </apex:pageBlock>  
   </apex:form>  
  </apex:page>

 

public with sharing class CusServiceRequestController {

    public CusServiceRequestController(ApexPages.StandardController controller) {

    }

 
 
 public sObject sObjectToBind {get;set;}  
 
 public CSR_Holder_1__c fieldObject {get;set;}
 
 public Map<String, sObjectField> mapFieldList {get;set;}
 public Map<String, String> mapgetLabel {get;set;}

 
   // public List<String> listObjectFields {get;set;}  
          
    public CusServiceRequestController()  
    {  
     Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();  
        //In this example I have hard coded the object name  
        Schema.sObjectType sObjType = globalDescription.get('CSR_Holder_1__c');  
          
        sObjectToBind = sObjType.newSObject();  
        Schema.DescribeSObjectResult r1 = sObjType.getDescribe();  
          
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
         
         mapgetLabel  = new Map<String , String> ();        
        for(Schema.SObjectField field : mapFieldList.values())  
            {  
               Schema.DescribeFieldResult fieldResult = field.getDescribe();  
               
mapgetLabel.put(fieldResult.getName() , 'Test Label'); //Error: Map Key //CF_1_PL__c not found in Map (This field is a field in the VF field set). Associated Label //field to this field is CF_1_PL_Label__c

//I am thinking I can use something like this below to populate the field label based on my //naming convention

 

// mapgetLabel.put(fieldResult.getName() , //mapFieldList.get((left(fieldResult.getName(),len(fieldResult.getName()-3)))&& ( //'_Label__c'));  

 



 } } }

 

 

neckrneckr

Any possible suggestions at this stage?

Victor19Victor19
Hi Neckr,

Did you come up with a solution for this issue.. I have the same requirement now.

I would really appreciate if you could help me out.

Thanks!
neckrneckr
I store my labels as records in salesforce and I ended up using Dynamic visualforce to display the label as an output text field on the visualforce page.

http://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf_components_intro.htm

I hope this helps.
Victor19Victor19
Thanks for the quick reply.. Do you have some code that you can share?

It will greatly help me out!
neckr2neckr2

I dont have much time to make my code more baord friendly at this very moment, but let me know if this helps or if you have any questions.  I have pasted the snippets of the controller and visuaforce page that pertains to  dyanamic visualforce

 

Public List<Service_Task_STQ_Joiner__c> STQFieldGenerator(){
        
        if(STQList == null){
        try{STQList = [SELECT Name, Service_Task_Request_Questionnaire__r.Type__c, 
        Required__c, Values__c, Service_Task_Request_Questionnaire__r.Description__c, Service_Task_Request_Questionnaire__r.Label__c,
        Service_Task_Request_Questionnaire__r.Help_Text__c, Service_Task__r.Cus_Service_Task_Help_Text__c, Service_Task__r.Cus_Service_Task_Description__c, Service_Task__r.Name, Service_Task__r.Cus_Service_Task_Name__c
        FROM Service_Task_STQ_Joiner__c WHERE Service_Task__c = : getSelectedStar().Service_Task__c];
        }
        catch (exception e){
          
            }  
        }
            
       }
       return STQList;
}



public Component.Apex.Outputpanel getSTQSection() {
    

    STQList = STQFieldGenerator();
    
    Component.Apex.Outputpanel STQSection = new Component.Apex.Outputpanel(id='STQSection');
    
   
    //Loop through STQ records
        integer i = 0;
       
        for(Service_Task_STQ_Joiner__c STQ: STQList){
        
                STRRList.add(new Service_Task_Request_Response__c());

// InputField Code block (Text, Date & Number fields)

                            if(STQ.Service_Task_Request_Questionnaire__r.Type__c == 'Text' || STQ.Service_Task_Request_Questionnaire__r.Type__c == 'Date' || STQ.Service_Task_Request_Questionnaire__r.Type__c == 'Number' ){
                                system.debug(' SFDC 1 '); 
                              if(!STQ.Service_Task_Request_Questionnaire__r.Label__c.Contains('Postal/ZIP Code'))  {
                              system.debug(' SFDC 2 ');                                 
                                 Component.Apex.InputField STQInputField = new Component.Apex.InputField();
                                   
                                    if(STQ.Service_Task_Request_Questionnaire__r.Type__c == 'Date')
                                    {STQInputField.expressions.value = '{!STRRList['+i+'].Date_Response__c}';
                                    }
                                    
                                    else if(STQ.Service_Task_Request_Questionnaire__r.Type__c == 'Number')
                                    {STQInputField.expressions.value = '{!STRRList['+i+'].Number_Response__c}';
                                    system.debug(' SFDC 4 ');}
                                    
                                    else
                                    {STQInputField.expressions.value = '{!STRRList['+i+'].Response__c}';
                                    system.debug(' SFDC 5 ');}
                                    
                                    STQInputField.id = 'STQInputField'+i;
                                    system.debug(' SFDC 3 ' + STQ.Required__c +'$$$' + STQInputField);                                                             
                                  //  STQInputField.Required = STQ.Required__c;
                                  //  STQInputField.Required = True;
                                    
                                 // system.debug('SFDC Test 516 ' +STQInputField.Required);
                                                                            
                                 Component.Apex.OutputLabel STQOutputLabel = new Component.Apex.OutputLabel();
                                    STQOutputLabel.value = STQ.Service_Task_Request_Questionnaire__r.Label__c;
                                    STQOutputLabel.for = 'STQInputField'+i;
                                    STQOutputLabel.Title = STQ.Service_Task_Request_Questionnaire__r.Help_Text__c;
                                                                     
                                 STRRList[i].Service_Task_Question__c = STQ.Service_Task_Request_Questionnaire__r.Label__c;
                                 
                                 Component.Apex.Outputtext dynLiItemOpen = new Component.Apex.Outputtext(escape=false);
                                 dynLiItemOpen.value='<li data-role="fieldcontain" class="ui-field-contain ui-body ui-br ui-li ui-li-static ui-body-c">';
                                 
                                 Component.Apex.Outputtext dynLiItemClose = new Component.Apex.Outputtext(escape=false);
                                 dynLiItemClose.value='</li>'; 
                                 
                                 STQSection.childComponents.add(dynLiItemOpen);
                                 STQSection.childComponents.add(STQOutputLabel);
                                 STQSection.childComponents.add(STQInputField);
                                 STQSection.childComponents.add(dynLiItemClose);
                              }     
                        } 
        
                       
      
        i++;}
     List<Service_Task_Request_Response__c> strResponseList = new List<Service_Task_Request_Response__c>();
     for(Service_Task_Request_Response__c sTaskResponse : STRRList) {
        if(sTaskResponse.Service_Task_Question__c != null) {
            strResponseList.add(sTaskResponse);
        }
     }
     STRRList = strResponseList;
     System.Debug('######DEBUGX: STQSection = ' + STQSection); 
        
        
    return STQSection;
}

 

Visualforce page section

 <div>
							                	<apex:dynamicComponent componentValue="{!STQSection}"/>
							                </div>