+ Start a Discussion
aj2taylo2aj2taylo2 

Return Page Error From Public Void

I have a VF Extension class with subclass, that has a void method.

 

public with sharing class myWizard {

    public myWizard(ApexPages.StandardController stdController) {
        ...
    }

        public List<listMap> getHierarchy() { 
            ... 
            return returnList;
        }


        // ------------------------------------------------------
        // Custom class for the Gantt Chart Projects
        // ------------------------------------------------------
            public class listMap {
                
                public listMap(...) {              
                   instantiateValues();
                }
                
                private void instantiateValues() {
                   ...
                }
                
                public void addChild() {
                    try {
                        ...
                    } catch (Exception e) {
                        if(e.getTypeName() != 'System.DmlException') {
                            this.myRecord.addError(e.getMessage());
                        }
                    }
                }
                
                
            }

            
}

 

When the void method (addChild) from my VF Page, I want to return an error for an Exception. Is there a way I can do this?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka
public with sharing class myWizard {

    public myWizard(ApexPages.StandardController stdController) {
        ...
    }

        public List<listMap> getHierarchy() { 
            ... 
            return returnList;
        }


        // ------------------------------------------------------
        // Custom class for the Gantt Chart Projects
        // ------------------------------------------------------
            public class listMap {
                
                public listMap(...) {              
                   instantiateValues();
                }
                
                private void instantiateValues() {
                   ...
                }
                
                public void addChild() {
                    try {
                        ...
                    } catch (Exception e) {
                        if(e.getTypeName() != 'System.DmlException') {
                           // this.myRecord.addError(e.getMessage());
				Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,''+e.getMessage()));  
                        }
                    }
                }
                
                
            }

            
}

 Try highlighted changes. Your visualforce page must have a <apex:pageMessages/> tag.

 

Hit the Kudos button if any post helps you - Mark the answer as solution, It might help others running to into similar problem in future.

All Answers

Chamil MadusankaChamil Madusanka
public with sharing class myWizard {

    public myWizard(ApexPages.StandardController stdController) {
        ...
    }

        public List<listMap> getHierarchy() { 
            ... 
            return returnList;
        }


        // ------------------------------------------------------
        // Custom class for the Gantt Chart Projects
        // ------------------------------------------------------
            public class listMap {
                
                public listMap(...) {              
                   instantiateValues();
                }
                
                private void instantiateValues() {
                   ...
                }
                
                public void addChild() {
                    try {
                        ...
                    } catch (Exception e) {
                        if(e.getTypeName() != 'System.DmlException') {
                           // this.myRecord.addError(e.getMessage());
				Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,''+e.getMessage()));  
                        }
                    }
                }
                
                
            }

            
}

 Try highlighted changes. Your visualforce page must have a <apex:pageMessages/> tag.

 

Hit the Kudos button if any post helps you - Mark the answer as solution, It might help others running to into similar problem in future.

This was selected as the best answer
aj2taylo2aj2taylo2

Thanks chamil; unfortunately, that doesn't return the error to the page.

aj2taylo2aj2taylo2

Actually, I got it working by removing the condition around DMLException.  Thank you!