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.