I have a site, when a guest user insert some data into a inputText it triggers a record creation (HojaEntrega__c object and child records from HojaEntregaItem__c object), then an attachment creation and this attachment. Then it sends by email to this user.
When I try this from visualforce (with Admin user) it works perfectly, I get the email with the attachment. But when I try from site, I get the email but the attachment is blank.
Into the visualforce I show a link to the PDF visualforce and the guest user can view the record as a PDF. This works fine.
This is the class that creates the record, the attachment and send the email.
public /*without sharing*/ class HojaEntregaManagementClass {
public static void createHojaEntregaRecordAndSendEmail(Order pedido, Account cliente, Account distribuidor){
system.debug('Método createHojaEntregaRecordAndSendEmail');
try{
HojaEntrega__c hEntregaCreada = createHojaEntregaSFRecord(cliente, distribuidor, pedido);
Id hEntregaId = hEntregaCreada.Id;
//GenerateCustomPDFAndEmail(hEntregaId, cliente, distribuidor);
GenerateCustomPDF(hEntregaId);
//actualizarHojaEntrega(hEntregaCreada);
}catch(Exception ex){
System.debug('Exception: ' + ex + ', getLineNumber: ' + ex.getLineNumber());
}
}
public static HojaEntrega__c createHojaEntregaSFRecord(Account cliente, Account distribuidor, Order pedido){
system.debug('Método createHojaEntregaSFRecord');
try{
//Crear registro de hoja de entrega
String idUserDistribuidor = [SELECT Id FROM User WHERE Profile.Name =: 'accesoDistribuidoresHT_NewVersion Perfil' LIMIT 1].Id;
//String idOwnerPedido = [SELECT Id, OwnerId FROM Order WHERE Id =: Pedido.Id LIMIT 1].OwnerId;
HojaEntrega__c hojaEntregaCliente = new HojaEntrega__c();
hojaEntregaCliente.Name = 'HE-' + cliente.Name + '_Fecha-' + System.today();
hojaEntregaCliente.bsqHT_Cliente__c = cliente.Id;
hojaEntregaCliente.bsqHT_Distribuidor__c = distribuidor.Id;
hojaEntregaCliente.bsqHT_Pedido__c = pedido.Id;
System.debug('hojaEntregaCliente = ' + hojaEntregaCliente);
insert hojaEntregaCliente;
List<HojaEntregaItem__c> listaItemsHojaEntrega = new List<HojaEntregaItem__c>();
List<OrderItem> listaProductosPedido = [SELECT Id, bsqHT_Activo__c FROM OrderItem WHERE OrderId =: pedido.Id];
for(OrderItem item : listaProductosPedido){
if(item.bsqHT_Activo__c != null){
HojaEntregaItem__c itemHE =
new HojaEntregaItem__c (bsqHT_HojaEntrega__c = hojaEntregaCliente.Id, bsqHT_Activo__c = item.bsqHT_Activo__c, intHTQuantity__c = 1);
listaItemsHojaEntrega.add(itemHE);
}
}
System.debug('listaItemsHojaEntrega = ' + listaItemsHojaEntrega);
insert listaItemsHojaEntrega;
return hojaEntregaCliente;
}catch(Exception ex){
System.debug('Exception: ' + ex.getMessage() + 'error line: ' + ex.getLineNumber());
return null;
}
}
@Future(callout=true)
public static void GenerateCustomPDF(Id recordId){
PageReference defaultPage = new PageReference('/apex/PDFHojaEntregaHT');
system.debug('defaultPage = ' + defaultPage);
system.debug('despues de variable defaultPage');
defaultPage.getParameters().put('id', recordId);
Blob pageData; //variable to hold binary PDF data.
system.debug('después del blob');
if(!Test.isRunningTest()){ //for code coverage
system.debug('No es test');
system.debug('defaultPage.getContentAsPDF' + defaultPage.getContentAsPDF());
pageData = defaultPage.getContentAsPDF();
system.debug('después de pageData');
}else{
pageData = Blob.valueOf('This is a Test');
}
HojaEntrega__c hojaE = [SELECT Id, Name, bsqHT_Cliente__c, bsqHT_Distribuidor__c FROM HojaEntrega__c WHERE Id =: recordId];
Account cliente = [SELECT Id, name, strEmail__c FROM Account WHERE id =: hojaE.bsqHT_Cliente__c];
Account distribuidor = [SELECT Id, Name, strEmail__c FROM Account WHERE Id =: hojaE.bsqHT_Distribuidor__c];
String emailCliente = cliente.strEmail__c;
String emailDistribuidor = distribuidor.strEmail__c;
System.debug('IDS FOR SEND EMAIL: ' + recordId + '--' + 'email cliente: ' + emailCliente + 'email distribuidor: ' + emailDistribuidor);
system.debug('antes de crear el attachment');
//create Attachment
Attachment att = new Attachment(
ParentId = recordId,
Body = pageData,
Name = 'HojaEntrega_' + cliente.Name + '_' + DateTime.now().formatLong() + '.pdf');
system.debug('antes de insertar el attachment');
insert att;
system.debug('después de insertar el attachment');
//create and send email
Messaging.EmailFileAttachment mssgAtt = new Messaging.EmailFileAttachment();
mssgAtt.setBody(pageData);
mssgAtt.setFileName('hojaEntregacliente.pdf');
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject('Envio de Hoja de Entrega');
mail.setSenderDisplayName('Holiday Telecom');
mail.setToAddresses(new String[]{emailCliente, emailDistribuidor});
mail.setPlainTextBody('Here is your attachment.');
mail.setFileAttachments(new Messaging.EmailFileAttachment[]{mssgAtt});
emails.add(mail);
Messaging.sendEmail(emails);
}
public static void actualizarHojaEntrega(HojaEntrega__c hojaEntregaCreada){
system.debug('Método actualizarHojaEntrega');
try{
update hojaEntregaCreada;
}catch(Exception ex){
system.debug('Error en proceso HojaEntregaManagementClass.actualizarHojaEntrega: ' + ex.getMessage() + ', getLineNumber: ' + ex.getLineNumber()
+ ', getCause: ' + ex.getCause());
}
}
}
I think maybe can this because the body of the attachment isn't creating fine but I'm stuck and I'm not know what to try.
I edit with info about guest User configuration.