从SOQL查询中获取列的元数据

时间:2012-09-25 08:13:25

标签: java salesforce metadata wsc

我在java中有一个简单的SOQL查询,用于提取Salesforce标准对象,如下所示 -

String soqlQuery = "SELECT FirstName, LastName FROM Contact";

QueryResult qr = connection.query(soqlQuery);

我想获取对象字段的数据类型。

2 个答案:

答案 0 :(得分:1)

我在下面写了一个小函数,它将提供Salesforce ORG的自定义或标准对象中的Phone字段及其标签列表。我希望这可以帮助您编写代码的业务逻辑。

 public list<String> getFieldsForSelectedObject(){    
   selectedPhoneNumber = ''; //to reset home number field    
   list<String> fieldsName = new list<String>(); 
   selectedObject = 'Object Name' // This should have the object name for which we want to get the fields type
   schemaMap = Schema.getGlobalDescribe(); //Populating the schema map
   try{
       if(selectedObject != null || selectedObject != '' || selectedObject != '--Select Object--'){             
            Map<String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();        
            for(Schema.SObjectField sfield : fieldMap.Values()){
                schema.describefieldresult dfield = sfield.getDescribe();
                schema.Displaytype disfield= dfield.getType();
                system.debug('#######'  + dfield );       
                if(dfield.getType() == Schema.displayType.Phone){// Over here I am trying to findout all the PHONE Type fields in the object(Both Custom/Standard) 
                    fieldsName.add('Name:'+dfield.getName() +'  Label:'+ dfield.getLabel ());
                }   
            }
        }
    }catch(Exception ex){          
        apexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'There is no Phone or Fax Field Exist for selected Object!'));
    }           
    return fieldsName;
}   

String ::

的示例OUTPUT列表

名称:Home_Phone__c标签:家庭电话 名称:Office_Phone__c标签:Office Phone

答案 1 :(得分:0)

假设我们有以下 soql

select FirstName,LastName from Contact limit 2

QueryResult 对象中的查询结果如下所示。

{
    [2]XmlObject
    {
        name={urn:partner.soap.sforce.com}records, value=null, children=
        [
            XmlObject{name={urn:sobject.partner.soap.sforce.com}type, value=Contact, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}Id, value=null, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}FirstName, value=Bill, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}LastName, value=Gates, children=[]}
        ]
    },
    XmlObject
    {
        name={urn:partner.soap.sforce.com}records, value=null, children=
        [
            XmlObject{name={urn:sobject.partner.soap.sforce.com}type, value=Contact, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}Id, value=null, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}FirstName, value=Alan, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}LastName, value=Donald, children=[]}
        ]
    },
}

为了解析 QueryResult 并获取列名,我实现了以下方法,该方法将以逗号分隔的字符串返回列名。代码里面的逻辑我已经提到了。

public String getColumnNames(QueryResult soqlResponse)
{       
    String columns = ""
    try
    {           
        // We are looping inorder to pick the 1st record from the QueryResult
        for (SObject record : soqlResponse.getRecords()) 
        {               
            Iterator<XmlObject> xmlList =  record.getChildren();
            int counterXml = 0;
            while(xmlList.hasNext())
            {
                XmlObject xObj = xmlList.next();
                // Since the 1st 2 nodes contains metadata of some other information, we are starting from the 3rd node only
                if(counterXml > 1)
                {                               
                    columns += xObj.getName().getLocalPart() + ",";                 
                }
                counterXml++;
            }
            // Since we can get the column names from the 1st record, we are breaking the loop after the data of 1st record is read
            break;
        }
        // We are removing the last comma in the string
        columns = columns.substring(0, columns.length() - 1);
    }
    catch(Exception ex)
    {
        
    }   
    return columns;
}