0
0

More than 3 years have passed since last update.

拡張メモの内容を取引先オブジェクトに更新する2

Posted at

前回の続きです。

どうやってもうまく行かないので、タイミングをずらす戦略に出ます。

取引先オブジェクトにはContentNoteのIdだけを記録して、取引先のAfter Updateトリガーで拡張メモを読みに行って
値を取得させる。

これなら、大丈夫じゃないのと超期待。

trigger NoteToAccount3 on Account (after update) {
    System.debug('############ NoteToAccount3 ##################');
    Map<Id, Account> aOldMap = new Map<Id, Account>();
    if (Trigger.Old != null) {
        aOldMap = new Map<Id, Account>(Trigger.Old);
    } else {
        aOldMap = new Map<Id, Account>();
    }

    Set<Id> ContentIdSet = new Set<Id>();
    for(Account accNew : Trigger.New){
        Account accOld = aOldMap.get(accNew.Id);
        if (accOld != null) {
            if (accOld.ContentId__c != accNew.ContentId__c) {
                if (accNew.ContentId__c != null) ContentIdSet.add(accNew.ContentId__c);
            }
        }
    }

    Map<Id,ContentNote> ContentNoteMap = new Map<Id,ContentNote>();
    List<ContentNote> ContentNoteList = [SELECT Id,Title,TextPreview,CreatedDate,Content FROM ContentNote WHERE ID =: ContentIdSet];
    for (ContentNote cn :ContentNoteList){
        ContentNoteMap.put(cn.Id,cn);
    }

    List<Account> updateAccountList = new List<Account>();
    for(Account accNew : Trigger.New){
        ContentNote cn = ContentNoteMap.get(accNew.ContentId__c);
        if (cn != null){
            Account acc = new Account();
            acc.Id = accNew.Id;
            acc.TextPreview__c = cn.Content.toString();
            System.debug('############ acc ##################' + cn.Content.toString());// NG null
            updateAccountList.add(acc);

        }
    }

    if (updateAccountList.size() > 0) update updateAccountList;    
}

結果はダメですねぇ。何でだろう。開発者コンソールでSOQLたたくとちゃんと値があるのに
トリガーでは取得できない。

image.png

じゃぁ、Anonymous windowsではどうだ。

image.png

ちゃんと値が取れますねぇ。2段階のトリガーでもタイミングが早いってこと?

次に考えられるのは、取引先の更新をApexバッチにしてもっとタイミングを遠くすることかな。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0