数式でSlackの@here対応
'<!here>'
kinkun's blog
数式でSlackの@here対応
'<!here>'
AggregateResultリストから項目値取得
List<String> testQueueUserIdList = new List<String>();
for(AggregateResult gmObj : [SELECT UserOrGroupId, COUNT( Id )
FROM GroupMember
WHERE GroupId IN ( SELECT Id FROM Group WHERE Type = 'Queue' And DeveloperName = 'testQueue')
GROUP BY UserOrGroupId]){
testQueueUserIdList.add(String.valueOf(gmObj.get('UserOrGroupId')));
}
System.debug('testQueueUserIdList : ' + testQueueUserIdList);
Unreachable statement
Apex Codeで当該の行への到達可能性がない場合に「Unreachable statement」エラーが発生します。
return true;//★①
String str = 'test'; ←この文は、★①の「return」より後なので、到達しないため、エラー発生する。
inherited sharing
クラスで inherited sharing キーワードを使用して、そのコール元のクラスの共有モードでクラスを実行します。
この例では、inherited sharing のある Apex クラスとその Apex コードの Visualforce 呼び出しを宣言します。inherited sharing 宣言により、実行ユーザが共有アクセス権を持つ取引先責任者のみが表示されます。この宣言が省略されている場合、安全でないデフォルトの動作により、ユーザが参照権限を持たない取引先責任者も表示されます。
public inherited sharing class InheritedSharingClass {
public List<Contact> getAllTheSecrets() {
return [SELECT Name FROM Contact];
}
}
キューユーザリスト取得
SELECT UserOrGroupId, COUNT( Id ) FROM GroupMember WHERE GroupId IN ( SELECT Id FROM Group WHERE Type = 'Queue' And DeveloperName = 'testQueue') GROUP BY UserOrGroupId
Apex コントローラでの内部クラスの使用
public with sharing class SampleController {
//Sample用
public SampleSetting Sample {get;set;}
// コンストラクタ
public SampleController(){
//初期処理
init();
}
//初期処理
private void init(){
//Sample
SampleSet();
}
//Sample
private void SampleSet(){
// カスタム[SampleSetting]から情報取得
SampleSetting__c mc = SampleSetting__c.getOrgDefaults();
mc = SampleSetting__c.getOrgDefaults();
this.Sample = new SampleSetting();
this.Sample.Sample_Key = mc.Sample_Key__c;
}
//Sampleクラス
public class SampleSetting {
public String Sample_Key {get;set;}
public SampleSetting(){
this.Sample_Key = '';
}
}
}
Apexトリガのアップデートの前後の値の比較
trigger UpdateProductObjTrigger on ProductObj__c (before update) {
for(ProductObj__c productObj : Trigger.New) {
ProductObj__c oldProductObj = Trigger.oldMap.get(productObj.id);
if (productObj.Status__c != oldProductObj.Status__c ) {
productObj.Is_Updated__c = true;
}
}
}
Apexバッチで集計関数
//バッチクラス
global class SampleAggregateBatch implements Database.Batchable<AggregateResult> {
// The batch job starts
global Iterable<AggregateResult> start(Database.BatchableContext bc){
String query = 'SELECT COUNT(Id) cnt, AccountId FROM Contact GROUP BY AccountId';
return new SearchResultIterable(query);
}
// The batch job executes and operates on one batch of records
global void execute(Database.BatchableContext bc, List<sObject> scope){
for(sObject sObj : scope) {
AggregateResult ar = (AggregateResult)sObj;
System.debug('>>>> COUNT : ' + ar.get('cnt'));
}
}
// The batch job finishes
global void finish(Database.BatchableContext bc){ }
}
//インタフェースIterable Apex class
public class SearchResultIterable implements Iterable<AggregateResult>{
private String query;
public SearchResultIterable(String soql){
this.query = soql;
}
public Iterator<AggregateResult> Iterator(){
return new SearchResultIterator(this.query);
}
}
//インタフェースIterator
public class SearchResultIterator Implements Iterator<AggregateResult>{
public AggregateResult [] results {get;set;}
public Integer index {get;set;}
public SearchResultIterator(String query){
this.index = 0;
results = Database.query(query);
}
public boolean hasNext(){
return results !=null && !results.isEmpty() && index < results.size();
}
public AggregateResult next(){
return results[index++];
}
}
// Run batch apex
SampleAggregateBatch batch = new SampleAggregateBatch();
Database.executebatch(batch, 200);
// Debug log output
>>>> COUNT : 1
>>>> COUNT : 3
>>>> COUNT : 2
SOQL文のHAVING
HAVINGではGROUP BYでグルーピングした結果に対して、絞込みの条件を入れることが出来ます。
絞込み条件という意味ではWHEREと似ていますが、WHEREではグルーピングする前、HAVINGはグルーピングした後です。
次のサンプルは商談のリードソースでグルーピングし、同じ値のレコード数が1以上のリードソース別金額を表示します。
AggregateResult[] results = [SELECT LeadSource, SUM(Amount) summary
FROM Opportunity
GROUP BY LeadSource
HAVING Count(LeadSource) > 1];
for(AggregateResult ar: results){
System.debug('LeadSource='+ ar.get('LeadSource')
+ ':Amount='+ ar.get('summary'));
}
デバッグの結果
LeadSource=LeadSource1:Amount=2
LeadSource=LeadSource2:Amount=3
日本時間に合わせることで共有します。
時型の項目はTIMEVALUE関数で時間型に変換できます。
日本時間に合わせるために9時間ずらす必要があります。
「 + 3600000 * 9」で9時間ずらすことができます。
HOUR( TIMEVALUE(CreatedDate) + 3600000 * 9)