Я пытаюсь обновить существующий код (написанный кем-то другим, и я не разработчик - просто пытаюсь помочь другу), чтобы он использовал «файлы» вместо старых «вложений».
Триггер состоит в том, чтобы: когда критерии были выполнены в Opportunity, отправить электронное письмо и включить соответствующий файл из этой возможности.
Когда я проверяю его, я получаю следующую ошибку: sendAttachments: выполнение AfterUpdate, вызванное: System.VisualforceException: получение содержимого из триггеров в настоящее время не поддерживается. Trigger.sendAttachments: строка 172, столбец 1
Я нашел с помощью поиска Google, что это может быть решено, если вы используете "@future (callout = true) аннотацию перед методом". Неважно, где я поместил это в коде - я не могу заставить его скомпилировать без ошибок. Хотите знать, потому что логика c все в триггере, а не в классе? Любая помощь приветствуется.
Вот код:
trigger sendAttachments on Opportunity (after update,after insert) {
for(Opportunity opp : trigger.new){
if(opp.Ready_for_Billing__c == true && opp.SenttoBilling__c == false){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
opp = [SELECT id, account.name,account.parent.name, bundledamt__c,SO_Number__c, IO_Number__c, OwnerId, Type, Contract_Start_Date__c, Expiration_Date__c,
Is_Insertion_Order__c, StageName, Note_to_Billing__c, Purchase_Order_Number__c,Billing_Country__c, Billing_Contact__c, Billing_Address__c, Bill_Email__c, Name FROM Opportunity WHERE Id = :opp.Id];
User u = [SELECT FirstName, LastName, Email FROM User WHERE Id = :opp.OwnerId];
Contact DC = [SELECT FirstName, LastName, Email FROM Contact WHERE LastName = :'Delivery-Invoices'];
Contact MC = [SELECT FirstName, LastName, Email FROM Contact WHERE LastName = :'Media-Invoices'];
Contact CC = [SELECT FirstName, LastName, Email FROM Contact WHERE LastName = :'CC-Invoices'];
String Name;
if (opp.Is_Insertion_Order__c == True){
Name = opp.IO_Number__c;
}
else{
Name = opp.SO_Number__c;
}
String AccName = opp.account.name;
String subject;
String repname = u.FirstName + ' ' + u.LastName;
String ordertype;
String owneremail = u.Email;
String notes = opp.Note_to_Billing__c;
String othernotes;
if( (opp.Billing_Country__c == 'India') || (opp.Billing_Country__c == 'Egypt')||(opp.Billing_Country__c == 'China')||(opp.Billing_Country__c == 'Pakistan' ) ||
(opp.Billing_Country__c == 'Indonesia') || (opp.Billing_Country__c == 'Viet Nam')||(opp.Billing_Country__c == 'Thailand')||(opp.Billing_Country__c == 'Malaysia' ) ||
(opp.Billing_Country__c == 'United Arab Emirates') || (opp.Billing_Country__c == 'Singapore')||(opp.Billing_Country__c == 'Poland')||(opp.Billing_Country__c == 'Romania' )){
othernotes = 'IMPORTANT: Note that invoice is going to a country that may withhold taxes. Refer to history'+'\r\n';
}else{
othernotes = '';
}
// create email content
if (opp.Type == null){
ordertype = 'Not Available';
}
else{
ordertype = opp.Type;
}
String contractdates;
if (opp.Contract_Start_Date__c == null){
if(opp.Expiration_Date__c == null){
contractdates = 'Not Available';
}
else{
contractdates = 'Expiration date - '+ opp.Expiration_Date__c.month()+ '/' + opp.Expiration_Date__c.day() +'/' +opp.Expiration_Date__c.year();
}
}
else{
if(opp.Expiration_Date__c == null){
contractdates = 'Start Date - ' + opp.Contract_Start_Date__c.month()+'/'+opp.Contract_Start_Date__c.day()+'/'+ opp.Contract_Start_Date__c.year();
}
else{
contractdates = opp.Contract_Start_Date__c.month()+'/'+opp.Contract_Start_Date__c.day()+'/'+ opp.Contract_Start_Date__c.year()+' - ' + opp.Expiration_Date__c.month()+'/'+opp.Expiration_Date__c.day()+'/'+opp.Expiration_Date__c.year();
}
}
subject = 'Ready for Billing: SO/IO '+Name+ ' for ' + AccName;
List<String> ccaddress;
if (CC != Null){
ccaddress = new List<String>{CC.Email, owneremail};
}
Else{
ccaddress = new List<String>{owneremail};
}
email.setSubject(subject);
String salutation = 'Hello Accounting, '+ '\r\n'+ '\r\n';
String line1 = 'A New order is ready to be billed: '+ '\r\n';
String line2;
if (opp.Is_Insertion_Order__c == False){
line2 = ' SO #: ' + Name + '\r\n';
}
else{
line2 = ' IO #: ' + Name + '\r\n';
}
String line3 = ' Account: '+AccName+ '\r\n';
String line4;
if (opp.Is_Insertion_Order__c == False){
line4 = ' Order Type (renewal/upsell/new): '+ ordertype + '\r\n';
}
else{
line4 = '\r\n';
}
String line5 = ' Contract Dates: '+ contractdates + '\r\n'+ '\r\n';
String line6 = '\r\n'+ '\r\n'+ 'Regards,' + '\r\n';
String line7 = repname + '\r\n' + '\r\n';
String body = salutation + line1 + line2 + line3 + line4+ line5;
string BillingNotes;
string BillingNotes2;
if (opp.Is_Insertion_Order__c == True){
if ( opp.Purchase_Order_Number__c != Null){
BillingNotes2 = 'Include PO#: ' + opp.Purchase_Order_Number__c + '\r\n';
}
else{
BillingNotes2 ='\r\n';
}
if( opp.Billing_Contact__c != Null) {
Contact bc = [SELECT Name FROM Contact WHERE Id = :opp.Billing_Contact__c];
BillingNotes = 'Bill To: ' + bc.Name + '\r\n';
BillingNotes = BillingNotes + ' ' + opp.Bill_Email__c + '\r\n';
if ( opp.Purchase_Order_Number__c != Null){
BillingNotes = BillingNotes2 + BillingNotes;
}
}
if( opp.Billing_Address__c != Null) {
if( opp.Billing_Contact__c != Null){
BillingNotes = BillingNotes + ' ' + opp.Billing_Address__c.replaceAll('<br>',', ') + '\r\n' + '\r\n';
}
else {
BillingNotes = 'Billing Address: ' + opp.Billing_Address__c.replaceAll('<br>',', ') + '\r\n' + '\r\n';
}
}
else{
BillingNotes = BillingNotes + '\r\n' + '\r\n';
}
}
if (opp.Note_to_Billing__c != Null){
string cleannotes = opp.Note_to_Billing__c.replaceAll('<br>','\r\n');
cleannotes = cleannotes.replaceAll('<[^>]+>',' ');
if (BillingNotes != Null){
BillingNotes = othernotes + '\r\n' + BillingNotes + cleannotes;
}
else{
BillingNotes = othernotes + '\r\n' + cleannotes;
}
body = body + 'Notes: '+ '\r\n' + BillingNotes;
}
if (opp.bundledamt__c > 0){
OpportunityLineItem[] bundledItems = [SELECT Id, Opportunityid, PricebookEntry.Name, TotalPrice, Quantity, UnitPrice, Description, Bundled__c
from OpportunityLineItem where (Bundled__c =: 'Yes' AND Opportunityid =: opp.Id) ];
body = body + '\r\n' + '\r\n' + 'Breakdown of bundled products:' + '\r\n';
for (OpportunityLineItem oli : bundledItems){
body = body + oli.Quantity.round() + ' ' + oli.PricebookEntry.Name + ' (' + oli.Description + '); Price: ' + oli.UnitPrice+ '; Total: ' + oli.TotalPrice + '\r\n';
}
}
body = body + line6+ line7;
email.setPlainTextBody(body);
if(opp.Is_Insertion_Order__c == true){
email.setToAddresses(new String[]{MC.Email});
}
else if( opp.account.parent.name == 'XXX Group' && (opp.Contract_Start_Date__c.daysBetween(opp.Expiration_Date__c))<32) {
email.setToAddresses(new String[]{MC.Email});
}
else{
email.setToAddresses(new String[]{DC.Email});
}
email.setCcAddresses(ccaddress);
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
PageReference pref = page.attachIOPDFforBilling;
/*for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :opp.Id]){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();*/
for (ContentDocumentLink a : [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink where LinkedEntityId=: opp.id]){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
//efa.setFileName(a.Title);
efa.setBody(pref.getContentAsPDF());
fileAttachments.add(efa);
}
email.setFileAttachments(fileAttachments);// Send email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
}
}
}