• Dennis Wilson
  • NEWBIE
  • 10 Points
  • Member since 2015
  • Owner
  • Covidi Consulting LLC


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Hello all,

I have a requirement to send a VF page as a PDF attachment and also add the PDF attachment to the related Opportunity in Salesforce.  This needs to be done via a public facing Force.com site.  

The site has a VF page that is a form for a customer to fill out, and the submit button performs an update to the Salesforce records(Account, Contacts, Opportunity, and a Custom Object) with the customers input.  The generateContractPdf() method below is then called to fulfill the PDF requirement mentioned above, which is a PDF formatted version of the form the customer filled out.

I am able to fulfill both requirements when running as an Admin, but through the Force.com site, the PDF attachment is created but will not open.("Failed to load PDF" or "format error: not a PDF or corrupted" error).  

If I remove the @future annotation, the PDF loads fine, however, the updates made from the original VF page controller(customer input) are not reflected on the resulting PDF.

If I change getContent() to getContentAsPdf(), the PDF will open, but it is just a blank page.

Any help is greatly appreciated.
 
@future(callout=true) 
public static void generateContractPdf(String oppId, String contactId, String businessName, Boolean requestedEmail) {

        Blob body;
        String fileName = businessName.replaceAll(' ', '_').replaceAll(',', '_').replaceAll('//', '_').replaceAll('/', '_') + 
            + '_Service_Agreement_' 
            + System.now().format('MM/dd/yyyy HH:mm:ss','America/Denver')
            + '.pdf';

        try {

            PageReference pdf = new PageReference('/apex/ContractPDFVersion1');
			pdf.getParameters().put('id', oppId);
		    
            body = Test.isRunningTest() ? Blob.valueOf('Test') : pdf.getContent();

            ContractForm_Controller.sendAndAttachPdf(body, fileName, oppId, contactId, requestedEmail);

        } catch (VisualforceException e) {

            Error_Log__c error = new Error_Log__c(
                Error__c = 'There was an error creating the Contract PDF: ' + e,
                Error_Type__c = 'Contract PDF Creation Error',
                Opportunity__c = oppId);
            insert error;
        }
    }

    public static void sendAndAttachPdf(Blob body, String fileName, String oppId, String contactId, Boolean requestedEmail) {

        ContractEmailTemplate__c contractEmailTemplateID = ContractEmailTemplate__c.getValues('Services');

        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setContentType('application/pdf');
        attachment.setFileName(fileName);
        attachment.setInline(false);
        attachment.body = body;

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setUseSignature(false);
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment }); 
        mail.setTemplateId(contractEmailTemplateID.TemplateID__c);
        mail.setTargetObjectId(contactId);

        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setSubject('Thank you for your business!');
        message.setPlainTextBody('Thank you for signing up');
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment });
        message.setToAddresses(new String[] { 'example@example.com' });

        try {
            
            if(requestedEmail) {
                
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail,message });

            } else {
                
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { message });
            }
            

        } catch (Exception e) {
            
            Error_Log__c error = new Error_Log__c(
                Error__c = 'There was an error sending the Contract PDF email: ' + e.getMessage(),
                Error_Type__c = 'Contract PDF Email Error',
                Opportunity__c = oppId);
            insert error;
        }

        try {

            Attachment accountAttachment = new Attachment();
            accountAttachment.ParentId = oppId;
            accountAttachment.Name = fileName;
            accountAttachment.Body = body;
            insert accountAttachment;

        } catch (Exception e) {

            Error_Log__c error = new Error_Log__c(
                Error__c = 'There was an error attaching the Contract PDF to the Opportunity: ' + e,
                Error_Type__c = 'Contract PDF Attachment Error',
                Opportunity__c = oppId);
            insert error;
        }
    }

 
  • October 23, 2015
  • Like
  • 0

Hi All,

 

Can anyone spot why this would not be showing the inline / hover over help text?

 

The page is valid

 

Thanks

 

 

<apex:page standardController="Vacancy__c"  extensions="ExternalVacancyControllerExtension" title="Client Vacancy " showHeader="false" standardStylesheets="true">

  <apex:form > 
   <apex:pageMessages />
    <apex:messages />
      <apex:pageBlock title="" mode="edit">
        
        <apex:pageBlockButtons location="bottom" >
           <apex:commandButton value="Confirm" action="{!ConfirmUpdate}"/>
           <apex:commandButton value="Cancel" action="{!Cancel}"/>
        </apex:pageBlockButtons>
        
        <apex:pageBlockSection title="Vacancy Information" collapsible="false" columns="2">
            
            
               <apex:pageBlockSectionItem helpText="{!$ObjectType.Vacancy__c.Fields.Client_Contact_Hiring_Manager__c.inlineHelpText}" >
                    <apex:outputLabel value="Client Hiring Manager" for="Client_Contact_Hiring_Manager__c"/>
                    <apex:inputText value="{!Vacancy__c.Client_Contact_Hiring_Manager__c}" id="hiremgr"/> 
                </apex:pageBlockSectionItem>
            
            

        
                    
        </apex:pageBlockSection>
            
       </apex:pageBlock>
       
       
       
      </apex:form>
</apex:page>

 

 

I'm trying to copy a new user as contact by using a trigger but I'm getting the following error

 

MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Contact, original object: User

 

trigger Acceleration_User_Copyto_Contact on User (after insert, after update) {
    
   // Map<String,User> newLead = new Map <String, User>();
    
    system.debug('Acceleration_User_Copyto_Contact Trigger....');
 
    for ( User user : System.Trigger.new)
    {
        if  (user.Email.contains('acceleration'))
        {
            system.debug('Acceleration_User_Copyto_Contact Trigger..2.');
            Integer RecordCount = [select count() from Contact c where c.Email = : user.Email];
            
            system.debug('Acceleration_User_Copyto_Contact Trigger..2a .' + RecordCount);
            
            if (RecordCount == 0)
            {
                String AccountId = '';
                for ( Account a : [Select a.Id From Account a where Name = 'Acceleration']) 
                {
                    system.debug('Acceleration_User_Copyto_Contact Trigger..3.');
                     
                    AccountId = a.Id;
                
                    Contact con = New Contact();
                    con.AccountId = AccountId ;
                    con.Email = user.Email;
                    con.Firstname = User.Firstname;
                    con.Lastname = User.Lastname ;
                    con.User__c = User.Id;
                    insert con;
                }          
            }                   
        }
        
     }
    
    
}