0

I'm trying to set up the following tutorial : https://blogs.absyz.com/2017/07/13/workaround-for-pdf-rendering-in-sf1/

It works perfectly for the Contact tab but, when i try to adapt this for my custom object (here "Visite_de_maintenance__c"), I have the following error message when I want to send the email :

common.apex.runtime.impl.ExecutionException: SObject row was retrieved via SOQL without querying the requested field: Visit_of_maintenance__c.Name

Here is my apex class :

public with sharing class sendEmail2{
public String subject { get; set; }
public String body { get; set; }
public String emailid{get; set;}
private final Visite_de_maintenance__c Con;

public sendEmail2(ApexPages.StandardController controller) {
  Con = [SELECT name, id
                   FROM Visite_de_maintenance__c
                   WHERE Id=:ApexPages.currentPage().getParameters().get('id')];
  }
public Visite_de_maintenance__c getContact() {
     return Con;
    }
 public PageReference send() {


    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    PageReference pdf =  Page.OrderLetterToCustomerPage;
    pdf.getParameters().put('id',(String)Con.id);
    pdf.setRedirect(true);
    Blob b = pdf.getContentAsPdf();

    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setContentType('application/pdf');
    efa.setFileName('attachment.pdf');
    efa.setBody(b);
    String addresses;
    addresses=emailid;
    String[] toAddresses = addresses.split(':', 0);
    email.setSubject( subject );
    email.setToAddresses( toAddresses );
    email.setPlainTextBody('Find the Attachment');
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
    // Sends the email
    Messaging.SendEmailResult [] r =
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
}
}

And here is my visualforce page render as PDF (Page.OrderLetterToCustomerPage) :

<apex:page standardController="Visite_de_maintenance__c"  showHeader="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false" renderas="pdf">
<head>
</head>
    <body>
        <!--<center>-->
        <div class="main-title"><h3>Visite de maintenance {!Visite_de_maintenance__c.Name}</h3>
        <apex:image id="logo" url="{!$Resource.logo}" width="115" height="19"/>
        <div class="info_title"><p><label>Commercial</label> :&nbsp;<apex:outputText value="{!Visite_de_maintenance__c.Owner.name}"/></p>
        <p><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Test_effectue_le__c'].label}"/> :&nbsp;<apex:outputText value="{0,date,dd'/'MM'/'yyyy}"><apex:param value="{!Visite_de_maintenance__c.Test_effectue_le__c}" /></apex:outputText></p></div>
        </div> 
        <span class="title-header">Fiche de contrôle du Défibrillateur Automatisé Externe</span>

       <!--</center>-->

        <apex:form >
            <div class="container">

            <div class="block-header">
            <h3>Informations générales :</h3>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Numero_contrat__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Numero_contrat__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['modele_du_defibrillateur__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Modele_du_defibrillateur__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['numero_serie_defibrillateur__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.numero_serie_defibrillateur__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['numero_lot_batterie__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.numero_lot_batterie__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['date_fabrication_batterie__c'].label}*"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.date_fabrication_batterie__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['numero_lot_electrodes__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.numero_lot_electrodes__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['date_peremption_electrodes__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.date_peremption_electrodes__c}"/></div>
            </div>

            <div class="block">
            <div class="title_bg"><h3>Conformité de l'installation:</h3></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Conformite_installation__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Conformite_installation__c}"/></div>
            </div>

            <div class="block">
            <div class="title_bg"><h3>Aspect général :</h3></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Aspect_general__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Aspect_general__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Commentaires_aspect_general__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Commentaires_aspect_general__c}"/></div>
            <br></br>
            <div class="info-batterie">L'aspect général permet d'évaluer les points suivants : état du boîtier et capot, intégrité du sceau d'immunité, marquage réglementaire, poignée, visibilité des aides (guides graphiques),état des touches/voyants et électrodes, enclenchement et maintien de la batterie, accessoires divers.</div>
            </div>

            <div class="block">
            <h3>Tests :</h3>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Batterie_chargee__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Batterie_chargee__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Batterie_dechargee__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Batterie_dechargee__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Autotest_insertion_batterie__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Autotest_insertion_batterie__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Mise_en_marche_defibrillateur__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Mise_en_marche_defibrillateur__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Realise_avec_simulateur__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Realise_avec_simulateur__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Branchement_des_electrodes__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Branchement_des_electrodes__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Guides_vocaux__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Guides_vocaux__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Delivrance_choc_mode_FVF__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Delivrance_choc_mode_FVF__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Sans_delivrance_choc_mode_asystole__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Sans_delivrance_choc_mode_asystole__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Valeur_energie_delivree__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Valeur_energie_delivree__c}"/></div>
            </div>

            <div class="block">
            <h3>Changement des électrodes ou de la batterie* :</h3>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['numero_lot_electrodes_new__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.numero_lot_electrodes_new__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['date_peremption_electrodes_new__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.date_peremption_electrodes_new__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['numero_lot_batterie_new__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.numero_lot_batterie_new__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['date_fabrication_batterie_new__c'].label}*"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.date_fabrication_batterie_new__c}"/></div>
            </div>
            <div class="info-batterie">*À remplacer de préférence (<b>date mise en service +3ans</b> pour Def-i et Auto-Def et <b>+4 ans</b> pour Def-NSI et Auto-NSI).</div>
            <div class="block">
            <h3>Résultats :</h3>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Appareil_conforme__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Appareil_conforme__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['demande_maintenance__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.demande_maintenance__c}"/></div>
            <div><apex:outputLabel value="{!$ObjectType.Visite_de_maintenance__c.fields['Commentaires__c'].label}"/> :&nbsp;<apex:outputField value="{!Visite_de_maintenance__c.Commentaires__c}"/></div>
            </div>
            <div class="signature"><span>Signature du commercial: </span></div>
            <div class="signature float_r"><span>Signature du représentant : </span></div>


            </div>
            <div class="footer">
            <span>07/2019 - rev.1<br></br></span>
            <span>Drive DeVilbiss Healthcare France - Chaussée du Ban la Dame - Parc d'activités Eiffel Energie - ZAC du Ban la Dame - BP 19 <br></br>54390 FROUARD - FRANCE - Tel 03 83 495 495 - Fax. 03 83 495 496 - <a href="drivedevilbiss.fr">www.drivedevilbiss.fr</a></span>
            </div>
        </apex:form>
    </body>
</apex:page>`

And here is my VFP page that call the Apex class :

<apex:page standardController="Visite_de_maintenance__c"  showHeader="false"  standardStylesheets="True"   extensions="sendEmail2"  >
<H1>To get PDF in the mail, Give mail id in the text box and click 'Email PDF' button</H1>
<H1>PDF will be mailed to the given Email Id</H1>

<apex:form >
<H1 style="font-size:15px"><span>Email id:</span></H1>

<apex:inputText label="Email" value="{!emailid}"   style="border-color: #696363 #696363;font-size:15px;margin-left:2px"></apex:inputText>
<apex:commandButton value="Email PDF" action="{!send}" style="font-size:15px" />
<apex:commandButton onclick="sforce.one.navigateToURL('/{!Visite_de_maintenance__c.id}')" value="Back" style="font-size:15px" />
 </apex:form>
</apex:page>

Thanks in advance for your help

  • Alexandre
  • 1
    In which line the error occurs? Maybe you are trying to render a custom page that tries to use the Name field, and you aren't querying it on its controller? – Renato Oliveira Jul 4 '19 at 11:54
  • Yeah, @Renato is right. The error is in the Visualforce page OrderLetterToCustomerPage, not here. – David Reed Jul 4 '19 at 12:05
  • I add the OrderLetterToCustomerPage in my original post. – Alexandre Jul 4 '19 at 12:07
  • Well I don't see why the page would fail with this message, since it is using a standard controller. Maybe then your Id passed to the page parameter is actually null, and it isn't retrieving the record? Does the error message display a stack trace (in which line the exception was triggered)? – Renato Oliveira Jul 4 '19 at 12:15
  • 1
    You have a custom button that calls a different Visualforce page + controller which then invokes OrderLetterToCustomerPage, correct? You need to post the other VF page. Separately: the reason the Owner.Name field is throwning an error is because related object fields are not automatically retrieved by a standard controller, so you will need to write an extension for OrderLetterToCustomerPage to retrieve the owner fields. – David Cheng Jul 6 '19 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.