Triggers Salesforce错误:当前不支持从触发器内获取内容

Triggers Salesforce错误:当前不支持从触发器内获取内容,triggers,salesforce,apex,Triggers,Salesforce,Apex,我正在尝试更新现有的代码(由其他人编写,我不是开发人员,我只是想帮助一个朋友),以便它使用“文件”而不是旧的“附件” 触发条件是:当Opportunity满足条件时,发送电子邮件并包含Opportunity中的相关文件 当我测试它时,我得到以下错误: sendAttachments:执行AfterUpdate的原因是:System.VisualforceException:当前不支持从触发器内获取内容。Trigger.sendAttachments:第172行第1列 我在谷歌搜索中发现,如果在方

我正在尝试更新现有的代码(由其他人编写,我不是开发人员,我只是想帮助一个朋友),以便它使用“文件”而不是旧的“附件”

触发条件是:当Opportunity满足条件时,发送电子邮件并包含Opportunity中的相关文件

当我测试它时,我得到以下错误: sendAttachments:执行AfterUpdate的原因是:System.VisualforceException:当前不支持从触发器内获取内容。Trigger.sendAttachments:第172行第1列

我在谷歌搜索中发现,如果在方法之前使用“@future(callout=true)注释”,就可以解决这个问题。不管我把它放在代码的什么地方——我不能让它编译时没有错误。想知道是不是因为逻辑都在触发器中而不是类中? 感谢您的帮助

代码如下:

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 });

        }

    }

}
trigger sendAttachments on Opportunity(更新后、插入后){
for(Opportunity opp:trigger.new){
if(opp.Ready\u for\u Billing\u c==真&opp.SenttoBilling\u c==假){
Messaging.SingleEmailMessage email=新建消息。SingleEmailMessage();
opp=[选择id、account.name、account.parent.name、bundledamt\uuuuuuuu c、SO\u编号\uuuuu c、IO\u编号\uuuuu c、所有者id、类型、合同\u开始日期\uuuuu c、到期日期\uuu c、,
是插入、订单、阶段名称、备注、账单、采购订单号、账单国家、账单联系人、账单地址、账单电子邮件、商机名称,其中Id=:opp.Id];
用户u=[选择名字、姓氏、来自用户的电子邮件,其中Id=:opp.OwnerId];
联系人DC=[选择名字、名字、来自联系人的电子邮件,其中名字=:'Delivery-Invoices'];
联系人MC=[选择姓氏、姓氏、来自联系人的电子邮件,其中姓氏=:'Media-Invoices'];
联系人CC=[选择姓氏、姓氏、来自联系人的电子邮件,其中姓氏=:'CC-Invoices'];
字符串名;
if(opp.Is\u Insertion\u Order\uu c==True){
名称=opp.IO\U编号\U\c;
}
否则{
名称=opp.SO\U编号\uu\c;
} 
字符串AccName=opp.account.name;
字符串主题;
字符串repname=u.FirstName+“”+u.LastName;
字符串顺序类型;
字符串owneremail=u.Email;
String notes=opp.Note\u to\u Billing\c;
串音符;
如果(印度)(埃及)(中国)(巴基斯坦)||
(opp.Billing_Country___c==‘印度尼西亚’)(opp.Billing_Country__c==‘越南’)(opp.Billing_Country__c==‘泰国’)(opp.Billing_Country___c==‘马来西亚’)||
(opp.Billing_Country__c==‘阿拉伯联合酋长国’)(opp.Billing_Country__c==‘新加坡’)(opp.Billing_Country__c==‘波兰’)(opp.Billing_Country__c==‘罗马尼亚’){
othernotes='重要提示:请注意,发票将寄往可能扣缴税款的国家/地区。请参阅历史记录'+'\r\n';
}否则{
其他注释=“”;
}
//创建电子邮件内容
if(opp.Type==null){
ordertype='不可用';
}
否则{
订单类型=对开类型;
} 
字符串日期;
如果(opp.Contract\u Start\u Date\u c==null){
如果(opp.Expiration\u Date\u c==null){
合同日期='不可用';
}
否则{
合同日期='到期日-'+opp.Expiration\u date\u c.month()+'/'+opp.Expiration\u date\u c.day()+'/'+opp.Expiration\u date\u c.year();
}
}
否则{
如果(opp.Expiration\u Date\u c==null){
合同日期='开始日期-'+opp.Contract\u Start\u Date\u c.month()+'/'+opp.Contract\u Start\u Date\u c.day()+'/'+opp.Contract\u Start\u Date\u c.year();
}
否则{
合同日期=opp.Contract\u Start\u Date\u c.month()+'/'+opp.Contract\u Start\u Date\u c.day()+'/'+opp.Contract\u Start\u Date\u c.year()+'+opp.Expiration\u c.month()+'/'+opp.Expiration\u c.day()+'/'+opp.Expiration\u-Date\u.year();
}
} 
subject='准备计费:SO/IO'+Name+'表示'+AccName;
列出地址;
如果(CC!=Null){
ccaddress=新列表{CC.Email,owneremail};
}
否则{
ccaddress=新列表{owneremail};
}
email.setSubject(主题);
字符串称呼='Hello Accounting'+'\r\n'+'\r\n';
字符串line1='新订单已准备好开票:'+'\r\n';
弦线2;
if(opp.Is\u Insertion\u Order\uu c==False){
第2行='SO#::'+Name+'\r\n';
}
否则{
第2行='IO#::'+Name+'\r\n';
} 
字符串行3='帐户:'+AccName+'\r\n';
弦线4;
if(opp.Is\u Insertion\u Order\uu c==False){
第4行='订单类型(续订/追加销售/新建):'+ordertype+'\r\n';
}
否则{
第4行='\r\n';
} 
字符串行5='合同日期:'+contractdates+'\r\n'+'\r\n';
字符串行6='\r\n'+'\r\n'+'表示'+'\r\n';
字符串line7=repname+'\r\n'+'\r\n';
字符串体=称呼+第1行+第2行+第3行+第4行+第5行;
弦乐;
字符串BillingNotes2;
if(opp.Is\u Insertion\u Order\uu c==True){
trigger  sendAttachments on Opportunity (after update,after insert) {
    Set<Id> opportunitiesToSend = new Set<Id>();
    for(Opportunity opp : trigger.new){
        if(opp.Ready_for_Billing__c == true &&  opp.SenttoBilling__c == false){
            opportunitiesToSend.add(opp.Id);
        }
    }
    if(!opportunitiesToSend.isEmpty()){
        sendEmails(opportunitiesToSend);
    }

    // it looks insane but it works, a method defined straight in a trigger.
    // to do it properly - you should have another class you're calling
    @future (callout=true)
    static void sendEmails(Set<Id> opportunityIds){
        Map<Id, Opportunity> opps = [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 IN :opportunityIds]);
        for(Id i : opportunityIds){
            opp = opps.get(i);
            // put rest of the code here
            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;
            // ...
        }
    }
}