0

I have template which is based on the merge fields related to order line items. I want to use this template in apex to send a mail,but using TagetObjectId: which expects the value to be user/contact/lead it : am unable to establish a context for template.

On testing, if i hard code the targetObject Id with some user Id,am able to get the emails-but the template referred merge fields are not populated.

How can i achieve to populate these merge fields properly.?

  1. create a new Messaging.singleEmailMessage()
  2. setTargetObjectId(a valid contactid)
  3. setWhatId(Id of order line item)
  4. setTemplateId(id of my email template based on orderline item)
  5. setSaveAsActivity(false)
  6. Messaging.sendEmail(emails, false)
  • so the email template displays only a single order line item? I would expect the WhatId to be the orderId and then email template displays all of the order's line items – cropredy Jul 31 '15 at 20:58
0

Reads like you've answered your own question... you need to provide the Id of the recipient of the invoice. So, you must be sending the invoice to someone via apex. Get that contact record id and pass it into the Messaging.singleEmailMessage entity ie. singleEmailMessage.setTargetObjectId(contactId);

Or am I missing something?

  • In my case the approach mentioned doesnt work as, the template i am using is based on all the fields from order merge fields and doesnt hold any merge fields referring the contact Id's passed via setTargetObjectId (i tried passing the id of Order owner,but dint help). Due to this the template never gets populated for order merge fields... And am not sure how can i make this work by using this template and targetObjectId.? – user10727 Jan 19 '15 at 9:57
  • So who are you sending the email to ? Isnt that why you are using a single email message ? Perhaps I dont understand the broader use case here as I am assuming you want to merge to template and send...? – CloudHugger Jan 19 '15 at 10:17
  • ok, the usecase says whenever there is any change in the order line item ,we need to mail a separate team to inform about this change. For this we have created a template- which would not hold any info of that contact whose sending. But salesforce expected targetObjId to have the contactId that can help to populate the merge fields..and mine is an exception to this..Does it mean for my case i cannot use this feature...and have to go with htmlbody type? – user10727 Jan 19 '15 at 11:40
  • Ok. Whilst the template may not be merging in any fields, the solution would likely still benefit from being provided the affected contact context via targetObjectId. For example if you set the setSaveAsActivity to true it would link the activity to contact and oppty for you. – CloudHugger Jan 19 '15 at 19:05
  • Further to previous note, assuming email approach is most appropriate - have you considered chatter or use of queues and workflows? - an option to consider might be establish a group email address, store as contact then use that contact as the target you need. Then if you store activity you'll have a log of the alerts sent to the team. – CloudHugger Jan 19 '15 at 20:38

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.