You need to sign in to do that
Don't have an account?
arti rathod
vf attchmenet via apex not working for community guest user salesforce
This is my below code on insertion of lead record community guest user unable to open pdf attchment although from backend when i am passing lead id its working fine --
public static void sendEmail(Id leadId,String leadStatus){
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
EmailTemplate emailTemp = new EmailTemplate();
String emailTempDevName;
if(leadStatus == System.Label.PartExp_LeadStatusOpen){
emailTempDevName= System.Label.PartExp_EmailTempName;
}else if(leadStatus == System.Label.PartExp_LeadStatusRejected){
emailTempDevName= System.Label.PartExp_RejectedEmailTempName;
}
emailTemp =[Select id, Subject, Body From EmailTemplate Where DeveloperName =:emailTempDevName];
OrgWideEmailAddress orgWideEmailAddress = [SELECT Id,DisplayName FROM OrgWideEmailAddress WHERE DisplayName =: System.Label.PartExp_OWDForPartnerEmail Limit 1];
Lead leadRecord = [SELECT Id, FirstName, LastName, Email FROM Lead WHERE Id = :leadId Limit 1];
// PageReference customPage = Page.PartExp_PrintViewRegistrationForm;
PageReference customPage = new pagereference('/apex/PartExp_PrintViewRegistrationForm?id='+leadId);
customPage.getParameters().put('id', leadRecord.Id);
System.debug('leadId:'+leadId);
Blob pdfBlob;
try {
pdfBlob = customPage.getContentAsPDF();
System.debug('pdfBlob'+pdfBlob);
} catch (VisualforceException e) {
pdfBlob = Blob.valueOf('data');
System.debug('error dedatil' + e.getStackTraceString());
System.debug('Error generating PDF: ' + e.getMessage());
// return;
}
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setFileName('LeadDetails.pdf');
attach.setBody(pdfBlob);
attach.Body = pdfBlob;
System.debug('attach#'+attach);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(emailTemp.Id);
mail.setTargetObjectId(leadId);
mail.setOrgWideEmailAddressId(orgWideEmailAddress.Id);
mail.setFileAttachments(new List<Messaging.EmailFileAttachment>{ attach });
emailList.add(mail);
if(!emailList.isEmpty()){
Messaging.sendEmail(emailList);
}
public static void sendEmail(Id leadId,String leadStatus){
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
EmailTemplate emailTemp = new EmailTemplate();
String emailTempDevName;
if(leadStatus == System.Label.PartExp_LeadStatusOpen){
emailTempDevName= System.Label.PartExp_EmailTempName;
}else if(leadStatus == System.Label.PartExp_LeadStatusRejected){
emailTempDevName= System.Label.PartExp_RejectedEmailTempName;
}
emailTemp =[Select id, Subject, Body From EmailTemplate Where DeveloperName =:emailTempDevName];
OrgWideEmailAddress orgWideEmailAddress = [SELECT Id,DisplayName FROM OrgWideEmailAddress WHERE DisplayName =: System.Label.PartExp_OWDForPartnerEmail Limit 1];
Lead leadRecord = [SELECT Id, FirstName, LastName, Email FROM Lead WHERE Id = :leadId Limit 1];
// PageReference customPage = Page.PartExp_PrintViewRegistrationForm;
PageReference customPage = new pagereference('/apex/PartExp_PrintViewRegistrationForm?id='+leadId);
customPage.getParameters().put('id', leadRecord.Id);
System.debug('leadId:'+leadId);
Blob pdfBlob;
try {
pdfBlob = customPage.getContentAsPDF();
System.debug('pdfBlob'+pdfBlob);
} catch (VisualforceException e) {
pdfBlob = Blob.valueOf('data');
System.debug('error dedatil' + e.getStackTraceString());
System.debug('Error generating PDF: ' + e.getMessage());
// return;
}
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setFileName('LeadDetails.pdf');
attach.setBody(pdfBlob);
attach.Body = pdfBlob;
System.debug('attach#'+attach);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(emailTemp.Id);
mail.setTargetObjectId(leadId);
mail.setOrgWideEmailAddressId(orgWideEmailAddress.Id);
mail.setFileAttachments(new List<Messaging.EmailFileAttachment>{ attach });
emailList.add(mail);
if(!emailList.isEmpty()){
Messaging.sendEmail(emailList);
}
Make sure that the guest user has all the necessary permissions to access and create attachments.
You can refer to below stackexchange link which can help you:
https://salesforce.stackexchange.com/questions/210009/create-a-pdf-attachment-when-you-are-a-guest-user
If this information helps, please mark the answer as best. Thank you