0

I have a problem with a oncomplete button. The goal is to do a loop throughout all my custom object and to generate a pdf and attach it to my custom object. I manage with the following loop to attach pdf to all my objects but the data inside the pdf is not dynamic. For now I would just like to attach a pdf to all my custom object with their id inside. (listakta[b].id).

the loop is working as parentid is updating correctly and attaching the pdf to all the custom objects. Why is boite1 not dynamically loading ? I have boite 1 equal to the first object id. But for all my custom object the pdf is the same. I have the oncomplete button because nothing else is working and I am losing my head over this.

Please help me

   public PageReference attachPDF()
    { 
          List<Account> listacc = [Select abo__c,website from account];
          List<Aktarus__c> listakta = [SELECT Id, Name, account__c,text1__c,Type_of_alert__c,comment__c,date__c FROM Aktarus__c ];

   for (integer b =0; b<listakta.size(); ++b){



   PageReference pdf = new PageReference('/apex/s');    

   boite1=listakta[b].id;
            if (listakta[b].id == 'a0o9E000001tH7pQAE') {
            boite1= 'lool';
        }
    parentID=listakta[b].id;
    Attachment applicationpdf = new Attachment();
    blob body = pdf.getContentaspdf();


    applicationpdf.body= body;
    applicationpdf.ContentType = '.pdf';
    applicationpdf.Name = '81 PDF';
    applicationpdf.Name += '.pdf';
    applicationpdf.ParentId = parentId;
    insert applicationpdf;          


    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'PDF has been attached!'));   
    }  

        return null;  

}   



public void saveAttachement() {    
attachpdf();
}

And my vf page is :

   <apex:commandButton action="{!saveAttachement}"
            value="Create Record and Attach PDF" 
            rendered="{!ISNULL(ParentId)}" 
            onComplete="attachPDF();" 
         reRender="myform"/>
{!boite1}

Edit : I did what you suggested with :

       public PageReference attachPDF()
    { 
          List<Account> listacc = [Select abo__c,website from account];
          List<Aktarus__c> listakta = [SELECT Id, Name, account__c,text1__c,Type_of_alert__c,comment__c,date__c FROM Aktarus__c ];
        boite2 = listakta[2].name;
  // for (integer b =0; b<listakta.size(); ++b){

for (Aktarus__c currentAktarus : listakta) {

         PageReference pdf = new PageReference('/apex/s');  
       pdf.getParameters().put('Id', currentAktarus.id);


     boite1=currentAktarus.name;
            if (currentAktarus.id == 'a0o9E000001tH7pQAE') {
            boite1= 'lool';
        }
    parentID=currentAktarus.id;
    Attachment applicationpdf = new Attachment();
    blob body = pdf.getContentaspdf();


    applicationpdf.body= body;
    applicationpdf.ContentType = '.pdf';
    applicationpdf.Name = 'ss PDF';
    applicationpdf.Name += '.pdf';
    applicationpdf.ParentId = parentId;
    insert applicationpdf;          


    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'PDF has been attached!'));   
    }  

        return null;  

}   
    public void saveAttachement() {  
        boite1= 'lool';  

attachpdf();

and in my vf page :

     <apex:page standardcontroller="Aktarus__C" extensions="Audit"    standardStylesheets="false" sidebar="false" cache="false" showHeader="false" applyhtmltag="false" applyBodyTag="false" docType="html-5.0"  >


        <apex:form id="myform">
        <apex:pageMessages />
            <apex:actionFunction name="myActionFunction" action="{!attachPDF}" 
    onComplete="addNext()" reRender="form" />

        <apex:commandButton action="{!saveAttachement}"
                value="Create Record and Attach PDF" 
                rendered="{!ISNULL(ParentId)}" 
                onComplete="attachPDF();" 
             reRender="myform"/>




            <table class='report_table'>
                    <tr>
                                <td style='padding-top:10px;color:  #606060'>{!boite1}<br/><br/><hr style='background:#aaa; height:1px; border:0px;'></hr><br/></td>
                                <td style='padding-top:10px;color:  #606060'>{!aktarus__c.type_of_alert__C}<br/><br/><hr style='background:#aaa; height:1px; border:0px;'></hr><br/></td>
                                <td style='padding-top:10px;color:  #606060'>{!aktarus__c.comment__C}<br/><br/><hr style='background:#aaa; height:1px; border:0px;'></hr><br/></td>
                                <td style='padding-top:10px;color:  #606060'>{!aktarus__c.date__c}<br/><br/><hr style='background:#aaa; height:1px; border:0px;'></hr><br/></td>

                    </tr>
            </table>

       {!boite1}
        <apex:actionFunction name="attachPDF" 
                action="{!attachPDF}" 
                reRender="myform"/>
    </apex:form>

</apex:page>

But now the pdf is empty. I have 2 copies as I call attachpdf from my saveattachment function and from my button. But even by passing the id of my currentaktarus.id I don't manage to display the name of my currentaktarus in my variable boite1

Do you have any idea? And what is the point of contentversion compare to attachment? I am new to salesforce so any help will be very much appreciated

SECOND EDIT :

I have updated my code above. The vf page is called s and my controller above is called AUDIT.

The pdf is saved for all my aktarus__c object, but in all of my attachment I have boite1 = currentaktarus.name, but instead of being the name of the object , it is always the same name (the name of one of my object)

So it is saved but not dynamically.

If it is not clear :

  • I have 6 custom object - one called 'test123', 'loopasda', 'thank' etc...

In all of my attachment object, in the pdf boite1 is displayed but it is always equal to 'loopasda'

I would like that inside my test123 object, in the pdf the boite1 should be 'test123' and not 'loopasda'. Inside my 'thank' object, it should be 'thank' etc

So basically a loop inside all my object, the name of my object saved as a pdf inside my object. One by one.

1

The Pagerefence does not get the id of your object. See also the answer to this question: How to pass {!Case.ID} to PageReference

pdf.getParameters().put('Id', listakta[b].id);

Otherwise you will always get the same content.

A small additional hint: do not loop over a counter, instead of

for (integer b =0; b<listakta.size(); ++b){

you could iterate directly over the list, making the code more readable and helps avoiding potential problems if the counter variable is manipulated in the code:

for (Aktarus__c currentAktarus : listakta) {

the you can access the current object directly through the variable currentAktarus and don't have to use array-access with an index.

Edit a bit too much assumptions on my side.

A missing piece of information is that the same controller was used for the UI and the rendered PDF - my assumption was that the controller for the PDF did not get the correct information.

So, on the receiving side (the PDFs controller) you need to work with the parameter you get. So the controller for the PDF must be something like

public class Aktest2 {
    private Aktarus__c akt;

    public Aktest2 () {
        String aktId = ApexPages.currentPage().getParameters().get('Id');
        akta = [SELECT Id, Name FROM Aktarus__c WHERE Id = :aktId];
    }

}

I explain some parts of the (lengthy) reason why you most probably want a different controller for the PDF at http://blog.loco-toys.de/wordpress/2019/08/salesforce-rendering-of-visual-force-pages-and-context/

  • 1
    In addition to your hint, salesforce is deprecating attachments in favor of Files. So it would probably be better to create a ContentVersion and ContentDocumentLink instead of an attachment. – H Alves Aug 16 '19 at 10:51
  • Good additional point! – Florian Heer Aug 16 '19 at 11:57
  • I uploaded in the first post my new pages but the same problem occurs. – Tibgg Aug 16 '19 at 13:29
  • In that case, there might be an issue with the second page. Do you use the Id-Parameter there (that was only a guess by me), or how do you determine where information should come from? – Florian Heer Aug 16 '19 at 13:37
  • What I still cannot see from the code parts you provided is what is happening in "s2" - the page that creates the PDF. I assume that the controller of that page does not correctly receive the parameter it needs. – Florian Heer Aug 16 '19 at 20:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.