Sugar Query for Sugar 7

Last Modified Date

As per the scenario, a SugarCRM developer needs to query the database. So, how would they go about doing that? For Sugar 7, the SugarQuery API can be used instead of SQL. Sugar Query is an SQL query builder for retrieving data directly from the database and is used within SugarCRM.  SugarQuery has a very simple interface for building queries.

Basic methods to create a Sugar Query:

Below are a number of basic methods that you can use to create a Sugar Query:

  • select($fields) – accepts an array of fields that you would like to select
  • from($bean) – validates the query against a SugarBean at generation
  • where() – creates an AND where object
  • orWhere() – creates an OR where Object
  • join($link) – accepts a link from the from bean to create the correct join(s)
  • orderBy($field, $direction) – accepts a field and a direction to sort upon
  • groupBy($field) – accepts a field to group by
  • having($condition) – accepts a condition (see below)

The compileSql() function can be called on the object for viewing the SQL that is generated and the exexute() function executes the query.  An example for retrieving all Accounts with industry Media:

$query = new SugarQuery();




$results = $query->execute();

You can always get in touch if you’re still having issues creating a query in Sugar, or if you require any SugarCRM Customization, or Integration.

Rolustech is a SugarCRM Certified Developer & Partner Firm. We have helped more than 600 firms with various SugarCRM Customization and Integration. Get in touch today for your FREE Business Analysis. We will be happy to assist you!

Need Help? Get Free Consultation

By clicking you agree to our Terms and Conditions

Send me news and updates

Get in touch

Contact Information

Looking For a Job ?