1

this is my first Apex Class and Visualforce.

EDIT DESCRIPTION_18.11.2024: I want to generate PDF from TWO custom objects - Faktura__c and Przychody_nowe__c.

I added a button to generate PDF via Visualforce Page on the Faktura__c module after Andrii Muzychuk's comment.

But now you can't see data from two modules: Faktura__c and Przychody_nowe__c.

We are returning in SandBox environment because we don't have Code Coverage - 0% (0/11) for Apex Class. FakturaTriggerHandler is Code Coverage 100% (10/10).

Apex Class:

    public class FakturaPDF {

    public FakturaPDF(ApexPages.StandardController controller) {

    }

    public Faktura__c faktura {get {
                                     if (faktura == null)
                                     faktura = new Faktura__c();
                                     return faktura;
                                    }
                               set;}
    
    public List<Przychody_nowe__c> przychody {get; set;}
    
    public FakturaPDF(){
        this.faktura = [SELECT Name, Nadawca__c, Odbiorca__c, Adres__c, NIP__c, Numer_Projektu__c, Okazja_biznesowa__c, 
                               Data_sprzeda_y__c, Termin_p_atno_ci__c, Rodzaj_faktury__c
                        FROM Faktura__c
                        WHERE Id = :this.faktura.Id
                        ];
                        

        
        this.przychody = [SELECT Name, Nazwa_Przychodu_tre_na_fakturze_cd__c, Przych_d_jednostkowy_netto__c, Ilo__c, Przych_d_ca_kowity_netto__c, Przych_d_ca_kowity_brutto__c
                              FROM Przychody_nowe__c
                              WHERE Faktura__c = :this.faktura.Id
                             ];
    }
}

Visual Page:

        <apex:page standardController="Faktura__c" extensions="FakturaPDF" renderAs="PDF">
    <html>
        <head>
          <meta charset="UTF-8" />
          <meta name="viewport" content="width=device-width" />
          <title>Dane do faktury z Salesforce MaxtoITS</title>
        </head>
        <body>
  

            <center>
                <h1 class="heading heading--main">Dane do wystawienia faktury:</h1>
            </center>
            
            
            
            <div class="box--half">
           
                <h2 class="heading heading--sub">Nadawca:</h2>
                <span>{!faktura.Nadawca__c}</span>
            
            </div>
            
            
            <div class="box-half">
                <h2 class="heading heading--sub">Odbiorca:</h2>
                <span>{!faktura.Odbiorca__c}</span> <br></br>
                <span>Adres: {!faktura.Adres__c}</span> <br></br>
                <span>NIP: {!faktura.NIP__c}</span>
            </div>
            
            
            <div class="box-half">
                <h2 class="heading heading--sub">Numer projektu:</h2>
                <span>{!faktura.Numer_Projektu__c}</span>
            </div>
            
          
            <div class="box-half">
                <h2 class="heading heading--sub">Okazja biznesowa:</h2>
                <span>{!faktura.Okazja_biznesowa__c}</span>
            </div>
            
           
            <div class="box-half">
                <h2 class="heading heading--sub">Data sprzedaży:</h2>
                <span>{!faktura.Data_sprzeda_y__c}</span>
            </div>
   
            <div class="box-half">
                <h2 class="heading heading--sub">Termin płatności:</h2>
                <span>{!faktura.Termin_p_atno_ci__c}</span>
            </div>
            
            <div class="box-half">
                <h2 class="heading heading--sub">Rodzaj faktury:</h2>
                <span>{!faktura.Rodzaj_faktury__c}</span>
            </div>
   
            
            <div class="box--half">
            <h2 class="heading heading--sub">Lista pozycji na fakturze:</h2>
            
            <table border="1" cellpadding="5" cellspacing="2" style="width: 100%;">
                    <tr>
                        <th>Nazwa</th>
                        <th>Kwota jednostkowa [zl netto]</th>
                        <th>Ilość</th>
                        <th>Wartosc netto [zl]</th>
                        <th>Wartosc brutto [zl]</th>
                        
                    </tr>
            
            <apex:repeat value="{!przychody}" var="przy">
            
             <tr>
                            <td>{!przy.Name}</td>
                            <td>{!przy.Przych_d_jednostkowy_netto__c}</td>
                            <td>{!przy.Ilo__c}</td>
                            <td>{!przy.Przych_d_ca_kowity_netto__c}</td>
                            <td>{!przy.Przych_d_ca_kowity_brutto__c}</td>
                        </tr>
                    </apex:repeat>
            </table>
           </div>
        </body>
    </html>
</apex:page>

Trigger:

public with sharing class FakturaTriggerHandler 
{
    private boolean m_isExecuting = false;
    private integer BatchSize = 0;
    public static boolean IsFromBachJob ;
    public static boolean isFromUploadAPI=false;
    
    public FakturaTriggerHandler(boolean isExecuting, integer size)
    {
        m_isExecuting = isExecuting;
        BatchSize = size;
    }
            

    public void OnBeforeInsert(List<Faktura__c> newFaktura)
    {
        system.debug('Faktura Trigger On Before Insert');
    }
    public void OnAfterInsert(List<Faktura__c> newFaktura)
    {
        system.debug('Faktura Trigger On After Insert');
    }
    public void OnAfterUpdate( List<Faktura__c> newFaktura, List<Faktura__c> oldFaktura, Map<ID, Faktura__c> newFakturaMap , Map<ID, Faktura__c> oldFakturaMap )
    {
        system.debug('Faktura Trigger On After Update ');
  
    }
    public void OnBeforeUpdate( List<Faktura__c> newFaktura, List<Faktura__c> oldFaktura, Map<ID, Faktura__c> newFakturaMap , Map<ID, Faktura__c> oldFakturaMap )
    {
        system.debug('Faktura Trigger On Before Update ');
    }
   }

Test FakturaPDFtest:

@isTest
public class FakturaPDFtest{

@isTest static void FakturaPDFTest() {


        Przychody_nowe__c przy = new Przychody_nowe__c();
        przy.Typ_przychodu__c = 'Sprzęt';   
        przy.Status__c = 'Roboczy';
        przy.Nazwa_Przychodu_tre_na_fakturze_cd__c = 'Test';
        przy.Stawka_VAT__c = 23;
        przy.Przych_d_jednostkowy_netto__c = 100;
        przy.Ilo__c = 1;
        przy.Okazja_biznesowa__c = '006Sb00000CLeEtIAL';
        przy.Data_sprzeda_y__c = System.today().addMonths(6);
        przy.Termin_platnosci__c = 1;
    
        insert przy;
    
    Test.startTest();
    
    przy.Status__c = 'Do sprawdzenia przez Dział Logistyki';
    update przy;
    
 
    
    
        Faktura__c fak = new Faktura__c();
        fak.Termin_p_atno_ci__c = 1;   
        fak.Status__c = 'Roboczy';
        fak.Rodzaj_faktury__c = 'Elektroniczna';
        fak.Data_sprzeda_y__c = System.today().addMonths(6);
        fak.Okazja_biznesowa__c = '006Sb00000CLeEtIAL';
    
        insert fak;
     
        List<Przychody_nowe__c> przychody = [SELECT Id, Name 
                                             FROM Przychody_nowe__c 
                                             WHERE Name = :przy.Nazwa_Przychodu_tre_na_fakturze_cd__c AND Faktura__c = :fak.Id];
        System.assertEquals(0, przychody.size());
    
       Test.stopTest();

  }
}
3
  • 1
    Hi and welcome to SFSE. Have you managed to create a VF-based action successfully in your sandbox? Have you included the page layout with that action in your change set when deploying to production? Please add these details via an edit of your question, rather than as answering comments. Thanks. (BTW, always "Salesforce", never "SalesForce".)
    – Phil W
    Commented Nov 15 at 15:43
  • I think we might need a little bit more information on what you are trying to achieve on the page itself, let alone a deployment error (I would focus on that later.) Is the page working as intended when you pass in the id to the vf page? /apex/yourPageName?id=yourIdHere Commented Nov 15 at 19:48
  • Thank you for interest in my case. I added more information to description. Commented Nov 18 at 10:28

1 Answer 1

1

You lack specifying standardController attribute on apex:page tag. Your Visualforce should start with

<apex:page standardController="Faktura__c" extensions="FakturaPDF" renderAs="PDF">

The documentation - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_std_associate.htm

7
  • not only that, but the controller constructor signature has to change from custom controller to standard controller.
    – cropredy
    Commented Nov 15 at 19:08
  • The constructor of the extension class has nothing to do with having a Visualforce page being available for a button for an object. Yes, it will get you a context like a record on which it was clicked (or records). But it is not mandatory. Commented Nov 15 at 22:58
  • 1
    good point but kind of a best practice
    – cropredy
    Commented Nov 15 at 23:03
  • Agree. I just checked a few of my implementations - there is no class without ApexPages.StandardController standardController attribute of the constructor. Commented Nov 15 at 23:19
  • Thank you for interest in my case. I added your code and I have an error: Error: Unknown constructor 'FakturaPDF.FakturaPDF(ApexPages.StandardController controller)' Commented Nov 18 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .