1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

項目として運用しているメモフィールドを拡張メモとしてセットする

Posted at

拡張メモもContentDocumentと同じでContentDocumentLinkで対象のオブジェクトと関連付けないといけないので、データローダなどではできなくはないけど、面倒だろう。

image.png

とりあえず、1レコードずつ処理する簡単なバージョンをさくっと作ってみた。

public class BAT_Create_ContentNote implements Database.Batchable<sObject> {

    public Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
        String query = 'Select Id,Name,Note1__c,Note2__c,Note3__c From Opportunity where Id =\'0066D000003595SQAQ\'';
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext BC, List<Opportunity> oppList) {
        System.debug('******** oppList ' + oppList);
        List<ContentDocumentLink> cdlinkList = new List<ContentDocumentLink>();
        for (Opportunity o : oppList){
            if(o.Note1__c != null ){
                ContentNote cn = new ContentNote();
                cn.Title = 'test1';
                String body = o.Note1__c;
                cn.Content = Blob.valueOf(body.escapeHTML4());
                insert(cn);
                ContentDocumentLink cdl = new ContentDocumentLink(); 
                cdl.LinkedEntityId = o.id;
                cdl.ShareType = 'V';
                //ContentVersion cv2 = cvMap.get(cv.Id);
                //Id cdId = cv2.ContentDocumentId;
                cdl.ContentDocumentId = cn.Id;
                cdlinkList.add(cdl);
            }
            if(o.Note2__c != null ){
                ContentNote cn = new ContentNote();
                cn.Title = 'test1';
                String body = o.Note2__c;
                cn.Content = Blob.valueOf(body.escapeHTML4());
                insert(cn);
                ContentDocumentLink cdl = new ContentDocumentLink(); 
                cdl.LinkedEntityId = o.id;
                cdl.ShareType = 'V';
                //ContentVersion cv2 = cvMap.get(cv.Id);
                //Id cdId = cv2.ContentDocumentId;
                cdl.ContentDocumentId = cn.Id;
                cdlinkList.add(cdl);
            }

            if(o.Note3__c != null ){
                ContentNote cn = new ContentNote();
                cn.Title = 'test1';
                String body = o.Note3__c;
                cn.Content = Blob.valueOf(body.escapeHTML4());
                insert(cn);
                ContentDocumentLink cdl = new ContentDocumentLink(); 
                cdl.LinkedEntityId = o.id;
                cdl.ShareType = 'V';
                //ContentVersion cv2 = cvMap.get(cv.Id);
                //Id cdId = cv2.ContentDocumentId;
                cdl.ContentDocumentId = cn.Id;
                cdlinkList.add(cdl);
            }
        }

        try {       
            if(cdlinkList.size() > 0) insert cdlinkList;
        } catch(Exception e) {
            System.debug(e);
        }

    }  
    public void finish(Database.BatchableContext BC) {
    }
}
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?