見積PDF(QuoteDocument)のトリガ

By | March 8, 2022

結論

QuoteDocumentをApexTriggerの条件にすることはできないが、ContentDocumentLinkのApex Triggerで親オブジェクトの判定を付ければ、事実上同じ。

■Apex Trigger

trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {
	ContentDocumentLinkTriggerHandler.onAfterInsert(trigger.new);
}

■Trigger Handler(※見積レコードの項目情報を取らなくてよい場合)


public with sharing class ContentDocumentLinkTriggerHandler {
    public static Id templateId = [SELECT ID FROM EmailTemplate WHERE DeveloperName = 'QuoteDocumentNotification' LIMIT 1].Id;
    public static void onAfterInsert(list<ContentDocumentLink> cdlList){        
        String strObjPrefix;
        Set<Id> quoteIdSet = new Set<Id>();
        //作成されたファイルが見積に紐づくものであるかどうか判定
        for(ContentDocumentLink cdl: cdlList){
             strObjPrefix = String.valueOf(cdl.LinkedEntityId).substring(0, 3);
             if(strObjPrefix == Quote.sObjectType.getDescribe().getKeyPrefix()) {
		なんか処理書く	 
            }         
        }
    }
}

■Trigger Handler(※見積レコードの項目情報を取りたい場合)

public with sharing class ContentDocumentLinkTriggerHandler {
    public static Id templateId = [SELECT ID FROM EmailTemplate WHERE DeveloperName = 'QuoteDocumentNotification' LIMIT 1].Id;
    public static void onAfterInsert(list<ContentDocumentLink> cdlList){        
        String strObjPrefix;
        Set<Id> quoteIdSet = new Set<Id>();
        //作成されたファイルが見積に紐づくものであるかどうか判定
        for(ContentDocumentLink cdl: cdlList){
             strObjPrefix = String.valueOf(cdl.LinkedEntityId).substring(0, 3);
             if(strObjPrefix == Quote.sObjectType.getDescribe().getKeyPrefix()) {
				 quoteIdSet.add(cdl.LinkedEntityId);
            }         
        }
        
        List<Quote> quoteList = [SELECT Id,CreatedById,ApprovalStatus__c FROM Quote WHERE ID IN :quoteIdSet];
		

        for(Quote q: quoteList){
            if(q.CustomField__c == ~){
                なんかメソッド呼び出す
            }       
        }     
    }
}

■Test Class

@isTest
public class ContentDocumentLinkTriggerTest {
    @isTest
    static void testMethod1(){
        Account accA =new Account(Name='Demo123');
        insert accA ; 
        Opportunity opp = new Opportunity(Name='test opp',StageName='stage', AccountId = accA.Id ,Probability = 95, CloseDate=system.today());
        insert opp;       
        Quote q = New Quote(Name='Test Quote', OpportunityId=opp.Id,カスタム項目の値);
        insert q;
        ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        content.origin = 'H';
        insert content;
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=q.id;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        contentlink.ShareType = 'I';
        contentlink.Visibility = 'AllUsers'; 
        
        Test.startTest();
        insert contentlink;
        Test.stopTest();        
    }

}