Generating a Document: Error generating document: Bad Request

If you’ve ever got this error when generating a document, you probably have less hair than you did before generating the document.

There are multiple reasons this could happen:

  • HTML tags in a Quote Line Line Column field in the lines section Template Content.
  • One of your HTML Template Content records has a reference to an image that has a bad SRC (does not exist, unable to load, no permission to access the image, etc).
  • The markup inside your Custom VisualForce Page Template Content is bad. Below is a list of supported tags per this Salesforce article.
    • Supported HTML Tags
      • html
      • br
      • div
      • p
      • em
      • li
      • ol
      • strike
      • strong
      • b
      • i
      • u
      • ul
      • a
      • img
      • span
      • sub
      • sup
    • Supported HTML Tag Attributes
      • style
      • href
      • src
      • alt

HTML tags in a Quote Line Line Column Field
You have a standard lines section. You have a list of Line Columns on your Quote Template. One of those Line Column fields on one or more of your Quote Line records on the offending Quote has a value that contains HTML tags. A frequent culprit of this is the Description field. It could also be some custom field you made and pulled into your template via a Line Column.

What to do? Do an export of the Fields from your Line Columns of the Quote Line records from the offending Quote. You can do this with Data Loader or with the Developer Console.

The Image Thing
You might have some HTML referencing an image that no longer existed or become corrupt (the url for the image just shows a blank page or some other corrupt image URL). Example:

<img alt="logo" height="100" src="" width="180" />

What to do? Create a new image and update the HTML to use the new URL.

Also! Do you have a Logo Document ID set on your Quote Template? Check the document record for that ID and make sure it has Externally Available Image set to TRUE.

Custom VisualForce Page Template Content
The markup of the content is bad. You have to go in and make sure that all tags that are non -apex tags are not HTML but XSL:FO instead.

Sometimes, when displaying rich text fields (such as Product Description) in a VisualForce Page Template Content, HTML tags are in the rich text. But you can not have HTML tags.

What to do? Put this in your controller:

// do this to replace all html tags in a rich text field.
richTextFieldValue = richTextFieldValue.replaceAll('<[^>]+>',' ');

Ampersand! You can’t have a & or a &amp; in your markup. Don’t do it. Do this instead:


Thanks to Ashley for her input on this article!

16 thoughts on “Generating a Document: Error generating document: Bad Request

  1. will you please explain how to use :
    // do this to replace all html tags in a rich text field.
    richTextFieldValue = richTextFieldValue.replaceAll(‘]+>’,’ ‘);

    I have a vf page which is used cpq quote template. I am querying product fields from quotelines.Product has rich text field which i want to display in quote template.

  2. Hello Harshada,

    The code you mention is used to remove HTML tags so you do not get the above error. For an actual value it would be something like:

    richTextFieldValue = quotelines[i].SBQQ__Product__r.Description.replaceAll(‘]+>’,’ ‘);

    Then you display richTextFieldValue in your vf page.

  3. I am receiving this error from the basic “Line Item” section. I have tested the other sections and they work, but when a basic line item section is included, I receive the error. I was using the default line columns.

    Any thoughts?

    1. Hey Blake,

      Could be the Description column. Check your Product record Description to see if there are any HTML tags in it. Also check the Quote Line Description field for HTML tags.

  4. I am learning CPQ and preparing for my CPQ Specialist Certification. Thanks to your blog I was able to easily troubleshoot the Error.

  5. Im building a section with a custom template, calling a vf. But im not being able to get the Record id of the quote on my custom controller. and the preview gives me the bad request error.

    Ive searched how to build this but i cant find it. The custom controller works fine outside this custom template when i render it as a pdf. But using the preview document from cpq i cant get the record id. When i erase that line and write an “hello wolrd” it works fine

    Can you help me with an controller example for this kind of VF?

    1. The first line of the constructor method of your controller should be:

      // Get Quote Id
      Id quoteId = (Id)ApexPages.currentPage().getParameters().get(‘qid’);

  6. Hi DP,

    Appreciate your efforts which help me to display the rich text field content on the CPQ document using the VF page, however, with the logic above we lost the formatting. Please let me know if there is better way to achieve simillar formatting like bullet points line breaks etc.

    Prabhakar Sharma.

    1. You would need to implement a format converter instead of using the replacement code above. Converting specific HTML tags and attributes to specific XSL:FO tags and attributes.

  7. I’m getting a “Bad Request” error when I try to generate a quote document. This started after I added a quote template section with HTML. The HTML looks like this, are there any errors you see that might be causing this Bad Request error?


    Lower Bound
    Upper Bound

    Tier 1

  8. How are you treating the rich text area fields in line columns to not have the first row blank?
    The formatting generates a blank row every time before the text starts which ruins the layout.

  9. Generate Quote (pdf) fails with Bad Request, when the values(e.g. group name) has special characters such as & or or double quote.

    Using CPQ standard Visual force page

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.