1

I'm on day 3 of my SalesForce development journey, what a cool platform, I'm having a blast.

I hope I'm saying this correctly: we'd like to apply a data object validation rule on a picklist.

Here's a screenshot of my rule. We need to prevent users from not picking a value from the Cancellation Reason - Cancellation_Reason__c picklist. This is the rule:

ISPICKVAL(Cancellation_Reason__c, "")

I'm pretty confident that my rule is correct, but this being day 3...

On update, I'm throwing the following exception, and it's happen when I'd expect a pretty validation failure message somewhere around the field. Of course, the value isn't saving correctly, but I'd like to for the save to abort and our validation message to be displayed somewhere on the screen.

12:48:55.231 (231827662)|USER_DEBUG|[48]|DEBUG|Update failed. First exception on row 0 with id 006c0000008hVyNAAU; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter a cancellation reason: [Cancellation_Reason__c]

This is the code that's creating the misbehavior and log messages

public PageReference updateEnrollment() {

    System.Debug('---------------- updateEnrollment() requested');

    try {
        update this.opportunity;
    } catch(Exception e)  {
        System.Debug('attempting to SDENrollmentsController.updateEnrollment(), but throwing an exception on update this.opportunity;');
        System.Debug(e.getMessage());
    }

    // update this.opportunity;

    PageReference retVal = null;
    retVal = Page.SDAccountOverview;
    retVal.getParameters().put('id', this.curAccount.Id);
    retVal.setRedirect(true);
    return retVal;
}

When I remove the try/catch, the screen doesn't error out, but potentially worse, it appears to the user that the save occurs (and the exception message gets moved in to the logs). Here's what I see on the screen when I pull the try/catch: screenshot of ugly error page.

It would be awfully nice to apply this validation rule w/o a swallowed exception or an ugly error page.

Thank you for looking.

3

You need to add <apex:pageMessages id="errors" /> on your visualforce page and then in your catch block, add the exception to the page message object and Salesforce will take care of the rest. ApexPages.addMessages(e);

public PageReference updateEnrollment() {

    System.Debug('---------------- updateEnrollment() requested');

    try {
        // try to perform the update
        update this.opportunity;

        // only redirect to another page if the update was successful
        // do this by placing the redirect inside the try, won't get here if validation fails
        PageReference retVal = null;
        retVal = Page.SDAccountOverview;
        retVal.getParameters().put('id', this.curAccount.Id);
        retVal.setRedirect(true);

        // send the user to the account overview page
        return retVal;

    } catch(Exception e)  {
        System.Debug('attempting to SDENrollmentsController.updateEnrollment(), but throwing an exception on update this.opportunity;');
        System.Debug(e.getMessage());

        // it is terrible practice to 'swallow' exceptions
        // which you are doing by just writing to the debug log
        // instead, add the exception details to the pageMessages collection to be
        // shown on screen
        ApexPages.addMessages(e);

        // do not redirect the user to another pageReference - keep them here
        return null;
    }

    return null;
}
  • AAU & Mark Pond: stacks of thanks gentlemen for the code and lesson. Take the rest of the day off! – fusion27 Aug 27 '15 at 19:02

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.