如何在Visualfroce页面中获取Selected Object对象的字段标签名称而不是Field API Names?

时间:2015-04-26 11:50:31

标签: salesforce visualforce apex

public class Testing4 {

    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    public String selectedObject {get; set;}

    public String selectedField {get; set;}

    Public Testing4()
    {   
        selectedObject = 'account';
    }

    public List<SelectOption> getObjectNames() 
    {
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
        {
            objNames.add(new SelectOption(name,name));
        }
        return objNames;
     }

     public List<SelectOption> getObjectFields() 
     {
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

            Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);

            Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();


            List<SelectOption> fieldNames = new List<SelectOption>();
            for (String fieldName: fieldMap.keySet()) 
            {  
              fieldNames.add(new SelectOption(fieldName,fieldName));

            }
            return fieldNames;
      }       
}

如何在Visualfroce页面中获取Selected Object对象的字段标签名称而不是Field API名称?

这里我得到所选对象的所有字段API名称,但我只需要字段Lable(而不是API名称)。

1 个答案:

答案 0 :(得分:2)

尝试使用此方法从Salesforce对象字段中获取标签名称:

Schema.getGlobalDescribe().get('ObjectName').getDescribe().fields.getMap().get('FieldName').getDescribe().getLabel();

您的代码:

public List<SelectOption> getObjectFields() 
 {
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

        Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);

        Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();


        List<SelectOption> fieldNames = new List<SelectOption>();
        for (String fieldName: fieldMap.keySet()) 
        {  
          String fName = fieldMap.get(fieldName).getDescribe().getLabel();
          fieldNames.add(new SelectOption(fName, fName));

        }
        return fieldNames;
  }