2012-01-08 11 views
16

मैं SOQL प्रलेखन के माध्यम से जा रहा था पर सभी क्षेत्रों को लाने के लिए, लेकिन लाने के लिए एक इकाई के सभी क्षेत्र डेटा का कहना है, खाता क्वेरी नहीं पा सके, जैसेSalesforce SOQL: क्वेरी इकाई

select * from Account [ SQL syntax ] 

क्या खाते के सभी डेटा लाने के लिए उपरोक्त की तरह एक वाक्यविन्यास है, या एकमात्र तरीका सभी क्षेत्रों को सूचीबद्ध करना है (हालांकि कई फ़ील्ड पूछे जाने वाले हैं)

उत्तर

16

आपको फ़ील्ड निर्दिष्ट करना होगा, अगर आप वर्णन गतिशील कुछ बनाना चाहते हैं SObject कॉल ऑब्जेक्ट के लिए सभी फ़ील्ड के बारे में मेटाडेटा देता है, ताकि आप उस से क्वेरी बना सकें।

+2

धन्यवाद। क्या आप वर्णन उदाहरण से क्वेरी बनाने के लिए एक उदाहरण साझा करना चाहते हैं। – Sukhhhh

24

इस तरह नक्शा बनाएं:

Map<String, Schema.SObjectField> fldObjMap = schema.SObjectType.Account.fields.getMap(); 
List<Schema.SObjectField> fldObjMapValues = fldObjMap.values(); 

तो फिर तुम एक SOQL क्वेरी स्ट्रिंग बनाने के लिए fldObjMapValues ​​के माध्यम से पुनरावृति कर सकते हैं:

String theQuery = 'SELECT '; 
for(Schema.SObjectField s : fldObjMapValues) 
{ 
    String theLabel = s.getDescribe().getLabel(); // Perhaps store this in another map 
    String theName = s.getDescribe().getName(); 
    String theType = s.getDescribe().getType(); // Perhaps store this in another map 

    // Continue building your dynamic query string 
    theQuery += theName + ','; 
} 

// Trim last comma 
theQuery = theQuery.subString(0, theQuery.length() - 1); 

// Finalize query string 
theQuery += ' FROM Account WHERE ... AND ... LIMIT ...'; 

// Make your dynamic call 
Account[] accounts = Database.query(theQuery); 

superfell सही है, कोई रास्ता नहीं सीधे एक का चयन करने के लिए है *। हालांकि, यह छोटा कोड नुस्खा काम करेगा (ठीक है, मैंने इसका परीक्षण नहीं किया है, लेकिन मुझे लगता है कि यह ठीक दिखता है)। समझा जा सकता है कि Force.com एक बहु-किरायेदार वास्तुकला चाहता है जहां संसाधनों को केवल स्पष्ट रूप से आवश्यक रूप से प्रावधान किया जाता है - आसानी से चयन करके * आसानी से जब फ़ील्ड का केवल एक उप-समूह वास्तव में आवश्यक होता है।

+0

धन्यवाद एडम। जैसा कि आप सुपरफेल से सहमत हैं, उसका जवाब स्वीकार करेंगे :-) – Sukhhhh

+0

बेशक सुख्ह्ह :) – Adam

6

मैं Force.com एक्सप्लोरर का उपयोग करता हूं और स्कीमा फ़िल्टर के भीतर आप टेबल नाम के बगल में स्थित चेकबॉक्स पर क्लिक कर सकते हैं और यह सभी फ़ील्ड का चयन करेगा और आपकी क्वेरी विंडो में डालेंगे - मैं इसे सभी को टाइप करने के लिए शॉर्टकट के रूप में उपयोग करता हूं - बस क्वेरी विंडो से कॉपी और पेस्ट करें। उम्मीद है की यह मदद करेगा।

public IEnumerable<String> GetColumnsFor<T>() 
{ 
    return typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance) 
     .Where(x => !Attribute.IsDefined(x, typeof(System.Xml.Serialization.XmlIgnoreAttribute))) // Exclude the ignored properties 
     .Where(x => x.DeclaringType != typeof(sObject)) // & Exclude inherited sObject propert(y/ies) 
     .Where(x => x.PropertyType.Namespace != typeof(Account).Namespace) // & Exclude properties storing references to other objects 
     .Select(x => x.Name); 
} 

यह वस्तुओं मैं परीक्षण किया है (और के लिए काम करने के लिए प्रकट होता है कॉलम से मेल खाता है:

3

मामले में किसी को भी एक सी # दृष्टिकोण के लिए देख रहा था, मैं प्रतिबिंब का उपयोग करें और निम्नलिखित के साथ आने में सक्षम था एपीआई परीक्षण द्वारा उत्पन्न)। वहाँ से, यह क्वेरी बनाने के बारे में बताया गया है:

/* assume: this.server = new sForceService(); */ 

public IEnumerable<T> QueryAll<T>(params String[] columns) 
    where T : sObject 
{ 
    String soql = String.Format("SELECT {0} FROM {1}", 
     String.Join(", ", GetColumnsFor<T>()), 
     typeof(T).Name 
    ); 
    this.service.QueryOptionsValue = new QueryOptions 
    { 
     batchsize = 250, 
     batchSizeSpecified = true 
    }; 
    ICollection<T> results = new HashSet<T>(); 
    try 
    { 
     Boolean done = false; 
     QueryResult queryResult = this.service.queryAll(soql); 
     while (!finished) 
     { 
      sObject[] records = queryResult.records; 
      foreach (sObject record in records) 
      { 
       T entity = entity as T; 
       if (entity != null) 
       { 
        results.Add(entity); 
       } 
      } 
      done &= queryResult.done; 
      if (!done) 
      { 
       queryResult = this.service.queryMode(queryResult.queryLocator); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     throw; // your exception handling 
    } 
    return results; 
} 
+0

अच्छा आदमी ब्रैड, जो काम करता है! – Shazoo

1

मेरे लिए यह आज Salesforce के साथ पहली बार था और मैं जावा में इस के साथ आया था: आपकी प्रतिक्रिया के लिए

/** 
* @param o any class that extends {@link SObject}, f.ex. Opportunity.class 
* @return a list of all the objects of this type 
*/ 
@SuppressWarnings("unchecked") 
public <O extends SObject> List<O> getAll(Class<O> o) throws Exception { 
    // get the objectName; for example "Opportunity" 
    String objectName= o.getSimpleName(); 

    // this will give us all the possible fields of this type of object 
    DescribeSObjectResult describeSObject = connection.describeSObject(objectName); 

    // making the query 
    String query = "SELECT "; 
    for (Field field : describeSObject.getFields()) { // add all the fields in the SELECT 
     query += field.getName() + ','; 
    } 
    // trim last comma 
    query = query.substring(0, query.length() - 1); 

    query += " FROM " + objectName; 

    SObject[] records = connection.query(query).getRecords(); 

    List<O> result = new ArrayList<O>(); 
    for (SObject record : records) { 
     result.add((O) record); 
    } 
    return result; 
} 
+3

कृपया कोड की दीवार पोस्ट करने के बजाय, हम जो देख रहे हैं उसे समझाएं। धन्यवाद। – Andrew

संबंधित मुद्दे