<?php

require_once __DIR__ . '/../vendor/autoload.php';
try {
    $SugarAPI = new \SugarAPI\SDK\SugarAPI('instances.this/Ent/7700/', array('username' => 'admin', 'password' => 'asdf'));
    $SugarAPI->login();
    $EP = $SugarAPI->filterRecords('Accounts');
    $response = $EP->execute()->getResponse();
    print_r($EP->getRequest());
    if ($response->getStatus() == '200') {
        $recordList = $response->getBody(false);
        $max = count($recordList->records);
        echo "found {$max} records from Filter Records request. <br>";
        $number = rand(0, $max);
        $randomRecord = $recordList->records[$number];
        echo "Choose random record #{$number}, with ID: " . $randomRecord->id . " <br>";
        $getRecord = $SugarAPI->getRecord('Accounts', $randomRecord->id)->execute(array('fields' => 'name'));
        $response = $getRecord->getResponse();
        if ($response->getStatus() == '200') {
            echo "Retrieved Record <br>";
            $randomRecord = $getRecord->getResponse()->getBody(false);
            $randomRecord->name = 'Updated Record Name';
            $updateRecord = $SugarAPI->updateRecord('Accounts', $randomRecord->id)->execute($randomRecord);
            $response = $updateRecord->getResponse();
            if ($response->getStatus() == '200') {
                $randomRecord = $updateRecord->getResponse()->getBody(false);
                echo "Updated Record <br>";
                print_r($randomRecord);
                $deleteRecord = $SugarAPI->deleteRecord('Accounts', $randomRecord->id)->execute();
                $response = $deleteRecord->getResponse();
                if ($response->getStatus() == '200') {
<?php

require_once __DIR__ . '/../vendor/autoload.php';
try {
    $SugarAPI = new \SugarAPI\SDK\SugarAPI('instances.this/Ent/7700/', array('username' => 'admin', 'password' => 'asdf'));
    $SugarAPI->login();
    $Accounts = $SugarAPI->filterRecords('Accounts')->setData(array('max_num' => 5));
    $Contacts = $SugarAPI->filterRecords('Contacts')->setData(array('max_num' => 1));
    $Notes = $SugarAPI->filterRecords('Notes')->setData(array('max_num' => 3));
    $Leads = $SugarAPI->filterRecords('Leads')->setData(array('max_num' => 2));
    $BulkCall = $SugarAPI->bulk()->execute(array($Accounts, $Contacts, $Notes, $Leads));
    $response = $BulkCall->getResponse();
    if ($response->getStatus() == '200') {
        echo "<h3>Requests Completed</h3><pre>";
        print_r($response->getBody());
        echo "</pre>";
    } else {
        echo "<h3>Request Failed</h3><pre>";
        print_r($response);
        echo "</pre>";
    }
} catch (\SugarAPI\SDK\Exception\Authentication\AuthenticationException $ex) {
    echo "Credentials:<pre>";
    print_r($SugarAPI->getCredentials());
    echo "</pre> Error Message: ";
    print $ex->getMessage();
} catch (\SugarAPI\SDK\Exception\SDKException $ex) {
    echo $ex->__toString();
}