1

The below code builds an error message displaying the values from multiple records (typically 12).

Map<String,Budget__c> budgetByIdentifier = new Map<String,Budget__c>([SELECT Identifier__c, Id
                                                                        FROM Budget__c
                                                                       WHERE Identifier__c IN :budgetIdentifiers]);

            for(Monthly_Revenue_Forecast__c mrf : newMRFs) {
                mrfOppId = mrf.Opportunity__c;
                mrfTeamName = teamByOppId.get(mrfOppId);
                mrfProductName = productByOppId.get(mrfOppId);
                try {
                    budgetId = budgetByIdentifier.get(mrfTeamName + '-' + mrfProductName + '-' + mrf.Revenue_Date__c.month() + '-' + mrf.Revenue_Date__c.year() ).Id;
                    mrf.Budget__c = budgetId;
                } catch(NullPointerException e) {
                    existingMessage = errorMessagebyOppId.get(mrfOppId);
                    if (existingMessage != null) {
                        errorMessagebyOppId.put(mrfOppId, existingMessage +  ' Team Name ' + mrfTeamName + ', Product Name ' + mrfProductName + ' & Revenue Date ' + mrf.Revenue_Date__c.format());
                    }
                    else {
                        errorMessagebyOppId.put(mrfOppId, ' Team Name ' + mrfTeamName + ', Product Name ' + mrfProductName + ' & Revenue Date ' + mrf.Revenue_Date__c.format());
                    }
                }
            }

            for(Monthly_Revenue_Forecast__c mrf : newMRFs) {
                errorMessage = errorMessagebyOppId.get(mrf.Opportunity__c);
                if (errorMessage != null) {
                    oppsByOppId.get(mrf.Opportunity__c).addError(' No Budget record was found for MRF record(s) with ' + errorMessage + '\n' + ' [WE_MRFv3]');
                }
            }

At the moment, the line break \n in my addError message doesn't 'work', this is the error message that's displayed -

error message

this message is not being displayed in a VisualForce page

obviously it would be much easier to make sense of the message if each record's details were displayed on a new line. Can line breaks be included in error messages?

  • 2
    try this <br/> -- developer.salesforce.com/forums/?id=906F000000090A2IAI – kurunve Mar 11 '16 at 14:14
  • @kurunve I get the unexpected token error if I use + errorMessage + <br/> + ' [WE_MRFv3]' This isn't being used in a VisualForce page, if that makes a difference. – Alex S Mar 11 '16 at 14:17
  • + errorMessage + '<br/>' + ... – kurunve Mar 11 '16 at 14:19
  • <br /> is an HTML tag, meaning it wouldn't need to be concatinated like you've done in your comment. It'd look more like + errorMessage + ' any text goes here.<br/>This is where the new line should start.' – Dan Jones Mar 11 '16 at 14:20
  • @kurunve @Poet if I use & Revenue Date ' + mrf.Revenue_Date__c.format() + '<br/>'); there's still no line break and <br/> is displayed in the message's text. – Alex S Mar 11 '16 at 14:29
7

Salesforce by default escapes any html in the addError String. To work around this, use the addError method with additional 'escape' parameter at the end:

oppsByOppId.get(mrf.Opportunity__c).addError(' No Budget record was found for MRF record(s) with ' + errorMessage + '<br/>' + ' [WE_MRFv3]',false);

The above should at least put [WE_MRFv3] on a new line. If you want all the errors on separate lines, put <br/> in with each iteration of adding text to the message.

Documentation (including a security warning!) can be found here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_addError_2

  • In order to display each record's error on a separate line I'll need to change the location of the break but when using errorMessagebyOppId.put(mrfOppId, existingMessage + ' Team Name ' + mrfTeamName + ', Product Name ' + mrfProductName + ' & Revenue Date ' + mrf.Revenue_Date__c.format() + '<br/>'); the html markup is not rendered, it's displayed in the message's text. Can I escape the html markup in the string? In the documentation it looks like this shouldn't be necessary but.. – Alex S Mar 11 '16 at 15:35
  • 1
    No it shouldn't, indeed. Are you sure you're still escaping html in the addError? Is the last line ([WE...) still on a new line? – Guy Mar 11 '16 at 15:38
  • That was it, I had removed the html from the addError section of the code so I also removed the escape, adding it back in got everything working. Thanks! – Alex S Mar 11 '16 at 15:41
  • Great, glad to be of help :-) – Guy Mar 11 '16 at 15:43

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.