function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alice JuAlice Ju 

Auto generate Quote pdf file using Javascript button

Hi there,
I'm trying to create a function that will allow the user to select multiple opportunities in the opportunity list view, and then click a button to generate all the quote s and PDF files. The button looks liks this

{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}


var idsToInsert= {!GETRECORDIDS( $ObjectType.Opportunity )};

if (idsToInsert.length) {

// Now make a synchronous call to the Apex Web service
// method
var result = sforce.apex.execute(
"MassQuoteInsert", // class
"insertNotes", // method
{iParentIds : idsToInsert // method arguments
}
);
{
window.alert(idsToInsert.length+" quote inserted.");

}
} else if (idsToInsert.length == 0) {
alert("Please pick the opportunities");
}

The Apex class is pasted below. The line "Blob pdfBlob = pr.getContent();" is causing a problem. I get an error message like this. Can some one be so kind and take a look for me? Thank you in advance!

Error message
User-added image

global class MassQuoteInsert{

 

  WebService static Integer insertNotes(Id[] iParentIds)
  {
    string TemplateId = '0EHU0000000LPpL';
    List<Quote> qts = new List<Quote>();
    List<QuoteLineitem> qtlines = new List<QuoteLineitem>();
    List <Opportunity> opts = [SELECT id, AccountId, Name, Type, CloseDate, SyncedQuoteId, Renewal_Due_Date__c, Pricebook2Id
                                from Opportunity where Id IN :iParentIds ];
    //Get all the contact roles
    List <OpportunityContactRole> OptContacts = [SELECT OpportunityId, Id, ContactId, IsPrimary from OpportunityContactRole where OpportunityId in : iParentIds];
    //Sort contact roles according to opportunity id   
    Map<Id, List<OpportunityContactRole>> OptContactMap= new Map<Id, List<OpportunityContactRole>>();   
    for(OpportunityContactRole OptContact : OptContacts)
    {
        if(OptContactMap.get(OptContact.OpportunityId)!=NULL)
           OptContactMap.get(OptContact.OpportunityId).add(OptContact);
        else          
        {
            List<OpportunityContactRole> TempContactRole = new List<OpportunityContactRole>();
            TempContactRole.add(OptContact);
            OptContactMap.put(OptContact.OpportunityId, TempContactRole);
        }
    }
    //Get all the opportunity line item
    List <OpportunityLineitem> OptProducts = [SELECT OpportunityId, PricebookEntryId, Quantity, UnitPrice, PricebookEntry.Product2Id, Subtotal,TotalPrice from OpportunityLineitem where OpportunityId IN : iParentIds];
    Map<Id, List<OpportunityLineitem>> OptProductMap= new Map<Id, List<OpportunityLineitem>>();       
    for(OpportunityLineitem OptProduct : OptProducts)
    {
        if(OptProductMap.get(OptProduct.OpportunityId)!=NULL)
           OptProductMap.get(OptProduct.OpportunityId).add(OptProduct);
        else          
        {
            List<OpportunityLineitem> TempProduct = new List<OpportunityLineitem>();
            TempProduct.add(OptProduct);
            OptProductMap.put(OptProduct.OpportunityId, TempProduct);
        }
    }
    //Finish all the preparation
    //Creating quotes   
   
    for (Opportunity opt : opts){

        Quote TempQuote = new quote( NAME = opt.Name, OpportunityId = opt.id, Valid_for_days__c = 15, Valid_For__c = '15',
        Status = 'Auto Generated',
        Rep_Name__c = UserInfo.getUserId(),
        Quote_Type__c = 'New Revenue',
        Pricebook2Id = opt.Pricebook2Id
        //,Description = string.valueof(opt.Renewal_Due_Date__c)
        );
        //Renewal Quote type

        If(opt.type == 'Renewal')
        TempQuote.Quote_Type__c = 'Renewal';

       
        //Renewal Due Date customization
        Date maintdate;
        if(opt.Renewal_Due_Date__c != NULL)
            maintdate = opt.Renewal_Due_Date__c;
        else           
            maintdate = opt.CloseDate;
        TempQuote.Description = 'Maintenance: '+string.valueof(maintdate.Month())+'/'+string.valueof(maintdate.Day())+'/'+string.valueof(maintdate.Year())+' to '+string.valueof(maintdate.Month())+'/'+string.valueof(maintdate.Day())+'/'+string.valueof(maintdate.adddays(365).Year());
//        TempQuote.Description_dd_mm_yy__c = 'Maintenance: '+string.valueof(maintdate.Day())+'/'+string.valueof(maintdate.Month())+'/'+string.valueof(maintdate.Year())+' to '+string.valueof(maintdate.Day())+'/'+string.valueof(maintdate.Month())+'/'+string.valueof(maintdate.adddays(365).Year());

        //Find out who's the contact
        List<OpportunityContactRole> QuoteContact = OptContactMap.get(opt.id);
        if(QuoteContact!=NULL)
        {
            integer i =0;
            While(TempQuote.Bill_to__c == NULL && i < QuoteContact.size())
            {
                if(QuoteContact[i].IsPrimary == true)
                    TempQuote.Bill_to__c = QuoteContact[i].ContactId;
                i++;
            }
            i=0;
            While(TempQuote.Bill_to__c == NULL && i < QuoteContact.size())
                    TempQuote.Bill_to__c = QuoteContact[i].ContactId;            
            if(TempQuote.Bill_to__c!=NULL)
            TempQuote.Ship_to__c = TempQuote.Bill_to__c;
        }     
       
       
        qts.add(TempQuote);       
       
       
    }
 
   
   
    insert qts;
   List<QuoteDocument> sr = new List<QuoteDocument>();
    for (Quote q: qts)
        {
                 PageReference pr = new PageReference('/quote/quoteTemplateDataViewer.apexp?id='+q.id+'&'+'summlid='+TemplateId); // link from answer by techtrekker
                 Blob pdfBlob = pr.getContent();

                 sr.add (new QuoteDocument(
                 QuoteId = q.Id,
                 document = pdfBlob)); 
         }
   insert sr;


    for (Opportunity opt : opts){   
        for(Quote qt:qts)
        {
        if(qt.OpportunityId == opt.id)
        {
            opt.SyncedQuoteId = qt.id;
            List<OpportunityLineItem> QuoteProduct = OptProductMap.get(opt.id);
            if(QuoteProduct!=NULL)
            {
                for(OpportunityLineitem optline:Quoteproduct)
                {
                    Quotelineitem templineitem = new QuoteLineitem(
                    QuoteId = qt.id,
                    UnitPrice = optline.UnitPrice,
                    Quantity = optline.Quantity,
                    PricebookEntryId = optline.PricebookEntryId,
                    Product2Id = optline.PricebookEntry.Product2Id
                    );
                    qtlines.add(templineitem);
                }           
//                PageReference pr = new PageReference('/quote/quoteTemplateDataViewer.apexp?id='+qt.id+'&'+'summlid='+TemplateId); // link from answer by techtrekker
//                Blob pdfBlob = pr.getContent();                       
//                Attachment a = new Attachment(parentId = qt.id, name=qt.name + '.pdf', body = pdfBlob);
//                insert a;       
               
            }      


        }
                   
        }
    }
     insert qtlines;
    update opts;
Sonam_SFDCSonam_SFDC
Hi,

I quickly went through your code - the usage of getContent() looks correct to me - I also copied the code to a word file and for me the error message shown at line #99 is the following line:
qts.add(TempQuote);

Could you please check if the above line in code is working for you
Vandana RattanVandana Rattan
Alice have you been able to resolve the error? I am in a same situation as yours nd would appreciate any help.