7

I'm trying to add a single quote to the following code

if(txo.Target__c != null){
                       targetsToCreate.add(txo);
                   }else{
                       o.addError(' Please ask your Salesforce Administrator to add a target record, for the month & year of your Opportunity's close date');
                   }

so that my string is displayed as

Please ask your Salesforce Administrator to add a target record, for the month & year of your Opportunity's close date.

The above causes the error

line breaks not allowed in string literals

Having found various 'solutions', I've tried Opportunity''s, Opportunity\\\\'s, Opportunity\'\'s and

" Please ask your Salesforce Administrator to add a target record, for the month & year of your Opportunity's close date"

but none of the above work.

How can I include the single quotation mark in my string?

8
  • possible duplicate of How to Escape Single Quotation Mark in String.format Commented Jul 31, 2015 at 10:32
  • @VamsiKrishna as I mentioned in my question, I've tried the solution from that post?
    – Alex S
    Commented Jul 31, 2015 at 10:34
  • Where the output is being displayed? Standard page or Custom page?
    – Ashwani
    Commented Jul 31, 2015 at 10:42
  • @Ashwani it's a standard page.
    – Alex S
    Commented Jul 31, 2015 at 10:43
  • Well it is working for me. I can see the ' in Opportunity.
    – Ashwani
    Commented Jul 31, 2015 at 10:44

2 Answers 2

9

This way it works:

trigger SingleQuote on Account (after insert) {
   Trigger.New[0].addError(' Please ask your Salesforce Administrator to add a target record, for the month & year of your Opportunity\'s close date');
}

enter image description here

AND

enter image description here

AND

enter image description here

8
  • Thanks Ashwani but as you can see, that's exactly what I've tried in the original version of the code and it's not working for me..
    – Alex S
    Commented Jul 31, 2015 at 10:55
  • @AlexS can you repost the screenshot covering the more area of page? It seems a difference.
    – Ashwani
    Commented Jul 31, 2015 at 10:59
  • I've updated the screenshot, just let me know if you need to see more of the page.
    – Alex S
    Commented Jul 31, 2015 at 11:03
  • I've tried the HTML alternative you've suggested but again, that's not had an effect.
    – Alex S
    Commented Jul 31, 2015 at 11:04
  • @AlexS updated answer with one more trick
    – Ashwani
    Commented Jul 31, 2015 at 11:15
2

You can try using html-entity for the single quotation mark:

if(txo.Target__c != null){
    targetsToCreate.add(txo);
}else{
    o.addError(' Please ask your Salesforce Administrator to add a target record, for the month & year of your Opportunity's close date', FALSE);
}


Edit #1 Ofcourse, don't forget to set pageMessages component's escape attribute to false (as I forgot to mention it):

<apex:pageMessages escape="false" />


Edit 2 Sorry, being in a rush I didn't notice that it's not a VF page you're having problems with, but rather a standard page. In any case, just use the false as the second argument in the addError(sObj, boolean) call, as the second parameter "Indicates whether any HTML markup in the custom error message should be escaped (true) or not (false)."

5
  • It will be printed exactly .....Opportunity&apos;s close date
    – Ashwani
    Commented Jul 31, 2015 at 10:58
  • &apos; doesn't cause any errors and wasn't printed exactly in the output but again, neither is the single quotation. Could there be something that's reformatting the output? cc @Ashwani
    – Alex S
    Commented Jul 31, 2015 at 10:58
  • @Ashwani, it will be printed exactly, unless you set escape attribute to false. Sorry I forgot to mention it.
    – dino ursic
    Commented Jul 31, 2015 at 11:16
  • 1
    Message is on standard page. <apex> tag can be used on custom vf page only.
    – Ashwani
    Commented Jul 31, 2015 at 11:17
  • 1
    Well in that case just use false as the optional second argument in the addError() call.
    – dino ursic
    Commented Jul 31, 2015 at 11:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .