0

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

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

QueryResult qr = connection.query(soqlQuery);

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

4

2 回答 2

1

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

 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;
}   

字符串的示例输出列表::

名称:Home_Phone__c 标签:家庭电话 名称:Office_Phone__c 标签:办公电话

于 2012-09-25T10:16:01.083 回答
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;
}
于 2021-03-13T11:34:57.243 回答