SugarCRM Cookbook – The School of REST – Part 1

sugarmajed —  February 28, 2014 — 9 Comments

Welcome to the School of REST – our first installment in the Sugar 7 Cookbook Series! We’ve always believed that DATA belongs to you and it should always be accessible to you! With Sugar 7 we’ve added a completely revamped REST API. Today we are going to cover all the basics of pushing data into and getting data out of your SugarCRM instance. For each of the API calls we’ll show how to do it in curl, a generic HTTP Request, as well as showing you the response. Also, before we get started you can always hit /rest/v10/help for detailed documentation on all the APIs.

In this Guide we will be creating an Account “Burger Palace” and relating it to a Contact “Bob Burger”. We will then show you the ins and outs of Creating and Updating Records, Filtering Lists, Creating Relationships, Filtering on Relationships, Marking Records as Favorites, and Deleting Records.

This guide is broken into 3 sections.

Part 1 – Connecting, Creating Records, and Filtering Lists

Part 2 – Retrieving, Updating, and Relating Records 

Part 3  – Filtering on Relationships, Favorites, and Deleting Records 

You may want to download PostMan for Chrome to follow along interactively from  http://www.getpostman.com/

You can download Our Cookbook1 PostMan collection from  https://gist.github.com/mitani/f39e8d94df9fbda4d97d

Step 1. Connecting/Authenticating

Sugar 7 uses two-legged OAuth2 for authentication. You simply need to do a POST to /rest/v10/oauth2/token with the following params:

grant_type String Type of request. Available grant types are “password” and “refresh_token”.
client_id String The client_id of “sugar” will automatically create an OAuth Key in the system and can be used for “password” authentication. The client_id of “support_portal” will create an OAuth Key if the portal system is enabled and will allow for portal authentication. Other client_id’s can be created by the administrator in the OAuthKeys section in the Administration section and can be used in the future for additional grant types, if the client secret is filled in, it will be checked to validate use of the client id.
client_secret String The clients secret key.
username String The username of the user authenticating to the system.
password String The plaintext password the user authenticating to the system.
platform String Defaults to “base” allows you to have custom meta-data per platform

So, first we are going to login using a grant_type of “password”.

Once you get the response you’ll need to hold onto the access_token and the refresh_token. Anytime the access_token is invalidated, you’ll want to make another request to the token endpoint with a grant_type of “refresh_token”. Store just the refresh_token in long term storage – not the username and password.

For all the following requests we are going to pass in a header of OAuth-Token: {{your access token here}}

2. Create an Account – Burger Palace

Now that we have the access_token we can start having some fun! Let’s create a new customer – “Burger Palace” as an Account.

Being that everything is RESTful creating a new record is just a POST away. In this case we just do a POST to /rest/v10/Accounts.

For this example we will be posting an id as part of the record. You do not need to do this as SugarCRM will automatically generate an id for your record.

And of course you can add any additional fields that you would like to set.

3. Retrieve a list of Accounts 

Great, now that we have records in our system let’s fetch them back.  Being RESTful you just need to do a GET request to /rest/v10/Accounts. Don’t forget to pass in the OAuth-Token header.  To speed things up be specific about the fields that you want. Let’s just fetch the name, account type, and description by passing in fields=name,account_type,description. We’ll pass in max_num to specify that we only want 3 records at a time. By default if max_num isn’t set it will return 20 records. In this case the request will look like /rest/v10/Accounts?fields=name,account_type,description&max_num=3

Excellent! If we wanted to paginate we could just add the offest param to the request. That is the number of records you want to skip in the list before returning anything. So passing an offset of 3 would get us to the next page of records.

4. Filter a list of Accounts by Name 

Now, let’s filter our list for records that have the name “Burger Palace”. To do this we just need to do a GET request to /rest/v10/Accounts and to pass in the filter parameter. In this case the request would look like /rest/v10/Accounts?filter[0][name]=Burger Palace&fields=name,account_type,description

You’ll notice in the response that the next_offset in the response is set to -1 which means that there are no more records in the collection.

5. Filter a list of Accounts by Names starting with “B” and a specific email address

Now, let’s pass in multiple filter conditions and let’s use some of the filter operations.

Sugar 7 supports the following filter operations

$equals Performs an exact match on that field.
$not_equals Performs an exact match on that field.
$not_equals Matches on non-matching values.
$starts Matches on anything that starts with the value.
$in Finds anything where field matches one of the values as specified as an array.
$not_in Finds anything where field does not matches any of the values as specified as an array.
$is_null Checks if the field is null. This operation does not need a value specified.
$not_null Checks if the field is not null. This operation does not need a value specified.
$lt Matches when the field is less than the value.
$lte Matches when the field is less than or equal to the value.
$gt Matches when the field is greater than the value.
$gte Matches when the field is greater than or equal to the value.

So to filter for Account names starting with the letter “B” we’ll use the $starts operator. So instead of “filter[0][name]=Burger Palace” we can use filter[0][name][$starts]=B.

SugarCRM stores email addresses as normalized values. To search for an Account with an email address we need to search using the relationship for email addresses which is email_addresses. Specifically, we want to filter on the field email_address within that relationship. So to do an exact match on a specific email address we pass in  filter[0][email_addresses.email_address]=burgers@example.com.  Our request will look like /rest/v10/Accounts?filter[0][name][$starts]=B&filter[0][email_addresses.email_address]=burgers@example.com&fields=name,account_type,description,email . You will notice that we also added email to the list of fields that we wish to have returned which will cause it to return a collection of all the email addresses that are associated with this record.

Continue to Part 2 – Retrieving, Updating, and Relating Records 

9 responses to SugarCRM Cookbook – The School of REST – Part 1

  1. 

    This is good information on SugarCRM V7 REST.

    Concerning filters, can custom expressions be used with multiple fields? For example, say I have custom latitude and longitude fields for my Accounts module. Based on a search by radius (estimation) expression, how would I pass in an expression using these two fields? The expression would require two fields and math functions such as COS() & SQRT(). Would I need to pull all records and then do the calculation in JavaScript?

    • 

      Hi Jeff- Thank you we are looking forward to providing a lot of great content around Sugar 7. Right now you can’t pass up custom expressions, but in an upcoming blog post we’ll show how to add custom REST endpoints that may help you out. Of course with Latitude and Longitude you can always use the existing filters to create bounding boxes and then to do additional work on the JS side to do calculations on any point within that bounding box.

  2. 

    Great article! I was looking for this…asking about it in other posts. Just must note that url in curl should be between quotes, brackets escaped and space replaced by %20 (otherwise, you’ll get a globbing error from curl).
    This worked for me:
    “htp://url/rest/v10/Accounts?filter\[0\]\[name\]=Burger%20Palace&fields=name,account_type,description,email”

  3. 

    Jeff, you reminded me things that can be done using mongoDB.

  4. 

    Could you please expand and explain how and why “email_addresses.email_address” works in the filter? There is no email_addresses “relationship” in the GUI or relationships table.

    Which other “relationships” work? I experimented with a few (accounts, accounts_contacts, etc) and none succeeded. Which other items are available to the filter like this? Where is this documented?

Trackbacks and Pingbacks:

  1. SugarCRM Cookbook – The School of REST – Part 3 « SugarCRM Developer Blog - February 28, 2014

    […] already created our Account “Burger Palace” in Part 1 and showed how to filter lists with various […]

  2. SugarCRM Cookbook – The School of REST – Part 2 « SugarCRM Developer Blog - February 28, 2014

    […] is part 2 of 3 of The School of REST. In Part 1 we covered creating our Account “Burger Palace” and using the filter API to get just […]

  3. daily 03/06/2014 | Cshonea's Blog - March 6, 2014

    […] SugarCRM Cookbook – The School of REST – Part 1 « SugarCRM Developer Blog […]

  4. SugarCRM Cookbook – So you wanna override an endpoint « SugarCRM Developer Blog - March 21, 2014

    […] need to have some basic idea of how to interact with the SugarCRM API from a client standpoint and The School of REST – Part 1 is a great place to start. After that you should probably learn how to add a new API so you could […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s