1

I want to build more or less the same functionality as the mail merge. I was wondering if there are any standard methods or classes that can aid me. I've made a custom object named "correspondance" this object has a couple lookup fields to contact, account, lead and opportunity. I will use these lookup fields to fill in the {!} variables, this is much the same as the "Standard" mail merge functionality. So this:

Dear {! Contact.Salutation} {! Contact.Firstname},

Thank you for ....

Sincerely {! User.Name}

must be processed to:

Dear Mr. Foo,

Thank you for ....

Sincerely Daniel

Can I use mail merge classes and or methods to achieve this? I know the global steps to get this done:

  1. Trigger on Correspondance before insert
  2. Regex all the {!} vars.
  3. Check for Contact, Lead, Account or Opportunity in the {! Object.field } var.
  4. Check if Contact, Lead, Account or Opportunity are set via the lookup.
  5. Query these objects with the fields supplied in the var {! Object.field}
  6. Use the found values to build a new correspondance text.
  7. Set the generated text to the correspondance_text__c field.

I explicitly don't want to use mail merge as a functionality, and it's a nice coding exercise. Who can point me in the right direction?

1

We did something similar at Dashcord specifically for mail merge, with the goal of getting a more full-featured templating engine. We did indeed do pretty much what you describe - lots of regex. A few pointers you may find helpful:

  • as with any regex work, make sure you do LOTS of testing. If users are able to create their own templates, your regexes need to be very solid to tolerate all the various typos / whitespace combos / non-conforming merge markup that your code will see.
  • you may find it convenient for your needs to use the core EmailTemplate object as a backend data store for your merge templates. That has some advantages: you can use the (very basic) Salesforce template builder as an editor, you can reuse existing templates (if applicable), and you get a (basic) data model that may meet your needs (folders, subject, text + html version). Disadvantages are that you can't natively extend the data model, and also Salesforce does some somewhat undocumented "magic" on "{!" tags that you may not expect (replacing tags it considers to be invalid, for one).
  • if you're not familiar with the excellent apex-lang library, it provides an ideal query engine for the types of dynamic queries you'll need to make. For instance, it allows you to pass in a SFDC ID and a Set<String> of fields, and give you the object plus requested fields.
  • I don't think you're going to find that the core Apex API gives you a much more convenient way of doing merges than you describe. The core template merge isn't openly exposed. You might investigate VisualForce templates and/or dynamic VisualForce, but I'm pretty sure neither will get you where you need to go.
  • in the (somewhat unlikely, but possible) event that your templating engine needs to match on patterns that include line breaks, be aware that Apex doesn't expose Java's DOTALL flag. This can make matching long patterns that include line breaks and/or funky characters a bit of an exercise in regex hackery.

Hope that gives you some ideas.

  • Thank you very much for these very usefull tips. I would like to see how you used the apex-lang library as you suggested. Do you have a blog post anywhere or code on github? Thanks again! – Daniël Zwijnenburg Oct 2 '12 at 21:09
  • 1
    Sadly no, but Richard Van Hook and others have posted lots of examples around the web. The DatabaseUtils.query API is really robust and easy to use. It also gives convenient "Select *" and injection-attack-protected SOQL builder, among many other things. – jkraybill Oct 3 '12 at 4:31
  • schweet! I think this is a library I was searching for :) – Daniël Zwijnenburg Oct 3 '12 at 7:26
  • I finished a basic implementation of this, the DatabaseUtils.query saved alot of time! Thanks again! – Daniël Zwijnenburg Oct 3 '12 at 11:31
  • Yeah, it's pretty awesome. Glad it helped. I heard a rumor that apex-lang libraries are slated to get rolled into the core Apex API at some point which would be even better. – jkraybill Oct 4 '12 at 4:21

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.