function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
buggs sfdcbuggs sfdc 

Please fit the below message in HTMLbody

HI Everyone

I wanted to display my message in following format can amyone help me out

this is what am trying to do, i dont want to use email templates,please helpme out to fix it.here am attaching(in sample code) how my message wants to loooks like
Subject : Session is NOT Created

Attention:

A session is created for the following Account and opportunity

Account: {!Account.Name}
Contact: {!opportunity.Name}

Thank you

Am trying the above shown messsage in the following format it doesnt works as the above message:--

message.setSubject('Session is NOT Created!');
              String body = 'Attention:';
              body += 'A session is created for the following Account and opportunity';
              body += 'Account: {!Account.Name}';
              body += 'Contact: {!opportunity.Name}';
            message.setHtmlBody(body);

Thanks in Advance
 
Best Answer chosen by buggs sfdc
Nachu RY 4Nachu RY 4
fint the below code.

 ToAddresses = new List<string>();
        toaddresses.add('nachu.srm@gmail.com');
        subject = 'Session is NOT Created'
        Body = '<html><head>Attention :  </head><body><br/> A session is created for the following Account and opportunity <br/> Account:'+{!Account.Name} +'<br/> Contact:'+ {!opportunity.Name} + '<br/>Thank You </body></html>';
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        email.setSubject( subject );
        email.setToAddresses(ToAddresses);
        email.setHTMLBody( body );
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

Thanks,
Nachiapphan RY

All Answers

Nachu RY 4Nachu RY 4
fint the below code.

 ToAddresses = new List<string>();
        toaddresses.add('nachu.srm@gmail.com');
        subject = 'Session is NOT Created'
        Body = '<html><head>Attention :  </head><body><br/> A session is created for the following Account and opportunity <br/> Account:'+{!Account.Name} +'<br/> Contact:'+ {!opportunity.Name} + '<br/>Thank You </body></html>';
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        email.setSubject( subject );
        email.setToAddresses(ToAddresses);
        email.setHTMLBody( body );
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

Thanks,
Nachiapphan RY
This was selected as the best answer
buggs sfdcbuggs sfdc
HI Nachiapphan RY

Thanks for the quick reply,am getting the following error,can you please have a look in to it

Error: Compile Error: unexpected token: '{' (this error is when am copy pasting as the above the code)

Error: Compile Error: line breaks not allowed in string literals at line(this is when am spliting the code) as below,please check it once,thanks again
subject = 'Session is NOT Created!'
        Body = '<html>
           <head>Attention : </head>
            <body>
            <br/> 
            A session is created for the following Account and opportunity 
            <br/> 
            Account:'+{!Account.Name} +'
            <br/> 
            opportunity:'+ {!opportunity.Name} + '
            <br/>
            Thank You 
            </body>
            </html>';

 
Nachu RY 4Nachu RY 4
You should not break the line in BODY . all HTML tag from top to bottom should be in the single line. We should not press enter to type in the next line. copy paste the below code.hope it will work

subject = 'Session is NOT Created!'
        Body = '<html> <head>Attention : </head><body><br/>  A session is created for the following Account and opportunity  <br/>  Account:'+{!Account.Name} +'<br/>   opportunity:'+ {!opportunity.Name} + '  <br/> Thank You  </body>  </html>';

 
buggs sfdcbuggs sfdc
HI Nachu

Am seeing the following error on the below line.Please have a look

Error: Compile Error: unexpected token: '{'
Body = '<html> <head>Attention : </head><body><br/>  A session is created for the following Account and opportunity  <br/>  Account:'+{!Account.Name} +'<br/>   opportunity:'+ {!opportunity.Name} + '  <br/> Thank You  </body>  </html>';

Thanks again
buggs sfdcbuggs sfdc
The same logic is working without {} braces,thanks Nachu
ToAddresses = new List<string>();
        toaddresses.add('nachu.srm@gmail.com');
        subject = 'Session is NOT Created'
        Body = '<html><head>Attention :  </head><body><br/> A session is created for the following Account and opportunity <br/> Account:'+Account.Name +'<br/> Contact:'+ opportunity.Name + '<br/>Thank You </body></html>';
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        email.setSubject( subject );
        email.setToAddresses(ToAddresses);
        email.setHTMLBody( body );
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});