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
AntonyasLenAntonyasLen 

Dynamic Pagereference for .pdf attachement - error:URL that no longer exists on salesforce.com.

Hi,

 

I created an invoice generator who is working fine.(by clicking on a button)

But i want to attach a pdf for every invoices to their master opportunity.(by clicking on a button)

I tried with one page layout and it's was working well, but i need to do it more dynamically.


Every invoices have a field called 'Layout_Number__c' who is a picklist field  set during the creation of the invoice (corresponding to a page name).

 

Here  my code :

public class TestAttachInvoices {

	
	ApexPages.StandardController controller;
	
	public Opportunity opp;

	
	public TestAttachInvoices(ApexPages.StandardController controller)
	{
		this.opp = (Opportunity)controller.getRecord();
		
	}
	
	public PageReference savePdf(){
		List<Invoice__c> invList = [Select id,Invoice_Name__c,Name,Layout_Number__c
									from Invoice__c
									Where Opportunity__c = :opp.id
									and Generated__c = false];
		for( Invoice__c invItem : invList ){
			pdfInvoices(invItem);
		}
		return new PageReference('javascript&colon;window.close()'); 
	}
	public static void pdfInvoices(Invoice__c inv){
		
		
		
		//Pagereference pdfPage = Page.InvoiceDeliveryA;  -> this was working well
		Pagereference pdfPage = Page.+inv.Layout_Number__c; // generating the error
		
		pdfPage.getParameters().put('Id',inv.id);
		
		//the contents of the attachment from my pdf
		Blob pdfBlob;

		try{
			
			pdfBlob = pdfPage.getContentAsPDF();
			
		}catch(VisualforceException e){
			system.debug(e);
			pdfBlob = Blob.valueOf('error');
		}
		
		Attachment attach = new Attachment(parentId = inv.id, name= inv.Name + '.pdf', body = pdfBlob);
		attach.IsPrivate = false;
		
		try{
			insert attach;
			inv.Generated__c = true;
			update inv;
		}
		 catch(DmlException d){
			d.getMessage();
		}
	}

}

 Here the error:

                                      error: You have attempted to reach a URL that no longer exists on salesforce.com.

 

 

Does anyone can help me?

Thanks,

 

edit: i tried to set as Pagereference pdfPage = Page.+inv.Layout_Number__c; but it still doesn't work =(