Let the platform do the work

How to Get the Most Active Users

Overview

A PHP example demonstrating how to fetch the most active users for meetings, calls, inbound emails, and outbound emails using the v11 /mostactiveusers REST GET endpoint.

Get Most Active Users

Authenticating

First, you will need to authenticate to the Sugar API. An example is shown below:

  <?php

$instance_url = "http://{site_url}/rest/v11";
$username = "admin";
$password = "password";

//Login - POST /oauth2/token
$auth_url = $instance_url . "/oauth2/token";

$oauth2_token_arguments = array(
    "grant_type" => "password",
    //client id - default is sugar. 
    //It is recommended to create your own in Admin > OAuth Keys
    "client_id" => "sugar", 
    "client_secret" => "",
    "username" => $username,
    "password" => $password,
    //platform type - default is base.
    //It is recommend to change the platform to a custom name such as "custom_api" to avoid authentication conflicts.
    "platform" => "custom_api" 
);

$auth_request = curl_init($auth_url);
curl_setopt($auth_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($auth_request, CURLOPT_HEADER, false);
curl_setopt($auth_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($auth_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($auth_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($auth_request, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json"
));

//convert arguments to json
$json_arguments = json_encode($oauth2_token_arguments);
curl_setopt($auth_request, CURLOPT_POSTFIELDS, $json_arguments);

//execute request
$oauth2_token_response = curl_exec($auth_request);

//decode oauth2 response to get token
$oauth2_token_response_obj = json_decode($oauth2_token_response);
$oauth_token = $oauth2_token_response_obj->access_token;

More information on authenticating can be found in the How to Authenticate and Log Out example and /oauth2/logout endpoint documentation.

Active Users

Next, we can retrieve the most active users using the /mostactiveusers endpoint.

  //Fetch users - GET /mostactiveusers

$most_active_arguments = array(
    "days" => 30,
);

$most_active_url = $instance_url . "/mostactiveusers";

$most_active_url .= "?" . http_build_query($most_active_arguments);

$most_active_request = curl_init($most_active_url);

curl_setopt($most_active_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($most_active_request, CURLOPT_HEADER, false);
curl_setopt($most_active_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($most_active_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($most_active_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($most_active_request, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "oauth-token: {$oauth_token}"
));

//execute request
$most_active_response = curl_exec($most_active_request);

//decode json
$most_active_response_obj = json_decode($most_active_response);

More information on the mostactiveusers API can be found in the /mostactiveusers documentation.

Request

The URL sent to the server is shown below:

  http://{site_url}/rest/v11/mostactiveusers?days=30

Response

The data received from the server is shown below:

  {
    "meetings": {
        "user_id": "seed_max_id",
        "count": "21",
        "first_name": "Max",
        "last_name": "Jensen"
    },
    "calls": {
        "user_id": "seed_chris_id",
        "count": "4",
        "first_name": "Chris",
        "last_name": "Olliver"
    },
    "inbound_emails": {
        "user_id": "seed_chris_id",
        "count": "23",
        "first_name": "Chris",
        "last_name": "Olliver"
    },
    "outbound_emails": {
        "user_id": "seed_sarah_id",
        "count": "20",
        "first_name": "Sarah",
        "last_name": "Smith"
    }
}

Download

You can download the full API example here.