function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Felix QuinonesFelix Quinones 

Why my ApexTrigger is not working? Error: sendPdfAsAttachement.

Hello All, 

I'm trying to automatically send a PDFPage as an attachment every time a new grant opportunity is created or StageName == 'Vetting'. 

The following is my ApexTrigger. 

trigger SendPDFOpportunity on Opportunity (after insert, after update) {
  // Check if the opportunity is newly created or marked as vetting
  for (Opportunity opp : Trigger.new) {
    if ((opp.IsClosed && opp.IsWon) || opp.StageName == 'Vetting') {
      // Send the PDF as an attachment
      sendPdfAsAttachment(opp.Id);
    }
  }
}

private void sendPdfAsAttachment(Id opportunityId) {
  // Generate the PDF
  PageReference pdfPage = Page.NewGrantOpportunity;
  pdfPage.getParameters().put('id', opportunityId);
  Blob pdfBlob = pdfPage.getContent();

  // Create the attachment
  Attachment pdfAttachment = new Attachment();
  pdfAttachment.Name = 'Opportunity.Name + '.pdf';
  pdfAttachment.Body = pdfBlob;
  pdfAttachment.ContentType = 'application/pdf';
  pdfAttachment.ParentId = opportunityId;
  insert pdfAttachment;
}

It is currently showing errors in line 6 and 11. 

Line 6: Method does not exist or incorrect signature: void sendPdfAsAttachment(Id) from the type SendPDFOpportunity

Line 11: Missing '<EOF>' at 'private'

I have been trying everything I could. Any recommendations for me? 

 

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you confirm in which class is the method sendPdfAsAttachment present?

Is it on same Trigger? If so this is not correct way. You have to create a class and add this method there and call that from Trigger.

Thanks,
 
Felix QuinonesFelix Quinones

Hello Sai, 

Thank you for your comment. 

That is what I think I did after reviewing the code.
I currently have visualforce page, an apex class, and an apex trigger. However, now I'm recevinf the following error: 

We hit a snag.
Review the errors on this page.
SendPDFOpportunity: execution of AfterInsert caused by: System.VisualforceException: Getting content from within triggers is currently not supported. Class.SendPDFAttachment.sendPdfAsAttachment: line 7, column 1 Trigger.SendPDFOpportunity: line 6, column 1


What I'm doing wrong? 


My Apex Codes are below. 

Visualforce page named: NewrantOpportunity

<apex:page standardController="Opportunity" renderAs="pdf" contentType="application/pdf#Opportunity-{!Opportunity.Name}.pdf">
  <head>
    <style>
      .center-image {
        display: flex;
        justify-content: center;
        width: 40px;
        height: 50px;
      }
      .label-column {
        font-weight: bold;
        text-align: left;
      }
      .value-column {
        text-align: right;
      }
    </style>
  </head>
  <table width="100%">
    <tr>
      <td width="20%">
        <div align="left" class="center-image">
          <apex:image url="{!$Resource.companyLogo}" alt="Company Logo" width="60%"/>
        </div>
      </td>
      <td width="80%">
        <div align="right" style="color: #007dc3;">
          <apex:sectionHeader title="New Grant Opportunity"/>
        </div>
    </td>
  </tr>
</table>    
    
<h2><u>Funding Opportunity Detail</u></h2>
  <br />
    
<table width="100%">
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Opportunity ID:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="opportunityId" value="{!Opportunity.Id}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Opportunity Name:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="opportunityName" value="{!Opportunity.Name}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Description:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="description" value="{!Opportunity.RecordType.Description}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Notification Date:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="notificationDate" value="{!Opportunity.Notification_Date__c}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Deadline (Close Date):</td>
    <td width="70%" class="value-column">
      <apex:outputField id="closeDate" value="{!Opportunity.CloseDate}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Funding Area:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="fundingArea" value="{!Opportunity.npsp__Grant_Program_Area_s__c}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Award Amount:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="awardAmount" value="{!Opportunity.Award_Amount__c}"/>
    </td>
  </tr>
</table>
    
<h2><u>Account Information</u></h2> 
 
<table width="100%">
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Funder Name:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="funderName" value="{!Opportunity.Account.Name}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Funder Area:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="funderArea" value="{!Opportunity.Programs__c}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Type:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="type" value="{!Opportunity.Account.Type}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Website:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="website" value="{!Opportunity.npsp__Grant_Requirements_Website__c}" />
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Application Website:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="application" value="{!Opportunity.Application_Link__c}" />
    </td>
  </tr>
</table>
    
    
<h2><u>Program Design and Budget Information</u></h2>
  <apex:outputLabel value="Grant Eligibility: " for="grantEligibility" style="font-weight:bold;"/> 
  <apex:outputField id="grantEligibility" value="{!Opportunity.Grant_Eligibility__c}"/>
  <br /><br />
  <apex:outputLabel value="Program Goals: " for="programGoals" style="font-weight:bold;"/> 
  <apex:outputField id="programGoals" value="{!Opportunity.Grant_Goals__c}"/>
  <br /><br />
  <apex:outputLabel value="Use of Funds: " for="useOfFunds" style="font-weight:bold;"/> 
  <apex:outputField id="useOfFunds" value="{!Opportunity.Use_of_Funds__c}"/>
  <br />
  
<h2><u>Strategic Information</u></h2>
<table width="100%">
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Funding Source:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="fundingSource" value="{!Opportunity.Funding_Source__c}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Strategic Alignment:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="strategicAlignment" value="{!Opportunity.Strategic_Alignment__c}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Fiscal Year:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="fiscalYear" value="{!Opportunity.Fiscal_Year__c}"/>
    </td>
  </tr>
  <tr>
    <td width="30%" style="font-weight: bold; text-align: left" class="label-column">Stage:</td>
    <td width="70%" class="value-column">
      <apex:outputField id="Stage" value="{!Opportunity.StageName}" />
    </td>
  </tr>
</table>
    
<h2><u>Vetting Process</u></h2>
  <apex:outputLabel value="Select the checkmark if the opportunity was reviewed and was approved as prospecting: " for="vetted" style="font-weight:bold;"/>   
  <apex:outputField id="vetted" value="{!Opportunity.Vetted_Grant__c}"/>
</apex:page>

I have an Apex Class named "SendPDFAttachment"

// Apex class to generate and send the PDF as an attachment
public class SendPDFAttachment {

    // This method will generate the PDF and create an attachment for the given opportunity
public static void sendPdfAsAttachment(Id opportunityId) {

	// Generate the PDF
    PageReference pdfPage = Page.NewGrantOpportunity;
	pdfPage.getParameters().put('id', opportunityId);
    Blob pdfBlob = pdfPage.getContentAsPDF();

    // Create the attachment
    Attachment pdfAttachment = new Attachment();
    pdfAttachment.Name = 'Opportunity.Name' + '.pdf';
    pdfAttachment.Body = pdfBlob;
    pdfAttachment.ContentType = 'application/pdf';
    pdfAttachment.ParentId = opportunityId;
    insert pdfAttachment;
  }
}
Finally, I have an Apex Trigger named "SendPDFOpportunity"
// Trigger to send the PDF when an opportunity is created or marked as vetting
trigger SendPDFOpportunity on Opportunity (after insert, after update) {
// Check if the opportunity is newly created or marked as vetting
for (Opportunity opp : Trigger.new) {
if ((opp.IsClosed && opp.IsWon) || opp.StageName == 'Vetting') {
// Send the PDF as an attachment
SendPDFAttachment.sendPdfAsAttachment(opp.Id);
}
}
}