第一次发送时附件为空白,但第二次通过 Apex 控制器发送时有数据 - SFDC NPSP
Attachment is blank when sent first time but has data when sent second time through Apex contoller - SFDC NPSP
我正在处理一项要求,我必须通过 SFDC NPSP 中的 Apex class 将 pdf 作为附件发送。所以以下是我的程序结构 -
VisualForcePage1 has two inputs – input1 and input2. The page also has
3 buttons – Preview, Send Email and Cancel. Depending on values of
input1 and input2, ApexClass1 computes values for say output1,
output2, output3, output4 and output5 using method getOutputMethod().
Values of these output1...5 variables are stored in a custom object
say Custom_Object__c.
public void getOutputMethod() {
// calculate values of output1...5
// store these values in Custom_Object__c
}
当用户单击预览按钮时,方法 previewPDF() 被调用,该方法又调用 getOutputMethod()。输出变量 (output1...5) 存储在 Custom_Object__c 中,然后控制被重定向到具有属性 renderAs = ‘pdf’ 的 VisualForcePage2。生成的pdf数据准确
当用户点击发送电子邮件按钮时,方法 emailPDF() 被调用,该方法又调用 getOutputMethod()。输出变量 (output1...5) 存储在 Custom_Object__c 中。 但是邮件中发送的附件pdf第一次没有数据。对于相同的输入值,附件 pdf 在第二次和随后的尝试中点击发送电子邮件按钮时具有数据。以下是将 pdf 作为电子邮件发送的代码片段 –
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Reference the attachment page and pass in the account ID
PageReference pdf = Page.VisualForcePage2;
pdf.getParameters().put('paramater1',input1);
pdf.getParameters().put('paramater2',input2);
pdf.setRedirect(true);
// Take the PDF content
Blob b = pdf.getContentAsPDF();
// Create the email attachment
String filename = 'myPage.pdf';
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(filename);
efa.setBody(b);
// Sets the paramaters of the email
String subject = 'Subject';
body = 'Hello';
email.setSubject(subject);
email.setToAddresses('example@email.com');
email.setPlainTextBody(body);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
ApexClass2 of VisualForcePage2 queries Custom_Object__c with input1
and input2 as parameters as below –
public ApexClass2() { // constructor
var1 = ApexPages.currentPage().getParameters().get('paramater1');
var2 = ApexPages.currentPage().getParameters().get('paramater2'); getCustomObject();
}
public List<Custom_Object__c> getCustomObject() {
List<Custom_Object__c> coList = new List<Custom_Object__c>([
SELECT field1, field2, field3, field4, field5
FROM Custom_Object__c
WHERE field1 =: var1
AND field2 =: var2 ]);
return coList;
}
请推荐。
问题已解决。发送附件的代码在更新之前从数据库中获取记录。因此,它正在选择空白或旧数据。
我在负责发送附件邮件的方法的开头添加了@future(callout=true)
。
我正在处理一项要求,我必须通过 SFDC NPSP 中的 Apex class 将 pdf 作为附件发送。所以以下是我的程序结构 -
VisualForcePage1 has two inputs – input1 and input2. The page also has 3 buttons – Preview, Send Email and Cancel. Depending on values of input1 and input2, ApexClass1 computes values for say output1, output2, output3, output4 and output5 using method getOutputMethod(). Values of these output1...5 variables are stored in a custom object say Custom_Object__c.
public void getOutputMethod() {
// calculate values of output1...5
// store these values in Custom_Object__c
}
当用户单击预览按钮时,方法 previewPDF() 被调用,该方法又调用 getOutputMethod()。输出变量 (output1...5) 存储在 Custom_Object__c 中,然后控制被重定向到具有属性 renderAs = ‘pdf’ 的 VisualForcePage2。生成的pdf数据准确
当用户点击发送电子邮件按钮时,方法 emailPDF() 被调用,该方法又调用 getOutputMethod()。输出变量 (output1...5) 存储在 Custom_Object__c 中。 但是邮件中发送的附件pdf第一次没有数据。对于相同的输入值,附件 pdf 在第二次和随后的尝试中点击发送电子邮件按钮时具有数据。以下是将 pdf 作为电子邮件发送的代码片段 –
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Reference the attachment page and pass in the account ID
PageReference pdf = Page.VisualForcePage2;
pdf.getParameters().put('paramater1',input1);
pdf.getParameters().put('paramater2',input2);
pdf.setRedirect(true);
// Take the PDF content
Blob b = pdf.getContentAsPDF();
// Create the email attachment
String filename = 'myPage.pdf';
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(filename);
efa.setBody(b);
// Sets the paramaters of the email
String subject = 'Subject';
body = 'Hello';
email.setSubject(subject);
email.setToAddresses('example@email.com');
email.setPlainTextBody(body);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
ApexClass2 of VisualForcePage2 queries Custom_Object__c with input1 and input2 as parameters as below –
public ApexClass2() { // constructor
var1 = ApexPages.currentPage().getParameters().get('paramater1');
var2 = ApexPages.currentPage().getParameters().get('paramater2'); getCustomObject();
}
public List<Custom_Object__c> getCustomObject() {
List<Custom_Object__c> coList = new List<Custom_Object__c>([
SELECT field1, field2, field3, field4, field5
FROM Custom_Object__c
WHERE field1 =: var1
AND field2 =: var2 ]);
return coList;
}
请推荐。
问题已解决。发送附件的代码在更新之前从数据库中获取记录。因此,它正在选择空白或旧数据。
我在负责发送附件邮件的方法的开头添加了@future(callout=true)
。