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
SFManiSFMani 

List has no rows for assignment to SObject with getContent() from pdf

I want to create a pdf from a cutom button click and then attach it to the correspondening Object record. I have the following code for the apex controller for this:
public class attachPDFToTimeRecord {
	
	private final TimeRecords__c a; //TimeRecords object
	
	//constructor
	public attachPDFToTimeRecord(ApexPages.StandardController standardPageController) {
		a = (TimeRecords__c)standardPageController.getRecord(); //instantiate the TimeRecord object for the current record
	}
	
	//method called from the Visualforce's action attribute
	public PageReference attachPDF() {
		//generate and attach the PDF document
		PageReference pdfPage = Page.Invoice_Matter; //create a page reference to our Invoice_Matter Visualforce page
		Blob pdfBlob = pdfPage.getContent(); //get the output of the page, as displayed to a user in a browser
        
		Attachment attach = new Attachment(parentId = a.Id, Name = 'pdfAttachmentRecord.pdf', body = pdfBlob); //create the attachment object
		insert attach; //insert the attachment
		//redirect the user
		PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); //we want to redirect the User back to the TimeRecord detail page
		pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
		return pageWhereWeWantToGo; //send the User on their way
	}

}

I get List has no rows for assignment to sObject error in the line
Blob pdfBlob = pdfPage.getContent();
Can anyone help my resolve this error?
 
Best Answer chosen by SFMani
Amit Singh 1Amit Singh 1
Hello,

Error is because you need to pass Record Id into the VF page. Use below code.
public class attachPDFToTimeRecord {
	
	private final TimeRecords__c a; //TimeRecords object
	
	//constructor
	public attachPDFToTimeRecord(ApexPages.StandardController standardPageController) {
		a = (TimeRecords__c)standardPageController.getRecord(); //instantiate the TimeRecord object for the current record
	}
	
	//method called from the Visualforce's action attribute
	public PageReference attachPDF() {
		//generate and attach the PDF document
		PageReference pdfPage = Page.Invoice_Matter; //create a page reference to our Invoice_Matter Visualforce page
        pdfPage.getParameters().put('id',a.id);// replace Id with the correct parameter if you are using other param.
		Blob pdfBlob = pdfPage.getContent(); //get the output of the page, as displayed to a user in a browser
        
		Attachment attach = new Attachment(parentId = a.Id, Name = 'pdfAttachmentRecord.pdf', body = pdfBlob); //create the attachment object
		insert attach; //insert the attachment
		//redirect the user
		PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); //we want to redirect the User back to the TimeRecord detail page
		pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
		return pageWhereWeWantToGo; //send the User on their way
	}

}


Let me know the outcomes.
Thanks!
AMit Singh

All Answers

Amit Singh 1Amit Singh 1
Hello,

Error is because you need to pass Record Id into the VF page. Use below code.
public class attachPDFToTimeRecord {
	
	private final TimeRecords__c a; //TimeRecords object
	
	//constructor
	public attachPDFToTimeRecord(ApexPages.StandardController standardPageController) {
		a = (TimeRecords__c)standardPageController.getRecord(); //instantiate the TimeRecord object for the current record
	}
	
	//method called from the Visualforce's action attribute
	public PageReference attachPDF() {
		//generate and attach the PDF document
		PageReference pdfPage = Page.Invoice_Matter; //create a page reference to our Invoice_Matter Visualforce page
        pdfPage.getParameters().put('id',a.id);// replace Id with the correct parameter if you are using other param.
		Blob pdfBlob = pdfPage.getContent(); //get the output of the page, as displayed to a user in a browser
        
		Attachment attach = new Attachment(parentId = a.Id, Name = 'pdfAttachmentRecord.pdf', body = pdfBlob); //create the attachment object
		insert attach; //insert the attachment
		//redirect the user
		PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); //we want to redirect the User back to the TimeRecord detail page
		pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
		return pageWhereWeWantToGo; //send the User on their way
	}

}


Let me know the outcomes.
Thanks!
AMit Singh
This was selected as the best answer
SFManiSFMani
Bingo!!!! Achieved the desired result. Thanks alot for your help.
SFManiSFMani
I am also trying to achieve this result for multiple records by clicking a custom list view button. Could you please direct me how can this be achieved?