Trigger๋ฅผ ์ฌ์ฉํ ์ฒญ๊ตฌ์ ์๋ ์ด๋ฉ์ผ ์ ์ก
Trigger๋ฅผ ์ฌ์ฉํ์ฌ Salesforce์ Quote ๊ฐ์ฒด์ Status ๊ฐ์ด Accepted๊ฐ ๋์์ ๋ Document Builder๋ก ์์ฑํ ๋ฌธ์[์ฒญ๊ตฌ์]๋ฅผ email๋ก ์ ์กํ๋ ์์
Quote ๊ฐ์ฒด Status ๊ฐ์ด Accepted๊ฐ ๋์์ ๋ ํจ์๋ฅผ ํธ์ถํ๋ ์์
trigger Quote on Quote (after update) {
public List<Quote> newObjList {get; set;}
public Map<Id, Quote> oldObj {get; set;}
newObjList = (List<Quote>)Trigger.new;
oldObj = (Map<Id, Quote>)Trigger.oldMap;
if (trigger.isAfter && trigger.isUpdate){
for(Quote qu : trigger.new){
if (qu.Status == 'Accepted' && qu.Status != oldObj.get(qu.id).Status){
// template Id์ recordId๋ฅผ parameter ๊ฐ์ผ๋ก ๋๊ฒจ์ค๋ค.
String templateId = DocumentCustomController.getTemplateId();
DocumentCustomController.triggeredSavePdf(qu.Id, templateId);
}
}
}
}
Document Builder Template ์ด๋ฆ ๊ธฐ์ค์ผ๋ก template Id๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์ ์์
public static String getTemplateId() {
String templateId = [SELECT Id
FROM SBLD__DocumentBuilder__c
WHERE name = 'BillingStatement'
WITH SECURITY_ENFORCED
ORDER BY CreatedDate DESC
LIMIT 1][0].Id;
return templateId;
}
์ฌ์ฉ์์๊ฒ ์ด๋ฉ์ผ์ ๋ณด๋ด๋ ํจ์ ์์
public static void sendEmailToUser( List<String> listAttachement) {
// ์ด๋ฉ์ผ ๊ธฐ๋ณธ์ ๋ณด ์ถ๊ฐ
Messaging.SingleEmailMessage clsSingleEmailMessage = new Messaging.SingleEmailMessage();
clsSingleEmailMessage.setToAddresses(new List<String>{'abc@example.com'});
clsSingleEmailMessage.setSubject('Email Subject');
clsSingleEmailMessage.setPlainTextBody('Email Body');
// attachement ์ถ๊ฐ
ContentVersion clsContentVersion = [SELECT VersionData, PathOnClient FROM ContentVersion WHERE ContentDocumentId = :listAttachement.get(0) WITH SECURITY_ENFORCED][0];
Messaging.EmailFileAttachment clsEmailFileAttachment = new Messaging.EmailFileAttachment();
clsEmailFileAttachment.setFileName(clsContentVersion.pathOnClient);
clsEmailFileAttachment.setBody(clsContentVersion.versionData);
clsSingleEmailMessage.setFileAttachments(new Messaging.EmailFileAttachment[] {clsEmailFileAttachment});
// ์ด๋ฉ์ผ ์ ์ก
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{clsSingleEmailMessage});
}
Document Template์ PDF๋ก ์ ์ฅํ๋ ์์
// Document Template PDF ์ ์ฅ
@future(callout = true)
public static void triggeredSavePdf(String recordId, String templateId) {
// PDF ์์ฑ
PageReference pdfPage = new PageReference('/apex/SBLD__DocumentBuilderTemplate?recordId=' + templateId + '&targetId=' + recordId);
Blob pdfContent;
if(Test.isRunningTest()) {
pdfContent = blob.valueOf('Unit.Test');
}else {
pdfContent = pdfPage.getContentAsPDF();
}
String title = 'BillingStatement.pdf';
// ContentVersion ์ถ๊ฐ
ContentVersion cv = new ContentVersion();
cv.VersionData = pdfContent;
cv.Title = title;
cv.PathOnClient = title;
cv.OwnerId = UserInfo.getUserId();
cv.ContentLocation = 'S';
insert cv;
// ContentDocumentLink ์ถ๊ฐ
Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id WITH SECURITY_ENFORCED ].ContentDocumentId;
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = conDocId;
cdl.LinkedEntityId = recordId;
cdl.ShareType = 'V';
cdl.Visibility = 'AllUsers';
if (Schema.sObjectType.ContentDocumentLink.fields.ContentDocumentId.isCreateable()) {
Database.insert(cdl);
}
// Attachement๊ฐ ์ถ๊ฐ๋ email ์ ์ก ๋ฉ์๋ ํธ์ถ
listAttachement = new List<String>{conDocId};
sendEmailToUser(listAttachement);
}