1

I believe cardRequest is the primary culprit in this issue. It is the record object.

Why does my save function not save and create a record of the page I am trying to save?

public PageReference Save() {
    System.SavePoint sp = Database.setSavepoint();
    PageReference ret;
    try {
        if (cardRequest.ID != NULL) {
            stdController.save();
            ret = new PageReference('/' + Business_Card_Request__c.getSObjectType().getDescribe().getKeyPrefix());
            ret.setRedirect(true);
        }
        else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Object is NULL*'));
            isSaveSuccessful = false;
        }
    }
    catch (System.DMLException ex) {
        Database.rollback(sp);
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,ex.getMessage()));
        isSaveSuccessful = false;
    }
    return ret;
}
4
  • check what are the error you are getting during save Commented Mar 20, 2017 at 17:47
  • @SantanuBoral unfortunately there is no error. Both the controller and vf page save and compile. Once the save button is pressed the page just seems to refresh and when I go to check if the record saved, it shows that it has not. Commented Mar 20, 2017 at 17:50
  • Have you already confirmed that your control flow is working as intended? If not, try dropping System.debug instructions inside your try/catch and if/else then check the log to see what actually ran.
    – Sequoyah
    Commented Mar 20, 2017 at 18:07
  • @Sequoyah I did and nothing is suspicious or shows up wrong in the log Commented Mar 20, 2017 at 18:56

1 Answer 1

1

If you are trying to insert a new record (as opposed to simply updating an existing one), then the cardRequest.ID != NULL condition will never be satisfied because the record Id is null until after it is inserted, so stdController.save() won't run.

Your error messages aren't showing up on the page because the message is lost when the page is refreshed. Returning null instead of ret when the save fails should prevent the page from refreshing. Your VF page will display your message if it includes an <apex:pageMessages /> component, though I believe it will need to be rerendered to show the message.

2
  • I removed that 'if' statement and the same result occurs. Commented Mar 20, 2017 at 18:59
  • @QuiteCurious If the page is still refreshing instead of redirecting to the PageReference specified in your try statement, that suggests that it's throwing an exception. You should be able to view the exception on the page by making the fix I described above, or you can just send it to the log with System.debug(ex.getMessage()).
    – Sequoyah
    Commented Mar 20, 2017 at 19:17

You must log in to answer this question.

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