+ Start a Discussion
Ryan GreeneRyan Greene 

get id from void method and redirect to newly created id

Hello,
I have a button on my page which runs a VF Page with an extension to my controller. It works to press the button and it produces the EchoSign Agreement as directed. However, the page it goes to when I click the button is a blank page. I would like to redirect it to the Id of the record I just created upon pressing the button. Basic outline of code below. So how can I get a void method to redirect to the ID of the record it just created?
public void SendPdf(){
//reference PDF Creator VF Page
pageReference pdfPage = Page.ContactPDF;
pdfPage.getParameters().put('Id',getid);
blob pdfBody;
pdfBody = pdfPage.getContentAsPDF();
          
// Build Echosign agreement record
echosign_dev1__SIGN_Agreement__c agreementRec = new echosign_dev1__SIGN_Agreement__c();
agreementRec.Name = 'Credit Info Release for: ';
//other variables entered here
insert agreementRec;

// insert the pdf as an attachment
attachment pdfFile = new attachment();
pdfFile.isPrivate = false;
pdfFile.body = pdfBody;
pdfFile.parentId = agreementRec.id;
pdfFile.Name = 'Agreement.pdf';
insert pdfFile;
}

 
Best Answer chosen by Ryan Greene
Gaurav Jain 7Gaurav Jain 7
Update your code as below

Mark it as Best Answer, if it helps​​
public PageReference SendPdf(){
//reference PDF Creator VF Page
pageReference pdfPage = Page.ContactPDF;
pdfPage.getParameters().put('Id',getid);
blob pdfBody;
pdfBody = pdfPage.getContentAsPDF();
          
// Build Echosign agreement record
echosign_dev1__SIGN_Agreement__c agreementRec = new echosign_dev1__SIGN_Agreement__c();
agreementRec.Name = 'Credit Info Release for: ';
//other variables entered here
insert agreementRec;

// insert the pdf as an attachment
attachment pdfFile = new attachment();
pdfFile.isPrivate = false;
pdfFile.body = pdfBody;
pdfFile.parentId = agreementRec.id;
pdfFile.Name = 'Agreement.pdf';
insert pdfFile;

PageReference pageref = new PageReference('/' + agreementRec.id);
pageref.setRedirect(true);
return pageref;
		
}

:

 

All Answers

Gaurav Jain 7Gaurav Jain 7
Update your code as below

Mark it as Best Answer, if it helps​​
public PageReference SendPdf(){
//reference PDF Creator VF Page
pageReference pdfPage = Page.ContactPDF;
pdfPage.getParameters().put('Id',getid);
blob pdfBody;
pdfBody = pdfPage.getContentAsPDF();
          
// Build Echosign agreement record
echosign_dev1__SIGN_Agreement__c agreementRec = new echosign_dev1__SIGN_Agreement__c();
agreementRec.Name = 'Credit Info Release for: ';
//other variables entered here
insert agreementRec;

// insert the pdf as an attachment
attachment pdfFile = new attachment();
pdfFile.isPrivate = false;
pdfFile.body = pdfBody;
pdfFile.parentId = agreementRec.id;
pdfFile.Name = 'Agreement.pdf';
insert pdfFile;

PageReference pageref = new PageReference('/' + agreementRec.id);
pageref.setRedirect(true);
return pageref;
		
}

:

 
This was selected as the best answer
Ryan GreeneRyan Greene
Are you kidding me?! Why did I have it as VOID??

Works great, thank you!