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
hmoh1920hmoh1920 

File does not begin with '%PDF with PDF attachment in e-mail

Hi,

 

I have this error when i open e-mail pdf attachment  "File does not begin with '%PDF-'"

 

this is my batch class to send email.

I do not know where is the problem! please help me.

 

note: AppelCotisation is my vf page genered pdf, i use this page in another class and execute successfully.

 

 

global class BatchT20 implements Database.Batchable<sObject>{



String OppId;
String varRech;
Decimal TaxeOpp=0;
Datetime StartDate;
Datetime EndDate;
 
string AdresseCity;
string AdressePostalCode;
string AdresseStreet;


string NomContrat;
string Numerodepolice;
decimal PrimeTTC;
decimal PrimeTTCPeriode;


        
global final String query ='SELECT id,name FROM Opportunity';    
   
//------------------------------------------------------------------------------------------------------------------------

global Database.querylocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);}

global void execute(Database.BatchableContext BC, List<Sobject> scope){


//calcul de la marge temporaire
    string Datedeffet;
    string Datedecheance;
    Datetime dateJour = datetime.now();

    Date dateRef = Date.newInstance(dateJour.year(),dateJour.month(),dateJour.day()-20);
    Date dateMax = Date.newInstance(dateJour.year()-1,dateJour.month()-1,dateJour.day());



                   //-------------------------------------------------------------------------//

List<Quittance__c>  QuittFac = [SELECT Id, Opportunit__c, CreatedDate, Facturation_Avoir__c, varBalance__c, Total_pay__c,
                               Montantdesecheances__c, Nbre_Rappel_Cotisation__c,
                               Opportunit__r.Accountid, Opportunit__r.Date_d_effet__c, Opportunit__r.Type_de_produit__c,
                               Opportunit__r.Statut_Police__c, Opportunit__r.id, Opportunit__r.name, Opportunit__r.Pricebook2.name,
                               Opportunit__r.Date_d_ech_ance_de_la_police__c, Opportunit__r.Account.BillingCity,
                               Opportunit__r.Account.BillingPostalCode, Opportunit__r.Account.BillingStreet,
                               Opportunit__r.Primes_TTC__c, Opportunit__r.Prime_TTC_periode__c
                               
                               FROM Quittance__c
                               where
                               ((Opportunit__r.Date_d_effet__c=:dateRef and Opportunit__r.Type_de_produit__c!='Produit'
                               
                               and (Opportunit__r.Statut_Police__c='Nouvelle'
                               or Opportunit__r.Statut_Police__c='Renouvellement'
                               or Opportunit__r.Statut_Police__c='Avenant'))
                               
                               and Facturation_Avoir__c='Facturation' and varBalance__c<0)];


                   //-------------------------------------------------------------------------//
                   

List <Contact> contact =[Select Id, AccountId, Salutation, FirstName, LastName, Email, Destinataire__c From Contact where Destinataire__c='Oui'];          


                   //-------------------------------------------------------------------------//
                   
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
                    List<String> contactids=new List<String>();
    
                
            //-----------------------//
              Integer K=0, s;
              k = QuittFac.size();     
              for ( s=0;s<k;s++)
                {
                     //-------------//
                      if (k!=0)           
                        {
  OppId=QuittFac[s].Opportunit__r.id;                         
  StartDate = datetime.newInstance(QuittFac[s].Opportunit__r.Date_d_effet__c.year(), QuittFac[s].Opportunit__r.Date_d_effet__c.month(), QuittFac[s].Opportunit__r.Date_d_effet__c.day());              
  EndDate = datetime.newInstance(QuittFac[s].Opportunit__r.Date_d_ech_ance_de_la_police__c.year(), QuittFac[s].Opportunit__r.Date_d_ech_ance_de_la_police__c.month(), QuittFac[s].Opportunit__r.Date_d_ech_ance_de_la_police__c.day());
              
  Datedeffet = StartDate.format('dd/MM/yyyy');
  Datedecheance = EndDate.format('dd/MM/yyyy');                            
                            
                         varRech=QuittFac[s].Opportunit__r.Accountid;
                         NomContrat =QuittFac[s].Opportunit__r.Pricebook2.name;
                         
                         PrimeTTC=QuittFac[s].Opportunit__r.Primes_TTC__c;
                         PrimeTTCPeriode=QuittFac[s].Opportunit__r.Prime_TTC_periode__c;
                         
                         
                         AdresseCity = QuittFac[s].Opportunit__r.Account.BillingCity;
                         AdressePostalCode = QuittFac[s].Opportunit__r.Account.BillingPostalCode;
                         AdresseStreet = QuittFac[s].Opportunit__r.Account.BillingStreet;
                         
                      

                         PageReference AppelCotisation = Page.AppelCotisation;
                         AppelCotisation.getParameters().put('id',OppId);
                          
                         Messaging.EmailFileAttachment myEQuote = new Messaging.EmailFileAttachment();
                         
 
                          
                                 
                         Blob htmBlob = AppelCotisation.getContent();                      
                                                        
                                   
                                    Integer M=0, n;
                                    M = contact.size();
                                    for ( n=0;n<M;n++)
                                        {
                                         
                                           if (M!=0 && contact[n].AccountId == varRech && contact[n].Email!=null )           
                                             {
                                                             
                                                 String ContactId =contact[n].Id ;
                                                 String Salutation =contact[n].Salutation ;
                                                 String prenomContact=contact[n].FirstName;
                                                 String nomContact=contact[n].LastName;     
            
                    
                                                 contactids.add(contact[n].Email);
   
   

                                                 mail.setSubject('Rappel 1');  
                               
                       
                                                 mail.setSenderDisplayName('Test@test.com');
                                                 mail.setReplyTo('test@test.com');
                                                 mail.setSenderDisplayName('test');
                                                 
                                                 
            Attachment a = new Attachment(parentId = OppId, name=QuittFac[s].Opportunit__r.name + '.pdf', body = htmBlob, isPrivate=false);
        
            // Insert the attachment
            insert a;
     
     mail.setHtmlBody('html e_mail');


                                                 mail.setWhatId(OppId);
                                                
                                                 mail.setTargetObjectId(ContactId);
                  
                                                 
                                         myEQuote.setFileName(QuittFac[s].Opportunit__r.name.replace('/',' - ')+'.pdf');
                                         myEQuote.setBody(htmBlob);
                      
                                         mail.setFileAttachments(new Messaging.EmailFileAttachment[] {myEQuote});                                                
                                                 
                                     
                      
                                                 Messaging.sendEmail(New Messaging.SingleEmailMessage[]{mail});              
                
                                                 contactids.clear();  
                            
                                             }
                                        }     
                                  
                           QuittFac[s].Nbre_Rappel_Cotisation__c='Rappel1';        
                                                                                                                    
                        }
                 }                    
        
        List<Database.SaveResult> dsrs = Database.update(QuittFac, false);
        for(Database.SaveResult dsr : dsrs){ }           
                    
}
global void finish(Database.BatchableContext BC){
     
}
}

 

 

thanks.

 

 


moh.

Laxman RaoLaxman Rao

Hi,

 

The problem is you cannot use the getContent and getContentAsPDF PageReference methods in a batch job.

 

Refer this link :

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm and search for text "getContent"

hmoh1920hmoh1920

Hi,

 

thanks but your answer has nothing to do with that I ask!

 

my problem is File does not begin with '%PDF into   "global void execute"  méthode, not into  "global void finish"

 

i delete the code of "global void finish" for have space.

 

 

thanks.

crop1645crop1645

hmoh1920

 

Here's how to debug this -- Check your object where you stored the PDF that you uploaded from a VF page. Use the EncodingUtil.convertToHex() method to display the value of the first few bytes in hex.  Verify that you see %PDF- at the beginning.

 

If you do, then continue this process all along to verify you don't lose this before it becomes an email attachment.body

 

If you don't, then the problem is in the uploading process