Skip to main content
Sruthi M (Dev) preguntó en #Apex
Hi,

 I have a visual force page with two sections each section(with input fields) showing diff object fields,from this page,i am saving/creating records of two objects at a time,when i click on save a mail should be sent with the data on the page,or the visual force page with the data in email body.

Can somebody help how to achieve this...

Thanks in Advance...
1 respuesta
  1. 30 dic 2015, 10:39
    You could write an email template that you could then send off from the "save" method in your visualforce page controller?

    You must have a custom controller to handle the saving of the two objects? So in that method use some of the SimpleEmailMessage stuff to fire out an e-mail:

     

    EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE name = 'MyTemplate'];

    List<Messaging.SingleEmailMessage> msgs = new List<Messaging.SingleEmailMessage>();

    Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();

    msg.setTemplateID(template.Id);

    msg.setToAddress(/* work out who you want to get it */);

    msg.setWhatId(custom_object.Id);

    msgs.add(msg);

    Messaging.sendEmail(msgs,false);

    of course, thinking about it - I hope you have a relationship between your two objects so you can reference their two fields on the same template...

    Regarding doing that, once you have setup a "Visualforce" email template - you should be able to copy and paste your page into it, jiggle the inputs into outputs and resuse that code. That is suboptimal I admit, because you'd have to maintain both files, but I don't think you can just reuse the input form as an output for e-mail.

    You might be able to use "getContentAs" to attach the original Visualforce page to an e-mail as a PDF? Might that work? Turning the inputs straight into outputs (or leaving them visually as inputs, but in a PDF it would just look like a form).

    Is any of that any use?
0/9000