0

I've a VF page to create cases. It has lot of workflows and VRs associated with it. While saving a record, if there's a VR error, the message shows at the top of page, however, I'm also able to see Save success message beneath the VR message. How do I restrict save success message to be displayed only when the record is actually saved? Please refer to the below screenshot. screenshot

Page Controller:

public class QRreportController

/* --------------------- variable for the standard controller ------------------------------- */ 
{
    private ApexPages.StandardController std;        
/* --------------------- variable for this case ------------------------------- */     
    public Case c {get; set;}

/*  -------------------- standard work order controller  ----------------------------  */            
    public QRreportController (ApexPages.StandardController stdCtrl)
    {
        // prepopulates some fields on page load
        this.c = (Case)stdCtrl.getRecord();
        Map<ID,Schema.RecordTypeInfo> rt_Map = Case.sObjectType.getDescribe().getRecordTypeInfosById();
        Id RecordTypeIdCase = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Quality Report Case').getRecordTypeId();
        Id rtId = RecordTypeIdCase;
        c.RecordTypeId=rtId;
        c.AccountId=System.currentPageReference().getParameters().get('AccountId');
        std=stdCtrl;
    }

/* --------------------- Get Case ------------------------------- */     
    public Case getCase()
    {       
     return (Case) std.getRecord(); 
    }

  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }

/* --------------------- Save and Exit method ------------------------------- */
    public PageReference saveAndExit()
    {
        std.save();
        PageReference pageRef1 = new PageReference('/' + getCase().id);
        return pageRef1;

    }
/* --------------------- Edit method ------------------------------- */
    public PageReference edit1()
    {
        PageReference pageRef2 = Page.QRcaseEdit;
        pageRef2.getParameters().put('id', getCase().id);
        pageRef2.getParameters().put('AccountId', getCase().AccountId);
        return pageRef2;
    }


/* --------------------- Quick Save method ------------------------------- */
public PageReference save()
    {
        std.save();
        PageReference pageRef3 = Page.QRcase;
       ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'SUCCESS!  Changes saved.'));

        pageRef3.getParameters().put('id', getCase().id);
        pageRef3.getParameters().put('AccountId',getCase().AccountId);
        return PageRef3;

    }


     public PageReference upload() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = getCase().Id ; // 
    attachment.IsPrivate = true;

    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment(); 
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
  }

}[![enter image description here][1]][1]

1 Answer 1

1

Check the return value from StandardController's save method. When there are errors, a null result is returned.

PageReference saveResult = std.save();
if(saveResult == null) {
    return saveResult; // Errors will be displayed automatically
}
2
  • Hello Sfdcfox, thank you for the reply. I'm getting an error 'Expecting '}' but was: 'if'' when I try to update the controller. Where exactly should I update the change in the controller?
    – TonySF
    Commented Jan 31, 2019 at 16:14
  • @TonySF You have a typo somewhere then. Any time you call std.save in your code, use the above instead.
    – sfdcfox
    Commented Jan 31, 2019 at 16:34

You must log in to answer this question.

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