1

I have two different approaches for adding an error to my visualforce page. The first approach I am concatenating a string at the end of exception. The second approach I am simply returning the message from the Exception itself.

However I'm not sure why the first one would display a duplicate and redudant error message. I want to use the first approach (with the record name at the end)... but I don't want the duplicate message in the beginning.

public PageReference saveChanges(){

    problematicIds = new Set<Id>();

    try{
        update InterBUoppz;
    }

    catch(DmlException de) {
    Integer numErrors = de.getNumDml();
    for(Integer i=0;i<numErrors;i++) {
        //Image 1: this gives a duplicate message on top (only for the first error)
        //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, de.getDmlMessage(i)+' - Opportunity: '+oppListMap.get(de.getDmlId(i)).Name));

        //Image 2: this works fine but it is not as precise
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, de.getDmlMessage(i)));
    }
    errorsFound = true;
    }

    return null;

}

Image 1 Image 1 Image 2 Image 2

4
  • Are you calling addMessage twice when you observe the behavior in the first image?
    – Adrian Larson
    Aug 1, 2017 at 20:05
  • That's a great thought, but no I am not. Just double checked.
    – rmarq423
    Aug 1, 2017 at 20:06
  • Here is a related bug that I found: salesforce.stackexchange.com/questions/40146/…
    – rmarq423
    Aug 1, 2017 at 20:10
  • @rmarq423 That's not a related bug. You're not using a label attribute.
    – sfdcfox
    Aug 1, 2017 at 20:50

2 Answers 2

9

The first message is provided to you "for free" when you try-catch a DMLException. Further, the reason why you only see one error in the second example is because duplicate messages are collapsed into a single instance. Therefore, even though you added it manually, there was no need to do so, because it was already added for you. If you don't want the first message to appear, you'll want to use the Database methods and not cause an exception.


problematicIds = new Set<Id>();
Database.SaveResult[] results = Database.update(InterBUoppz, false);
Integer numErrors = 0;
for(Database.SaveResult result: results) {
    if(!result.isSuccess()) {
        numErrors++;
        // Display your custom error message with ApexPages.addMessage...
    }
}
3
  • Thumbs up - I have observed this for years and never realized it could be worked around with Database.xxx methods - I had built elaborate isAlreadyInContext(...) utility methods to avoid the duplicate messages. Thanks!
    – cropredy
    Aug 2, 2017 at 0:47
  • Interesting... I never knew this. I'll have to figure out a way to somehow get the failing record's Id - as I will need that for my page. For some reason, result.getId() is always NULL.
    – rmarq423
    Aug 2, 2017 at 12:53
  • @rmarq423 Yeah, getId returns null when there's an error. If you need to know which record failed, use for(Integer index = 0, size = InterBUoppz.size(); index < size; index++) { if(!results[index].isSuccess()) { /* InterBUOppz[index] failed to update */ } } The results are always in the same order as the original list.
    – sfdcfox
    Aug 2, 2017 at 15:03
1

Couldn't you just add create a string and add your message to it? I may not understand the question well enough though

Integer numErrors = de.getNumDml();
Boolean errorsFound = false;
String errorMessage = '';

for(Integer i=0; i < numErrors; i++) 
{
    errorMessage +=   de.getDmlMessage(i) + '<br />  ';
    errorsFound = true;
}

if(errorsFound)
{
   ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, errorMessage));
}

You must log in to answer this question.

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