1

I am trying to call a method from a trigger that will generate a pdf whenever any order(object) record is created. I wrote a after insert, after updated trigger and called the method for pdf generation like below.

  for (Order_vod__c ord : trigger.new)
  {
      if (ord != null)
      {
           PDFExport_OrderDetails exp= new  PDFExport_OrderDetails();
          exp.send_order_pdf(ord.id);
      }
  }


  //method body
  public PageReference send_order_pdf(Id ordId)
  {
      List<String> toAddress = new List<String>();
      String orderId ;
      if(ApexPages.currentPage().getParameters().get('Id') != null)
      {
        orderId = ApexPages.currentPage().getParameters().get('Id');  
      }
      Order_vod__c order =[SELECT Id,Name,Owner.Email,Account_vod__r. Email__c From Order_vod__c WHERE Id=:orderId ];
      Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
      PageReference pdf;
      pdf = new PageReference('/apex/View_Order_vod?id='+orderId);
      pdf.getParameters().put('id',orderId); 
      pdf.setRedirect(true);
      Blob b = pdf.getContent(); 
      System.debug('Blob ****'+b);

      Messaging.EmailFileAttachment fileAttachmt = new Messaging.EmailFileAttachment();
      fileAttachmt.setFileName('attachment.pdf');
      fileAttachmt.setBody(b);
      System.debug('fileAttachmt****'+fileAttachmt);
      String address ;

     if(order.Owner.Email!= null)
     {
        address= order.Owner.Email;
     }
     //String[] toAddress=address.split(',',0);
     System.debug('Rep email id****'+address);
     toAddress.add(address);
     toAddress.add('abc@xyz.com');
     email.setToAddresses(toAddress);
     System.debug('email*****'+email);
     email.setPlainTextBody('Please find the Order'+order.Name+' as an attachment');
     email.setFileAttachments(new Messaging.EmailFileAttachment[] {fileAttachmt});
     // Sends the email
     Messaging.SendEmailResult [] r = 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});    
     return null;
 } 

but I am getting an error "System.NullPointerException: Attempt to de-reference a null object". Although I kept the checking ord != null, not sure what is causing this error. Kindly suggest.

  • Where are you getting the null dereference? What debug output are you seeing? ord is never going to be null as the trigger wouldn't process a null record reference. – Matt Lacey Apr 16 '14 at 4:31
3

You are passing parameter ordId to the method send_order_pdf() and not using it. you are using orderId in the query.
That is giving you null order and you might be getting NullPointerException at line if(order.Owner.Email!= null) because you are trying to access owner of null.

  • hi, corrceted that. Now I am getting the same error on ----if(ApexPages.currentPage().getParameters().get('Id') != null) – user4644 Apr 16 '14 at 4:36
  • You can change your approach. Just get the id in the constructor and pass that id to your method so its behavior remains same in both cases ( i.e. called from trigger or from vf page) – Sven Apr 16 '14 at 4:46
  • Hi Sven- I used the below lines in constructor only ------ public PFE_PDFExport_OrderDetails() { if(ApexPages.currentPage().getParameters().get('Id') != null) { oId = ApexPages.currentPage().getParameters().get('Id'); System.debug('**** ORDER ID **** '+oId); and getting error on - " oId = ApexPages.currentPage().getParameters().get('Id'); " – user4644 Apr 16 '14 at 5:01
  • is old declared as instance variable? – Sven Apr 16 '14 at 5:10
  • can you post separate question for that with error msg? – Sven Apr 16 '14 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy