Last active
March 13, 2017 17:44
-
-
Save na0AaooQ/74d4848ed8c394ad9645277b0b5527fa to your computer and use it in GitHub Desktop.
Salesforce Apex で独自の Salesforce REST API を作成する (取引先責任者をLIKE検索して表示) ref: http://qiita.com/na0AaooQ/items/1ca72453d0192492d06e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestResource(urlMapping='/CustomContact/*') | |
global with sharing class CustomContactSample { | |
@HttpGet | |
global static sObject doGet() { | |
RestRequest req = RestContext.request; | |
String ContactName = req.params.get('name'); | |
ContactName = '%' + ContactName + '%'; | |
try { | |
Contact acc = [SELECT Id, Name, Email, Account.Name FROM Contact WHERE Name LIKE :ContactName LIMIT 1]; | |
return acc; | |
} catch (exception e) { | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@salesforce-api-test ~]$ vi /home/ec2-user/.bashrc | |
(末尾に以下を追加する) | |
### Sandbox環境へ接続する場合のAPIエンドポイント | |
##export DATABASEDOTCOM_HOST="test.salesforce.com" | |
### 本番環境へ接続する場合のAPIエンドポイント | |
export DATABASEDOTCOM_HOST="login.salesforce.com" | |
export DATABASEDOTCOM_CLIENT_ID="前述の「コンシューマ鍵」(OAuthコンシューマキー)を記載します。" | |
export DATABASEDOTCOM_CLIENT_SECRET="前述の「コンシューマの秘密」(OAuthコンシューマシークレット)を記載します。" | |
export DATABASEDOTCOM_CLIENT_USERNAME="APIを有効化しているSalesforceユーザアカウント名を記載します。" | |
export DATABASEDOTCOM_CLIENT_AUTHENTICATE_PASSWORD="APIを有効化しているSalesforceユーザアカウントのパスワードを記載します。" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@salesforce-api-test ~]$ source /home/ec2-user/.bashrc | |
[ec2-user@salesforce-api-test ~]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@salesforce-api-test ~]$ php -v | |
PHP 5.3.29 (cli) (built: May 12 2015 22:42:19) | |
Copyright (c) 1997-2014 The PHP Group | |
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies | |
[ec2-user@salesforce-api-test ~]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@salesforce-api-test ~]$ vi /home/ec2-user/select_salesforce_custom_api_contact.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@salesforce-api-test ~]$ php -l /home/ec2-user/select_salesforce_custom_api_contact.php | |
No syntax errors detected in /home/ec2-user/select_salesforce_custom_api_contact.php | |
[ec2-user@salesforce-api-test ~]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@salesforce-api-test ~]$ php /home/ec2-user/select_salesforce_custom_api_contact.php テスト | |
string(74) "https://ap2.salesforce.com/services/apexrest/CustomContact/?name=テスト" | |
Salesforce Custom REST API Access Success StatusCode =[200] | |
array(6) { | |
["attributes"]=> | |
array(2) { | |
["type"]=> | |
string(7) "Contact" | |
["url"]=> | |
string(56) "/services/data/v39.0/sobjects/Contact/00**************AS" | |
} | |
["Id"]=> | |
string(18) "00**************AS" | |
["Name"]=> | |
string(16) "テスト 太郎" | |
["Email"]=> | |
string(19) "example@example.com" | |
["AccountId"]=> | |
string(18) "00**************AH" | |
["Account"]=> | |
array(3) { | |
["attributes"]=> | |
array(2) { | |
["type"]=> | |
string(7) "Account" | |
["url"]=> | |
string(56) "/services/data/v39.0/sobjects/Account/00**************AH" | |
} | |
["Id"]=> | |
string(18) "00**************AH" | |
["Name"]=> | |
string(21) "テスト株式会社" | |
} | |
} | |
[ec2-user@salesforce-api-test ~]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("/home/ec2-user/Force.com-OAuth-Toolkit-for-PHP/oauth.php"); | |
if ( $argc == 2 ) { | |
$name = "$argv[1]"; | |
} | |
else { | |
echo "[Usage]: $argv[0] テスト\n"; | |
exit; | |
} | |
///// Salesforce REST API接続用の環境変数を.bashrcから取得する | |
// Salesforce REST APIで接続するアプリケーションの「コンシューマ鍵」 | |
$DATABASEDOTCOM_CLIENT_ID = getenv('DATABASEDOTCOM_CLIENT_ID'); | |
// Salesforce REST APIで接続するアプリケーションの「コンシューマの秘密」を.bashrcから取得する | |
$DATABASEDOTCOM_CLIENT_SECRET = getenv('DATABASEDOTCOM_CLIENT_SECRET'); | |
// Salesforce REST API接続用のSalesforceユーザ(Salesforceログインに使用しているEメールアドレスを設定)を.bashrcから取得する | |
$DATABASEDOTCOM_CLIENT_USERNAME = getenv('DATABASEDOTCOM_CLIENT_USERNAME'); | |
// Salesforce REST API接続用のSalesforceユーザのパスワードを.bashrcから取得する | |
$DATABASEDOTCOM_CLIENT_AUTHENTICATE_PASSWORD = getenv('DATABASEDOTCOM_CLIENT_AUTHENTICATE_PASSWORD'); | |
// Salesforce REST APIエンドポイントを.bashrcから取得する | |
$DATABASEDOTCOM_HOST = getenv('DATABASEDOTCOM_HOST'); | |
$LOGIN_URL = "https://" . $DATABASEDOTCOM_HOST . "/"; | |
///// Salesforce REST API接続用の設定 | |
$CACHE_DIR = '/home/ec2-user/tmp/session'; | |
$CALLBACK_URL = 'https://localhost/callback'; | |
///// | |
// Salesforce REST API接続用のOauthインスタンスを生成 | |
$oauth = new oauth( $DATABASEDOTCOM_CLIENT_ID, $DATABASEDOTCOM_CLIENT_SECRET, $CALLBACK_URL, $LOGIN_URL, $CACHE_DIR); | |
// Salesforce REST API接続にあたりSalesforceへの認証を実行 | |
$oauth->auth_with_password( $DATABASEDOTCOM_CLIENT_USERNAME, $DATABASEDOTCOM_CLIENT_AUTHENTICATE_PASSWORD ); | |
// カスタムAPIのURLを指定する | |
$url = $oauth->instance_url . "/services/apexrest/CustomContact/?name=" . $name; | |
$curl = curl_init($url); | |
var_dump($url); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth " . $oauth->access_token)); | |
// Salesforce REST API実行結果を保存 | |
$response = json_decode(curl_exec($curl), true); | |
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if ( $status != 200 ) { | |
print("Salesforce Custom REST API Access Failed StatusCode =[" . $status . "]\n"); | |
} else { | |
print("Salesforce Custom REST API Access Success StatusCode =[" . $status . "]\n"); | |
} | |
var_dump( $response ); | |
curl_close($curl); | |
$oauth->auth_with_refresh_token(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment