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
Rocks_SFDCRocks_SFDC 

Trigger to Save the Pdf file generated by visualforce into Attachment related list

Hello Everyone,

 

When i click on the "Save" button, i want to insert the pdf file that is generated by visualforce into the attachments related list.

 

I have implemented the code for this. But i am able to insert the attachment but when i try to open the pdf file, i am getting "Failed to Load PDF Document" error.

 

Here is the code:

 

Trigger:

----------

trigger InsertAttachment on Account (after insert) {
for(Account acc:Trigger.New)
{
MergePDF_Controller ac=new MergePDF_Controller();
ac.Save(trigger.new[0]);
}

}

 

Class:

 

public with sharing class MergePDF_Controller {
public pagereference save(Account AccId){

PageReference pdf = new PageReference('/apex/MergePDF?id='+Accid);
Attachment attach = new Attachment();

Blob body;

try {

body = pdf.getContent();

} catch (VisualforceException e) {
body = Blob.valueOf('Some Text');

}

attach.Body = body;
attach.Name = Account.name+'.pdf';
attach.IsPrivate = false;
attach.ParentId = AccId.id;
insert attach;
return new PageReference('/' +AccId.id);


}
}

 

 

Any suggestions will be helpful.

 

Thanks,

Anil

kranjankranjan

Hi,

 

There are few things here. Though you have tried to bulkify in the trigger, however youa re still using the 0th index always. Do correct this to use ac.Save(acc); instaed of ac.Save(trigger.new[0]); Also you are passing the complete Accid whihc is account object to the VF Page id param. I believe you wnat to just pass the account Id so it should be something like this: PageReference pdf = new PageReference('/apex/MergePDF?id='+Accid.id); So there might be an error coming up and should get resolved with this. If not please post the VF code for reference.

 

Regards

Rocks_SFDCRocks_SFDC

Thank you for reply !!!

i am getting the following Error.

 

"Error: Compile Error: Method does not exist or incorrect signature: [MergePDF_Controller].Save(SOBJECT:Account) at line 5 column 1"

kranjankranjan

Hi Anil,

 

Basically what you were missing was adding contentType to be assigned to the attachment as "PDF". Also you cannot call the getCOntent method from inside trigger, so you would need to make it a future method. I tried to clean up your code and here is the working code for you:

 

APEX:

public with sharing class MergePDF_Controller {
	@future
	public static void save(Id AccId, String acctName){
	
		PageReference pdf = new PageReference('/apex/MergePDF?id='+Accid);
		Attachment attach = new Attachment();
		
		Blob body;
		
		try {
		
			
		} catch (VisualforceException e) {
			body = Blob.valueOf('Some Text');
		
		}
		attach.Body = body;
		attach.Name = acctName+'.pdf';
		attach.IsPrivate = false;
		attach.contentType = 'pdf';
		attach.ParentId = AccId;
		insert attach;
		//return new PageReference('/' +AccId.id);
		
	}
}

 

TRIGGER:

trigger InsertAttachment on Account (after insert) {
	for(Account acc:Trigger.New)
	{
		//MergePDF_Controller ac=new MergePDF_Controller();
		MergePDF_Controller.Save(acc.Id, acc.Name);
	}
}

 

 

Let me know if this works.

 

Regards

Rocks_SFDCRocks_SFDC

Hi Kamal,

 

Thank you for providing the Information.

 

When i have tried with your code. I am getting the following error.

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Body]: [Body]

 

So i have modified the code in the following way.

Public with sharing class MergePDF_Controller {

@future
Public static void save(Id AccId, String acctName){

PageReference pdf = new PageReference('/apex/MergePDF?id='+Accid);
Attachment attach = new Attachment();

Blob body;

try {

body = pdf.getContentAsPDF();
system.debug('AAAAAAAAAAAAA'+body);

} catch (VisualforceException e) {
body = Blob.valueOf('Some Text');
system.debug('BBBBBBBBBBB'+body);
}

attach.Body = body;
attach.Name = acctName+'.pdf';
attach.IsPrivate = false;
attach.ParentId = AccId;
insert attach;

    }
}

 

I am able to insert the pdf attachment but i am getting the body null. Please find my visualforce page code below:

 

<apex:page standardController="Account" renderAs="pdf">

This is the test pdf
{!Account.Name}
</apex:page>

 

 

Thanks,

Anil

 

 

kranjankranjan

Hi Anil,

 

Yeah thats actually the one. I probably cleaned up few things after running and while pasting i guess that main line went off. :)

You are still missing the key thing which is to assign the contentType = 'PDF'. Here is the complete code for you again:

 

APEX:

public with sharing class MergePDF_Controller {
	@future
	public static void save(Id AccId, String acctName){
	
		PageReference pdf = new PageReference('/apex/MergePDF?id='+Accid);
		Attachment attach = new Attachment();
		
		Blob body;
		
		try {
		
			body = pdf.getContentAsPDF();
			system.debug('AAAAAAAAAAAAA'+body);
			} catch (VisualforceException e) {
			body = Blob.valueOf('Some Text');
		
		}
		attach.Body = body;
		attach.Name = acctName+'.pdf';
		attach.IsPrivate = false;
		attach.contentType = 'pdf';
		attach.ParentId = AccId;
		insert attach;
		//return new PageReference('/' +AccId.id);
		
	}
}

 

TRIGGER:

trigger InsertAttachment on Account (after insert) {
	for(Account acc:Trigger.New)
	{
		//MergePDF_Controller ac=new MergePDF_Controller();
		MergePDF_Controller.Save(acc.Id, acc.Name);
	}
}

 

 

Regards

Rocks_SFDCRocks_SFDC

Hi Kamal,

 

I am getting null body in pdf. Could you please let me know why it is working like that.

 

Thanks,

Anil

kranjankranjan

Hi Anil,

 

I added the corrected code again above. Let me know if you were able to resolve the issue now.

 

Regards

Rocks_SFDCRocks_SFDC

Hi Kamal,

 

Sorry Kamal.....Still i am getting the body of pdf null.

 

Thanks,

Anil

kranjankranjan

Hi Anil,

 

Basically when i tried this the other day also, it asked me for login when I clicked to view the attachment file. I ignored that and provided the login, however what it actually did was that it had stored the page URL and it took me there and the pDF was generated at that time and was shown. I have tried various ways after that and also read through some articles as follows:

 

http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/

 

 

http://stackoverflow.com/questions/12510287/generating-pdf-from-trigger-future-class-in-salesforce

 

What I feel is that there is limitation to calling the getcontent method as well frmo the Future method. So even when we can call that method from trigger by making it a future method, we are not able to utilize that completely. Having said that there are 2 ways, one as explained above where the page will be called again on view button click and PDF generated and shown. Second and suggested one would be to add a button on the acocunt detail page which does this instead of from the trigger.

 

Regrads