|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | /** |
4 | | - * ©[2024] SugarCRM Inc. Licensed by SugarCRM under the Apache 2.0 license. |
| 4 | + * ©[2025] SugarCRM Inc. Licensed by SugarCRM under the Apache 2.0 license. |
5 | 5 | */ |
6 | 6 |
|
7 | 7 | require_once 'include.php'; |
8 | 8 |
|
9 | 9 | $SugarAPI = new \Sugarcrm\REST\Client\SugarApi($server, $credentials); |
| 10 | + |
10 | 11 | try { |
11 | | - if ($SugarAPI->login()) { |
12 | | - echo "Logged In: "; |
13 | | - pre($SugarAPI->getAuth()->getToken()); |
14 | | - $Account = $SugarAPI->module('Accounts')->set("name", "Test")->set("phone_office", "555-555-5555"); |
15 | | - echo "Creating Account: "; |
16 | | - pre($Account->toArray()); |
| 12 | + if ($SugarAPI->isAuthenticated()) { |
| 13 | + echo "Logged In: " . json_encode($SugarAPI->getAuth()->getToken(),JSON_PRETTY_PRINT) . "\n"; |
| 14 | + // Create an Account called Test with a phone number |
| 15 | + $Account = $SugarAPI->module('Accounts'); |
| 16 | + // You can set data via Array Access, Object Access, or set methods |
| 17 | + $Account->set("name", "Test") |
| 18 | + ->set("phone_office", "555-555-5555"); |
| 19 | + $Account['account_type'] = 'Prospect'; |
| 20 | + $Account->email1 = "test@sugar.dev"; |
17 | 21 | $Account->save(); |
18 | | - pre("Saved Account ID: {$Account['id']}"); |
| 22 | + echo "Saved Account ID: {$Account['id']}\n"; |
| 23 | + //Update Account |
19 | 24 | $Account->set('employees', '100'); |
20 | 25 | $Account['shipping_address_city'] = 'Indianapolis'; |
21 | | - echo "Changing fields employees and shipping address...<br>"; |
22 | 26 | $Account->save(); |
23 | | - echo "Account Updated: "; |
24 | | - pre($Account->toArray()); |
| 27 | + echo "Account Updated: " . json_encode($Account->toArray(),JSON_PRETTY_PRINT) . "\n"; |
| 28 | + |
| 29 | + //Retrieve the Account in a new Object |
25 | 30 | $Account2 = $SugarAPI->module('Accounts', $Account['id']); |
26 | 31 | $Account2->retrieve(); |
27 | | - echo "Retrieving the Account Again...<br>"; |
28 | | - pre($Account2->toArray()); |
| 32 | + echo "Retrieved Account: " . json_encode($Account2->toArray(),JSON_PRETTY_PRINT) . "\n"; |
| 33 | + //Delete the Account |
29 | 34 | $Account2->delete(); |
30 | | - echo "Account Deleted. Response: "; |
31 | | - pre($Account2->getResponseBody()); |
| 35 | + echo "Deleted Response: " . json_encode($Account2->getResponseBody(),JSON_PRETTY_PRINT) . "\n"; |
32 | 36 | } else { |
33 | 37 | echo "Could not login."; |
34 | | - pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); |
| 38 | + $oauthEndpoint = $SugarAPI->getAuth()->getActionEndpoint('authenticate'); |
| 39 | + $response = $oauthEndpoint->getResponse(); |
| 40 | + if ($response) { |
| 41 | + $statusCode = $oauthEndpoint->getResponse()->getStatusCode(); |
| 42 | + echo "[$statusCode] - " . $oauthEndpoint->getResponse()->getBody()->getContents(); |
| 43 | + } |
35 | 44 | } |
36 | 45 | } catch (Exception $ex) { |
37 | | - echo "Error Occurred: "; |
38 | | - pre($ex->getMessage()); |
39 | | - pre($ex->getTraceAsString()); |
| 46 | + echo "Exception Occurred: " . $ex->getMessage(); |
| 47 | + echo $ex->getTraceAsString(); |
40 | 48 | } |
0 commit comments