0

I am sending a pdf in an email when the opportunity is created on updated which works fine. Now when the opportunity is created through batch so its throws the error future method can not call future method because I use @future (callout= true) in the handler class. when I remove @future from the handler method so it also throws the error Getting content from within triggers is currently not supported. I also tried to use if(System.IsBatch() == false && System.isFuture() == false) then trigger can not executed.What is the best way to solve the problem like its works when I create a single opportunity without a batch? How to solve this while using batch?

 if(trigger.isAfter && trigger.isInsert){ 
            list<opportunity> oppList = [select id, name ,Email_Sending_Option__c,Language__c,Template__c,Email__c,
                                         Email_Date__c,Email_Status__c from opportunity where id  =: trigger.new ];
            for(Opportunity opp:oppList) {
            if(opp.Template__c != null &amp;&amp; opp.Language__c != null){
                if(opp.Email_Sending_Option__c == 'Send Now'){ 
                    opp.Email_Date__c = date.today();
                    opp.Email_Status__c = 'Email Sent';
                    updateOpp.add(opp);
                    if(System.IsBatch() == false &amp;&amp; System.isFuture() == false){ 
                        PDFGenerationService.generatePDF1(opp.Template__c+'.pdf',opp.Email__c,opp.Id,opp.Template__c,opp.Language__c);
                    }

Apex classs


public class PDFGenerationService {
    @future (callout=true)
    public static void generatePDF(string filename, string email,ID OpportunityId,string pageName,string lang )
    {
        if(lang == 'English'){
            EmailTemplate coverTemplate = EmailTemplateSelector.getEmailTemplate('Development And Peace');
            Map<String, String> ncParams=new Map<String, String> {'{name}' => 'name'};
                Messaging.EmailFileAttachment attach = EmailHelper.attachementBuilder(filename,OpportunityId,pageName,lang);
            Messaging.SingleEmailMessage mail=EmailHelper.emailBuilder(email,coverTemplate.Subject,coverTemplate.HtmlValue,ncParams);
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach}); 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
relcey
  • 13
  • 1
  • 6
  • Hi and welcome to SFSE. Is there a specific question here? Please [edit] to state the specific issue you face with the code you have posted. I assume the issue is that with the condition where you test for batch or future is that this then prevents the sending, which you do actually want to happen...? – Phil W Jun 21 '22 at 06:54
  • how to call batch or future to the future method
  • – relcey Jun 21 '22 at 07:01
  • how to solve the problem. – relcey Jun 21 '22 at 07:02
  • You need to separate out the sending from the trigger itself. There are several options. One is to publish a platform event from the trigger that indicates the record needs to have sending done and use an apex trigger based event subscriber to process the event and send the PDF. The problem here is that platform events are not guaranteed to be delivered. So instead, you could mark the record as needing to be processed and have a scheduled job that periodically looks for records that need to be processed, process them and clear the mark. The problem here is a delay. Other options exist. – Phil W Jun 21 '22 at 07:03
  • BTW, don't add comments in reply, [edit] your question to add the detail. – Phil W Jun 21 '22 at 07:05