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
Steve ThurstonSteve Thurston 

Convert Long Text Area to String

I am trying to convert the contents of a Long Text Area into a String to include in an HTML Email.  The line of code I am using is:

emailHTML = emailHTML.replace('%%Opportunity.Dispatch_Notes__c%%', Opportunity.Dispatch_Notes__c);

emailHTML is the full HTML content of the email.  '%%Opportunity.Dispatch_Notes__c%%' is the tag in the HTML that I am trying to replace with the Opportunity's 'Dispatch_Notes__c' field.  That field's data type is Long Text Area.

When I try to save the Apex class in IDE, I get the error message on that line:

"Save error: Method does not exist or incorrect signature: [String].replace(String, Schema.SObjectField)"

I have any number of calls to .replace() on that 'emailHTML' string, so the method presumably isn't the problem, which means the signature is.  Apparently, it doesn't like replacing contents in a String ('emailHTML') with the contents of a Long Text Area field.

How do you include Long Text Area field content in HTML?  Is there some way to cast the Long Text Area type into a String type?
Best Answer chosen by Steve Thurston
PawanKumarPawanKumar
I think the probelm is : your are not querying the opportunity and referencing Opportunity class directly . Please try below where we are using opportunity instance variable instead class name directly.

Opportunity oppty = [Select Id,Dispatch_Notes__c from Opportunity where Id='YourOptyId' Limit 1];
emailHTML = emailHTML.replace('%%Opportunity.Dispatch_Notes__c%%', oppty.Dispatch_Notes__c);

Regards,
Pawan Kumar
 

All Answers

PawanKumarPawanKumar
Hi Steve,
Please try below.

emailHTML = emailHTML.replace('%%Opportunity.Dispatch_Notes__c%%', (String)Opportunity.Dispatch_Notes__c);

Regards,
Pawan Kumar
 
Steve ThurstonSteve Thurston
Hi Pawan,

Now I get the error:

"Save error: Incompatible types since an instance of Schema.SObjectField is never an instance of String"
PawanKumarPawanKumar
Hi Steve,
Please share your full code.

Regards,
Pawan Kumar
PawanKumarPawanKumar
I think the probelm is : your are not querying the opportunity and referencing Opportunity class directly . Please try below where we are using opportunity instance variable instead class name directly.

Opportunity oppty = [Select Id,Dispatch_Notes__c from Opportunity where Id='YourOptyId' Limit 1];
emailHTML = emailHTML.replace('%%Opportunity.Dispatch_Notes__c%%', oppty.Dispatch_Notes__c);

Regards,
Pawan Kumar
 
This was selected as the best answer
Steve ThurstonSteve Thurston
D'Oh!  That was exactly it!  I was making the query, but the variable I put it in is called "theOpportunity", and I didn't see that my replace statement had "Opportunity" instead.  Argh!  Proof positive that it always helps to have a second pair of eyes!

It works fine now.  Thanks for your help!