• Jaymin Patel
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Apex Class:

global with sharing class InstallationServiceReportController {

   public Injection_Moulding_Machine_Installation__c ISR;
   public String imageURL{get;set;}
   
public InstallationServiceReportController(ApexPages.StandardController controller) {
ISR = (Injection_Moulding_Machine_Installation__c)controller.getRecord(); 
    
imageURL='/servlet/servlet.FileDownload?file=';
    
List<Attachment> AttachmentList=[select Id, Name, LastModifiedDate FROM Attachment WHERE Name =:ISR.Id ORDER BY LastModifiedDate DESC NULLS LAST];
       
if( AttachmentList != null &&AttachmentList.size()>0)

  {

   imageURL=imageURL+AttachmentList[0].id;
   }
}

public pageReference clear(){
    pageReference page = new PageReference('/'+ISR.id);
  page.setRedirect(false);
   return page;

  
public PageReference savePDF() {
    PageReference pageRef = new PageReference('/apex/InstallationServiceReport');
     List<Attachment> AttachmentList=[select Id, Name, LastModifiedDate FROM Attachment WHERE Name =:ISR.Id];
     if( AttachmentList.size() == 0){
      ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'First Capture Customer Signature and then try to Generate Report'));
      
          Return pageRef; 
        }   
     else{
      PageReference pdf = new PageReference('/apex/InstallationServiceReport');
      pdf.getParameters().put('id',ISR.Id);
Blob pdfBlob = pdf.getContentasPDF();
       
    // add parent id to the parameters for standardcontroller
    // create the new attachment
    Contentversion CV = new Contentversion();
    // the contents of the attachment from the pdf
    Blob body;

    try {
        // returns the output of the page as a PDF
        body = pdf.getContentasPDF();

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

    CV.VersionData = body;
    CV.Title = ISR.Shop_Order_Number__c+'-Machine Installation Report';
    
    // attach the pdf to the WorkOrder
    CV.FirstPublishLocationId = ISR.Id;
    CV = new ContentVersion (FirstPublishLocationId = ISR.Id,Title = CV.Title,PathOnClient=CV.Title+'.pdf', VersionData = pdf.getContentasPDF());
      insert CV;

    // send the user to the account to view results
    return new PageReference('/'+ISR.Id);
    }
    }
 
public pageReference cancel(){
    pageReference page = new PageReference('/'+ISR.id);
    page.setRedirect(true);
    return page;
}
  }