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:
- Trigger on Correspondance before insert
- Regex all the {!} vars.
- Check for Contact, Lead, Account or Opportunity in the {! Object.field } var.
- Check if Contact, Lead, Account or Opportunity are set via the lookup.
- Query these objects with the fields supplied in the var {! Object.field}
- Use the found values to build a new correspondance text.
- 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?