I'm trying to generate and auto-send a quote to a community User when they Close Won an Opportunity.

I have a Flow that calls my apex class. I've debugged while logged in as the user and the error I'm seeing is below when the class attempts to insert the PDF document.



Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.: [NetworkId]


public without sharing class QuoteEmail {

    public static void QuoteCreateAndEmail(List < Id > quoteIds) {

        List < Quote > quoteList = [SELECT ID, Opportunity.Primary_Contacts_Email__c, Opportunity.ContactId  FROM Quote WHERE ID =: quoteIds];

        //If a Quote is found lets generate a new Qutote PDF
        if (!quoteList.isEmpty()) {

    //Generates a Quote PDF and associates it to the Quote
    public static void CreateQuotePDF(Id quoteID) {

        AutoQuoteEmail__mdt emailInfo = [SELECT Id, Email_Template_ID__c, Quote_Template_ID__c FROM AutoQuoteEmail__mdt WHERE MasterLabel = 'Master'];
        Quote newQuote = [SELECT ID, Opportunity.Primary_Contacts_Email__c, Opportunity.ContactId  FROM Quote WHERE ID = :quoteID];
        //This Url creates the pdf for quote
        String quoteUrl = '/quote/quoteTemplateDataViewer.apexp?id=';
        quoteUrl += quoteID;
        quoteUrl += '&headerHeight=190&footerHeight=188&summlid=';
        quoteUrl += emailInfo.Quote_Template_ID__c;
        quoteUrl += '#toolbar=1&navpanes=0&zoom=90';

        //Create pdf content
        PageReference pg = new PageReference(quoteUrl);

        //Document object of quote which holds the quote pdf
        QuoteDocument quotedoc = new QuoteDocument();

        //Get the content of Pdf.
        Blob b = pg.getContentAsPDF();

        //content assign to document
        quotedoc.Document = b;

        //assign quote id where pdf should attach
        quotedoc.QuoteId = QuoteID;

        //insert the quotdoc
        insert quotedoc;
        SendEmail(newQuote.id, emailInfo.Email_Template_ID__c, newQuote.Opportunity.Primary_Contacts_Email__c, newQuote.Opportunity.ContactId, quotedoc.Id);

    public static void SendEmail(Id quoteID, String emailTemplateID, String emailAddress, Id contactID, Id quotedocID) {

        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setSenderDisplayName('Company Support');
        message.setReplyTo('[email protected]');
        message.setWhatId(quoteID); //This is important for the merge fields in template to work
        message.toAddresses = new String[]{emailAddress};
        //Get and attach the Quote Document
        List<QuoteDocument> QDs = [SELECT Name, Document, ContentVersionDocumentId FROM QuoteDocument WHERE QuoteId = :quoteID];   
        List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
        for (QuoteDocument QD: QDs) {
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        Messaging.SingleEmailMessage[] messages = new List < Messaging.SingleEmailMessage > {
        Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

        if (results[0].success) {
            System.debug('The email was sent successfully.');
        } else {
            System.debug('The email failed to send: '+results[0].errors[0].message);

  • verify user has access to quote, oppo, account and salesforce content
    – cropredy
    May 5, 2021 at 0:22
  • I've confirmed the user has access to the Quote, Opp, Account and Salesforce CRM Content User is set to TRUE May 5, 2021 at 0:45
  • Also while logged into the community as the user I'm able to generate the Quote PDF manually May 5, 2021 at 1:26


You must log in to answer this question.

Browse other questions tagged .