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');
        HojaEntrega__c hEntregaCreada = createHojaEntregaSFRecord(cliente, distribuidor, pedido);
        Id hEntregaId = hEntregaCreada.Id;
        //GenerateCustomPDFAndEmail(hEntregaId, cliente, distribuidor);
    }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');
        //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);
        System.debug('listaItemsHojaEntrega = ' + listaItemsHojaEntrega);
        insert listaItemsHojaEntrega;

        return hojaEntregaCliente;

    }catch(Exception ex){
        System.debug('Exception: ' + ex.getMessage() + 'error line: ' + ex.getLineNumber());
        return null;

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');
        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();

    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});


public static void actualizarHojaEntrega(HojaEntrega__c hojaEntregaCreada){
    system.debug('Método actualizarHojaEntrega');
        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.

enter image description here

enter image description here

enter image description here

  • 1
    Did you give VF Page and Object access to guest site user profile? Jun 8, 2020 at 9:12
  • Yes, I have permission for object and visualforces and classes Jun 8, 2020 at 9:24
  • 1
    I think the issue is here ' PageReference defaultPage = new PageReference('/apex/PDFHojaEntregaHT');' guest site user will refer to this page differently . can you expose that page to an URL in focce.com site and then try creating pdf? Jun 8, 2020 at 11:01
  • @PranayJaiswal from the site, I put a link to the page and this is the url xxx.cs84.force.com/accesoDistribuidoresHT/… I have tried putting this: PageReference defaultPage = PageReference('/accesoDistribuidoresHT/pdfhojaentregaht'); defaultPage.getParameters().put('id', recordId); But I get an 'Salesforce Internal Error' Jun 8, 2020 at 16:29
  • Thanks @PranayJaiswal you were right! Jun 9, 2020 at 6:29

1 Answer 1


I had changed the pageReference:

PageReference defaultPage = new PageReference('https://xxxxx.force.com/accesoDistribuidoresHT/pdfhojaentregaht');

It's hardcode and I think this is not correct but it works!

PS: I tried with

PageReference defaultPage = Page.Pdfhojaentregaht;

But this doesn't work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .