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
chubsubchubsub 

Quote Trigger to auto-generate Quote Document

Is it possible to auto-generate a quote pdf based on changes to the quote line items.  For example, a quote and quote line items have already been create, and a quote pdf has been generated and attached to the quote and is shown in the quote pdf related list.  Now, the user goes back and changes values in the quote line items.  Is there a way to automatically generate a quote pdf after the user makes these changes to the line items?   Meaning, another quote pdf will appear in the related list based off the changes made to the line items?

 

I'm currently trying to see if it's possible to generate a Quote Document with a trigger on the Quote, below is some code for the trigger, but I'm stuck at specifying the template name, the only template I have access to is a template I created called "Testing"  :

 

Can this even be done? If so, how would I specify the template to use?

 

Thanks in advance!

 

trigger createQuotePDF on Quote (after update) {
List<QuoteDocument> sr = new List<QuoteDocument>();

    for (Quote newQuote: Trigger.New)
         if (newQuote.Quote_Approval_Status__c == 'Quote on Hold'){
                 sr.add (new QuoteDocument(
                 Template = 'Testing'));  
         }
   insert sr;
 }



chubsubchubsub

Ok, I'm trying to accomplish this in a developer org that only has a standar template, I got this far before receiving this error message, which I can't find anything about:

REQUIRED_FIELD_MISSING, This field is required: [VersionData]:

 

trigger createQuotePDF on Quote (after update) {

List<QuoteDocument> sr = new List<QuoteDocument>();
    for (Quote q: Trigger.new)
         if (q.Status == 'Draft'){
                 sr.add (new QuoteDocument(
                 QuoteId = q.Id));  
         }
   insert sr;
 }

vishal@forcevishal@force
trigger createQuotePDF on Quote (after update) {

List<QuoteDocument> sr = new List<QuoteDocument>();
    for (Quote q: Trigger.new)
         if (q.Status == 'Draft'){
                 sr.add (new QuoteDocument(
                 QuoteId = q.Id,
                  VersionData = 'someValue'));  
         }
   insert sr;
 }

 you are inserting QuoteDocument, for which there is a required field VersionData, so you need to assign some value to that field to before you insert your list. See in the code i posted, i have added the field. just give it appropriate value based on the fieldtype. 

 

hope this helps.

chubsubchubsub

Thanks Vishal, I have found that the field type for VersionData is base64, which I also understand is a Blob type.

 

I'm thinking that setting the VersionData field from the Blob value of the Quote object may do the trick.  But i'm not sure how to do that.

wisconsamwisconsam

The VersionData field didn't appear to be present in the object, but the 'document' field existed and seemed to represent similar data.  Modifying the trigger to something similar to the following seemed to get past the error and produce the desired results:

 

trigger createQuotePDF on Quote (after update) {

List<QuoteDocument> sr = new List<QuoteDocument>();
    for (Quote q: Trigger.new)
         if (q.Status == 'Draft'){
                 sr.add (new QuoteDocument(
                 QuoteId = q.Id,
                 document = Blob.toPDF('Your Contents Here')));  
         }
   insert sr;
 }

 

Addressing the original question, I am assuming we are forced to manually build the string that represents the contents of the quote template to make this useful.  This seems best suited for a visualforce page rather then inside the trigger itself.