salesforce SOQL : query to fetch all the fields on the entity

前端 未结 6 2031
不思量自难忘°
不思量自难忘° 2020-12-17 10:31

I was going through the SOQL documentation , but couldn\'t find query to fetch all the field data of an entity say , Account , like

select * from Account         


        
6条回答
  •  抹茶落季
    2020-12-17 11:03

    For me it was the first time with Salesforce today and I came up with this in Java:

    /**
     * @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  List getAll(Class 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 result = new ArrayList();
        for (SObject record : records) {
            result.add((O) record);
        }
        return result;
    }
    

提交回复
热议问题