Dataspiderから2個以上の値を返してもらう時
ApexクラスからDataspiderへリクエストし、値を2つ以上返してもらう場合のApex側の値の取得の書き方メモ。
Dataspider側で設定する方はXML型。
Textで返してもらおうとしたらうまくいきませんでした。
・Apexクラス
//Httpリクエストのステータスを作成
Http http = new Http();
HttpRequest request = new HttpRequest();
//Dataspiderの接続に必要な情報を送る
Blob sendInfo = Blob.valueOf('ID情報' + ':' + 'PASS情報');
request.setEndpoint('エンドポイント情報');
request.setMethod('POST');
request.setHeader('Authorization', 'BASIC' + EncodingUtil.base64Encode(sendInfo));
request.setBody('何か送るものがあればここに記載'); ☆
request.setTimeout(ミリ秒で設定可能);
HttpResponse response = http.send(request);
//XMLの読み取り
Dom.Document doc = response.getBodyDocument();
Dom.XMLNode elements = doc.getRootElement();
String str1 = elements.getChildElement('戻り値の変数名1', null).getText();
String str2 = elements.getChildElement('戻り値の変数名2', null).getText();
☆補足
Dataspiderに渡す値がある場合、以下のようにして渡すことが可能。
String sendData = 'Dataspider側の変数名1' + 渡したい値 + '&Dataspider側の変数名2' + 渡したい値;
選択リストの内容を取得する
選択リストに設定されている有効な値を全部取得し、ページに表示したい。
(選択リストの取得と表示方法のみ記載。)
・Apexクラス
Schema.DescribeFieldResult dfResult = オブジェクト名.選択リスト型の項目API名.getDescribe();
List<Schema.PicklistEntry> pList = dfResult.getPicklistValues();
List<Striing> sList = new List<String>();
for (Schema.PicklistEntry p : pList) {
sList.add(p.getLabel());
}
<aura:attribute access="private" type="List" name="selectList" />
<aura:iteration items="{!v.selectList}" var="master" indexVar="index">
<div>
<label>
<input id="target1" type="radio" name="target" value="{!c.clickVal}">
<span id="target1_name" class="xxx">{!master}</span>
</label>
</div>
</aura:iteration>
Cookieの保存方法
テキストボックスに入力した情報をcookieで1ヶ月値を保存する場合
【cmp側】
//attribute
<aura:attribute access="private" type="Boolean" name="hasCookie" default="false">
<aura:attribute access="private" type="String" name="inputCookie" default="">
~省略~
//テキストボックス
<input id='cookie_input' class="xxx" type="text" name="testTextBox" value={!v.inputCookie} placeholder="未入力の時に表示する内容"/>
【controller.js側】
・初期処理のメソッド内
// cookie情報チェック
var checkInfo = document.cookie.split(';');
checkInfo.forEach(function(value){
// cookie名と値に分ける
var content = value.split('=');
// cookie名が「inputcookie」の場合、コンポーネントに設定
if (content[0] == 'inputcookie') {
component.set('v.hasCookie', true);
component.set('v.inputCookie', content[1]);
return;
}
});
・cookie保存処理のメソッド
// 入力した値
var inputVal = document.getElementById('cookie_input').value;
// cookie保存期間(30日を秒数に変換)
var deadline = 30*24*60*60;
// cookie保存
if (inputVal != '' && inputVal != null) {
// 初めてcookieが保存される場合
if (!component.get('v.hasCookie')) {
var cookie = "inputcookie=" + inputVal + "; Max-Age=" + deadline + "; path=/xxxxx/ ;";
document.cookie = cookie;
component.set('v.inputCookie', inputVal); //cmp側で定義
}
// すでに保存されているcookie情報と一致しない場合
else if (component.get('v.inputVal') != document.getElementById('cookie_input').value) {
var cookie = "inputcookie=" + inputVal + "; Max-Age=" + deadline + "; path=/xxxxx/ ;";
document.cookie = cookie;
component.set('v.inputCookie', inputVal); //cmp側で定義
}
}
Apexの処理完了を待たずにAura側の処理を進める
これが最適化は不明ですが、、、
以下の状態の時、
・Apex側はvoidメソッド
・非同期処理の@Furureが使えない
Aura側で以下のような書き方をしたらなんとかなった。
controller.js
var action = component.get('c.testMethod');
action.setParams({aaa : test1});
$A.enqueueAction(action);普段()で囲む部分を外しました。
トースト表示
トースト表示の備忘録。
詳細については公式サイトをご確認ください。
developer.salesforce.com
(例)
ボタンクリック後画面の上部に処理成功のトーストを表示
controller.js
showToastSuccess : function(component, event, helper) {
var toastEvent = $A.get('e.force:showToast');
toastEvent.setParams({
title: '処理が完了しました',
message:'問題がなければこちらのページを閉じてください',
type:'success',
duration:'5000' ←単位:ミリ秒 5秒表示するという意味
});
toastEvent.fire();
}