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
teknicsandteknicsand 

Future Handler error

I have a trigger which makes a callout to a controller which is using @future annotation. The controller then call a email class to send out the email with the pdf attachment. But I get the following error in the debug logs:

 

Status java.lang.NullPointerException Application Browser
Request Type Api Operation FutureHandler
Duration (ms) 528 Log Length 887
Log

20090225222643.115:Class.myOpportunityController.createSamplevfpdf: line 8, column 5: controller method called
20090225222643.115:Class.myOpportunityController.createSamplevfpdf: line 10, column 5: vf opp id: 0068000000M1lOCAAZ
20090225222643.115:Class.myOpportunityController.createSamplevfpdf: line 11, column 5: vf opp name: United Oil Installations
20090225222643.115:Class.myOpportunityController.createSamplevfpdf: line 12, column 5: vf con id: 0038000000UJtnFAAT
20090225222643.115:Class.myOpportunityController.createSamplevfpdf: line 13, column 5: vf con name: Stella Pavlova
20090225222643.115:Class.myOpportunityController.createSamplevfpdf: line 14, column 5: vf con email: spavlova@uog.com
System.VisualforceException: java.lang.NullPointerException

Class.sendPDFEmailClass.sendmyEmail: line 8, column 20
Class.myOpportunityController.createSamplevfpdf: line 16, column 5

 

Trigger:

 

 

trigger OpportunityVFEmail on Opportunity (after insert, after update)
{

for(Opportunity a: Trigger.new){

System.debug('id is: ' + a.Id);

System.debug('name is : ' + a.Name);

System.debug('same custom field: ' + a.same__c);

if (a.stageName == 'Closed Won')
{
Contact con1 = [select id, name, email, accountid from Contact
where accountid = :a.accountid limit 1];

System.debug('contact id: ' + con1.id);
System.debug('contact name: ' + con1.name);
System.debug('contact email: ' + con1.email);
String con_id = con1.id;
String con_name = con1.name;
String con_email = con1.email;
System.debug('s contact id: ' + con_id);
System.debug('s contact name: ' + con_name);
System.debug('s contact email: ' + con_email);

String opp_id = a.id;
String opp_name = a.name;
System.debug('S opp name: ' + opp_id);
System.debug('S opp name: ' + opp_name);

myOpportunityController.createSamplevfpdf(a.id, a.name, con1.id, con1.name, con1.email);
}
}
}

 

 

Controller:

 

 

global class myOpportunityController {

@future (callout=true)
public static void createSamplevfpdf(ID id, String Opp_name, String Con_id, String Con_name, String Con_email)
{
Opportunity opp;
Contact con1;
System.debug('controller method called');

System.debug('vf opp id: ' + id);
System.debug('vf opp name: ' + Opp_name);
System.debug('vf con id: ' + Con_id);
System.debug('vf con name: ' + Con_name);
System.debug('vf con email: ' + Con_email);

sendPDFEmailClass.sendmyEmail(id,Opp_name, Con_id, Con_name, Con_email);
}

}

 

 Email Apex Class:

 

 

global class sendPDFEmailClass
{
public static void sendmyEmail(ID EOpp_id, String EOpp_name, String ECon_id, String ECon_name, String ECon_email)
{

PageReference samplevfpdf = Page.samplevfpdf;
samplevfpdf.getParameters().put('id',EOpp_id);
Blob pdf1 = samplevfpdf.getContent();


Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

String[] toAddresses = new String[] {ECon_email}; //get it from contacts
mail.setToAddresses(toAddresses);
mail.setReplyTo('user@salesforce.com');//sf user email add
mail.setSenderDisplayName('xxxxxxxxxx');//sf user name

mail.setSubject(' New Account Purchase Details' + 'Invoice no' + ': ' + ECon_name + ' ' + EOpp_name + ' : ' + EOpp_id);

mail.setBccSender(true);
mail.setUseSignature(true);

mail.setPlainTextBody('This is a test email');
mail.setHtmlBody('Welcome to ');

//Attachments

Messaging.EmailFileAttachment mailAttachment;
mailAttachment = new Messaging.EmailFileAttachment();
mailAttachment.setFileName('samplevfattachment.pdf');
mailAttachment.setBody(pdf1);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { mailAttachment });

//mail.setFileAttachments(fileAttachments);

//send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

}

 

 I'm not sure why I get this error? Is it related to the use of @future? Can anyone let me know how I should resolve it?

 

Thanks

 

 

Message Edited by teknicsand on 02-25-2009 02:52 PM
Ron HessRon Hess

The getContent

method for PageReference objects is not allowed in triggers.  

 

 

 

i suspect the same is true for at future from a trigger.

etoeto

I always get a NullPointerException in a @future-method when calling getContent. Here is a really easy example on how to reproduce this:

 




@future
public static void futureTest(){

System.debug('############################################FUTURE');

PageReference pdfPage = new PageReference( '/apex/test' );
//pdfPage.getParameters().put('id',billId);

System.debug(pdfPage);

Blob pdfblob = pdfPage.getContent(); //throws java nullpointer exception

}






<apex:page >
<!-- Begin Default Content REMOVE THIS -->
<h1>Congratulations</h1>
This is your new Page
<!-- End Default Content REMOVE THIS -->
</apex:page>



I tried to call it from a Button, which calls Apex code and from the execute anonymous feature in Eclipse. In both cases i get the Nullpointer.

 

Seems more to be a bug. If it's a non documented restriction I would expect to receive a apex exception instead of a java nullpointer.