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.

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="https://mydomain--c.naXX.content.force.com/servlet/servlet.ImageServer?id=0151K0000040ptM&oid=00D41000000FWOw" 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('<[^>]+>',' ');

Thanks to Ashley for her input on this article!

DP

4 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.

Leave a Reply

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