Find your content:

Search form

You are here

Mail merge templating functionalty rebuild for custom object


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?

Attribution to: Daniël Zwijnenburg

Possible Suggestion/Solution #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.

Attribution to: jkraybill
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content