0

I am trying to automatically attach a document to the Contract Object when I create the object, but with fields auto populated (think DocuSign).

I've tried a few things in Apex, but keep running into issues.

1) The first solution I tried was to upload the document as a word document into Salesforce Documents. I was hoping to convert the text into a string and search for keywords in the document to replace with populated Contract object fields.

Ex) In the word doc there would be a line that says: "This contract is for name__c". I would want to replace name__c with the same field on the Contract object.

The issue with this solution was that when I try to convert the document from a blob to a string, Salesforce will not allow me by saying "BLOB is not a valid UTF-8 string".

2) The second solution I tried was to save the word doc as a rich text format file (rtf). This solution seemed promising at first. I was able to convert the text into a string and replace the desired text in the string. And was even able to attach the document with most of the formatting.

The issue with this solution was that it kept "most of the formatting" not all of the formatting. The logo on the document was no longer there, and much of the syntax was gone as well.

My Question: Is there a way to achieve what I am trying to do without relying on third party software? Is there some feature in Salesforce that is made for exactly this situation, or maybe some apex code I haven't tried? Maybe a way to keep the file as a word doc and still edit text?

Any help is appreciated! Thank you.

0

It's possible to write the file as a Microsoft Word document, in theory, but non-trivial. You could use apex-zip and Dom.Document to follow the Open Office Format for rendering. All of the facilities are there to make such a thing possible, but I wish you the best of luck if you want to make it 100% native without going to a third-party system. Common problems include Heap Size limits and CPU limits; your best bet is to kick off an asynchronous process for the actual conversion process.

If you want to make something that's still easily editable, definitely consider using RTF. It's an older specification, but is text-based and a lot easier to work with. You'll want to download the specification to figure out why things went missing, since it is a particularly "picky" format to work with. Even better, pretty much every OS has a RTF reader built-in or available for practically nothing; WordPad for Windows, etc.

Personally, if I were going to render something, I'd use PDF format. Later versions of Word, as well as various free software like OpenOffice, can easily edit PDF files. To do this, you just need to render a normal Visualforce page, and call PageReference.getContentAsPDF. Even better, since it is a Visualforce page, you can easily modify the markup, use expressions to render dynamic text, etc. You could even probably get away with writing a custom Apex REST service to obtain the data, skipping the step of writing Visualforce.

Finally, I might also consider doing the same thing in an actual Visualforce page or Lightning Component. The idea is that the Word document could be rendered directly on the client computer, then upload the results to Salesforce. This presumes you don't want to do this in a trigger. It does have the advantage of faster execution time and no governor limits, so it might be worth exploring if your users are primarily in the UI anyways.

I can think of a few other, more esoteric solutions, but hopefully you'll see that there's at least a glimmer of hope, even if it's not as simple as you originally thought it would be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.