Apexバッチの処理内でAPIを実行
Apexバッチの処理内でAPIを実行する場合は『Database.AllowsCallouts』の宣言が必要になります。
global with sharing class testBatch implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts{
public testBatch() {
}
}
kinkun's blog
Apexバッチの処理内でAPIを実行
Apexバッチの処理内でAPIを実行する場合は『Database.AllowsCallouts』の宣言が必要になります。
global with sharing class testBatch implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts{
public testBatch() {
}
}
ApexでREST API使用して画像ファイルUpload
public void fileUpload(Attachment attach){
HttpRequest req = new HttpRequest();
Http http = new Http();
HTTPResponse res = new HTTPResponse();
String boundary = '------------' + String.valueOf(DateTime.now().getTime());
String header += '--' + boundary;
header += '\r\n';
header += 'Content-Disposition: form-data; name="attach"; filename="' + attach.name + '"';
header += '\r\n';
header += 'Content-Type: ' + attach.ContentType;
String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
while(headerEncoded.endsWith('=')){
header += ' ';
headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header + ' ' +'\r\n\r\n'));
}
String bodyEncoded = EncodingUtil.base64Encode(attach.Body);
String footor += '\r\n';
footor += '--'+boundary;
footor += '\r\n';
footor += 'Content-Disposition: form-data; name="attachname"';
footor += '\r\n\r\n';
footor += attach.name;
footor += '\r\n';
footor += '--'+boundary+'--';
String footorEncoded = EncodingUtil.base64Encode(Blob.valueOf(footor));
Blob bodyBlob = null;
String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
if(last4Bytes.endsWith('='))
{
Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
HttpRequest tmp = new HttpRequest();
tmp.setBodyAsBlob(decoded4Bytes);
String last4Bytesfootor = tmp.getBody()+footor;
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded.substring(0,bodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4Bytesfootor)));
}
else
{
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footorEncoded);
}
req = new HttpRequest();
req.setHeader('Content-Type','multipart/form-data; boundary=' + boundary);
req.setMethod('POST');
req.setEndpoint('https://' + endPoint);
req.setBodyAsBlob(bodyBlob);
req.setTimeout(120000);
http = new Http();
res = http.send(req);
}
ApexでawsへファイルUpload
public Integer fileUpload(Attachment file){
String attachmentBodyEncoded = EncodingUtil.base64Encode(file.Body);
String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
String key = awsAccessKey;
String secret = awsSecretAccessKey;
String bucketname = awsBucket;
String host = awsService + '-' + awsReign + '.amazonaws.com';
String method = 'PUT';
String fileNameEncoded = EncodingUtil.urlEncode(file.Name, 'UTF-8');
HttpRequest req = new HttpRequest();
req.setMethod(method);
req.setEndpoint('https://' + bucketname + '.' + host + '/' + bucketname + '/' + fileNameEncoded);
req.setHeader('Host', bucketname + '.' + host);
req.setHeader('Content-Length', String.valueOf(attachmentBodyEncoded.length()));
req.setHeader('Content-Encoding', 'UTF-8');
req.setHeader('Content-type', file.ContentType);
req.setHeader('Connection', 'keep-alive');
req.setHeader('Date', formattedDateString);
req.setHeader('ACL', 'public-read-write');
req.setBodyAsBlob(EncodingUtil.base64Decode(attachmentBodyEncoded));
String stringToSign = 'PUT\n\n' +
file.ContentType + '\n' +
formattedDateString + '\n' +
'/' + bucketname + '/' + bucketname + '/' + fileNameEncoded;
Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(stringToSign),blob.valueof(secret));
String signed = EncodingUtil.base64Encode(mac);
String authHeader = 'AWS' + ' ' + key + ':' + signed;
req.setHeader('Authorization',authHeader);
Http http = new Http();
HTTPResponse res = http.send(req);
return res.getStatusCode();
}
Apexで選択リスト取得
//取引先の種別の選択リスト取得
List<Schema.PicklistEntry> picklist = getPicklist('Account', 'Type');
for(Schema.PicklistEntry pick : picklist){
System.debug('-----------------');
System.debug('Label :' + pick.getLabel());
System.debug('Value :' + pick.getValue());
System.debug('Active:' + pick.isActive());
}
//有効な選択リスト取得
public List<Schema.PicklistEntry> getPicklist(String sObjName, String fieldName){
List<Schema.PicklistEntry> pickList;
Schema.SObjectType sObjType = (Schema.SObjectType) Schema.getGlobalDescribe().get(sObjName);
if(sObjType != null){
Map<String, Schema.SObjectField> fieldmap = sObjType.getDescribe().fields.getMap();
Schema.SObjectField f = (Schema.SObjectField) fieldmap.get(fieldName);
if(f != null){
Schema.DescribeFieldResult r = f.getDescribe();
pickList = r.getPicklistValues();
System.debug('pickList' + pickList);
}
}
return pickList;
}
Bad Request
HyperText Transfer Protocol (HTTP) の 400 Bad Request
レスポンスステータスコードは、何らかのクライアント側のエラーであると分かったために、サーバーがそのリクエストを処理しない (できない) ことを表します (例えば、リクエストの構文が正しくない、リクエストメッセージのフレーミングが無効、リクエスト経路に偽りがあるなど)。
Visualforceページで静的リソース参照
Visualforce マークアップで静的リソースを参照する方法は、単独ファイルを参照するのか、またはアーカイブ (zip ファイルや .jar ファイルなど) に含まれるファイルを参照するのかによって異なります。
単独ファイルを参照するには、差し込み項目として $Resource.<resource_name> を使用します。このとき、<resource_name> は、リソースをアップロードしたときに指定した名前です。次に例を示します。
<apex:image url="{!$Resource.TestImage}" width="50" height="50"/>
又は
<apex:includeScript value="{!$Resource.MyJavascriptFile}"/>
アーカイブ内のファイルを参照するには、URLFOR 関数を使用します。最初のパラメータには、そのアーカイブをアップロードしたときに指定した静的リソース名を、第 2 パラメータには、アーカイブ内での目的ファイルへのパスを指定します。たとえば、次のとおりです。次に例を示します。
<apex:image url="{!URLFOR($Resource.TestZip,
'images/Bluehills.jpg')}" width="50" height="50"/>
又は
<apex:includeScript value="{!URLFOR($Resource.LibraryJS, '/base/subdir/file.js')}"/>
StandardSetControllerテストクラス
Apexクラス
/*
testページコントローラ
*/
public with sharing class testController {
//testページへ渡す対象Idリスト
public String testIdList { get;set; }
//コンストラクタ
public testController( ApexPages.StandardSetController controller ){
//初期化
List<test__c> testList = new List<test__c>();
Set<Id> testIdSet = new Set<Id>();
this.testIdList = '';
//testページで選択済みIdリスト取得
for(test__c testObj : (List<test__c>) controller.getSelected()){
testIdSet.add(testObj.Id);
}
//testページで選択済みリスト取得
testList = gettestList(testIdSet);
System.debug('testList.size() : ' + testList.size());
//testページへ渡す対象Idリスト取得
Integer cnt = 1;
if(testList.size() > 0){
for(test__c testObj : testList){
if(cnt == 1){
this.testIdList += String.valueOf(testObj.id);
}
else{
this.testIdList += ',';
this.testIdList += String.valueOf(testObj.id);
}
cnt += 1;
}
}
System.debug('this.testIdList : ' + this.testIdList);
}
//testページで選択済みリストからtestページへ渡す対象取得
private List<test__c> gettestList(Set<Id> testIdSet){
return [SELECT Id
, test__c // test
FROM
test__c
WHERE
Id IN : testIdSet
AND
test__c = 'test'
ORDER BY Id
];
}
}
Apexテストクラス
/*
testページコントローラテストクラス
*/
@isTest
private class testControllerTest {
//正常ケース
//対象レコード数:1件
@isTest static void UnitTest1() {
//testレコード作成
List<test__c> testList = new List<test__c>();
test__c test = inserttest();
testList.add(test);
// VisualForceのページリファレンスを取得
PageReference prRef = Page.test;
// カレントページにVisalForceページをセット
Test.setCurrentPage(prRef);
// テスト開始
Test.startTest();
// StandardSetControllerのコンストラクタ
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(testList);
ssc.setSelected(testList);
testController bbController = new testController(ssc);
// テスト終了
Test.stopTest();
}
//正常ケース
//対象レコード数:2件
@isTest static void UnitTest2() {
//testレコード作成
List<test__c> testList = new List<test__c>();
test__c test1 = inserttest();
test__c test2 = inserttest();
testList.add(test1);
testList.add(test2);
// VisualForceのページリファレンスを取得
PageReference prRef = Page.test;
// カレントページにVisalForceページをセット
Test.setCurrentPage(prRef);
// テスト開始
Test.startTest();
// StandardSetControllerのコンストラクタ
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(testList);
ssc.setSelected(testList);
testController bbController = new testController(ssc);
// テスト終了
Test.stopTest();
}
//異常ケース
//対象レコード数:0件
@isTest static void UnitTest3() {
//testレコード作成
List<test__c> testList = new List<test__c>();
test__c test = inserttest2();
testList.add(test);
// VisualForceのページリファレンスを取得
PageReference prRef = Page.test;
// カレントページにVisalForceページをセット
Test.setCurrentPage(prRef);
// テスト開始
Test.startTest();
// StandardSetControllerのコンストラクタ
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(testList);
ssc.setSelected(testList);
testController bbController = new testController(ssc);
// テスト終了
Test.stopTest();
}
//testレコード作成
//test:test
private static test__c inserttest(){
test__c test = new test__c();
test.Name = 'test';
test.test__c = 'test';
insert test;
return test;
}
//testレコード作成
//test:test以外
private static test__c inserttest2(){
test__c test = new test__c();
test.Name = 'test';
insert test;
return test;
}
}
テストメソッドは isTest アノテーションを使用して定義し、次の構文を使用します。
@isTest static void testName() {
// code_block
}
テストメソッドは、テストクラス (isTest アノテーションが付加されているクラス) で定義されている必要があります。次のサンプルクラスは、テストメソッドが 1 つあるテストクラスの定義を示しています。
@isTest
private class MyTestClass {
@isTest static void myTest() {
// code_block
}
}
JSENCODE
バックスラッシュ (\) などのエスケープ文字をアポストロフィー (‘) などの安全でない JavaScript 文字の前に挿入して、JavaScript で使用するテキスト文字列や差し込み項目値をエンコードします。
ページの読み込み時に JavaScript が実行され、アラートが表示されます。
<script>var ret = "foo";alert('xss');//";</script>
この場合、JavaScript が実行されないように、JSENCODE 関数を使用します。例
<script>var ret = "{!JSENCODE($CurrentPage.parameters.retURL)}";</script>