function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amit Singh.ax1042Amit Singh.ax1042 

How to display Error message in Catch?

Hi friends,

I ma creatin role with the help of vf and apex class...

and displaying roles in Visualforce page with the help of Apex class....

i want to give edit and delete functionalty on vf page... i have given the same...Edit is working properly...

Delete is also working fine... but while i want to delete a role for which any user is assigned it is Showing standard error..(i want to make it custom eg- you can not delete this role)...

 

can i do this???

 

this is my code..

 

public class RoleList

{

        public List<UserRole> list_role{get;set;}   

        public String message {get;set;} 

      public RoleList(ApexPages.StandardController controller)    

        {

         list_role=[Select Name From UserRole Order By Name];       

        }

   public PageReference del()

        {    String delname = ApexPages.CurrentPage().getParameters().get('id'); 

       try       

          {

          Database.delete(delname);  

          }

        catch(System.DMLException e)

        { 

       //         message = 'Whoops! An error occurred -- ';

      //         System.assert(false,message);

          System.assert(false, e.getMessage());        

         } 

  PageReference p=new PageReference('/apex/AdminListView?intFocus=6'); 

  p.setRedirect(true);

    return p;

    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Just modifying some part of Chamil's post, so as to catch the error dynamically.

 

catch(System.DMLException e)
{       
	ApexPages.addMessage(myMsg);
	ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,string.valueof(e)); 
}

 

All Answers

Chamil MadusankaChamil Madusanka

Hi,

 

Add this code segment in catch block

 

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Your Error');
ApexPages.addMessage(myMsg);

 And place following code in visualforce (add this in place which you need to show error)

 

<apex:pageMessages ></apex:pageMessages>    

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

Rahul SharmaRahul Sharma

Just modifying some part of Chamil's post, so as to catch the error dynamically.

 

catch(System.DMLException e)
{       
	ApexPages.addMessage(myMsg);
	ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,string.valueof(e)); 
}

 

This was selected as the best answer
Amit Singh.ax1042Amit Singh.ax1042

Now I am not getting any error message....

and if user is associated with role to which i want to delete... after clicking on delete button it lands to same page(not showing error which i have written in catch block).

 

why?

Amit Singh.ax1042Amit Singh.ax1042

Sorry friends...

its working now...

i missed to give return value....

 

i am getting error on my page.... is it possible to give message in dialog box??  and after pressing any button on dialog box i should land to the same page...

any suggestion?

Rahul SharmaRahul Sharma

Could you elaborate more on:


is it possible to give message in dialog box??  and after pressing any button on dialog box i should land to the same page...




Amit Singh.ax1042Amit Singh.ax1042

see...

i am using an vf page and apex class to view all the roles...

in this vf page i have given option of edit and delete...

right now if i am deleting any role (suppose this role is assigned to any user)...then after pressing delete button it shows a message on the same vf page (on which i am displaying list of roles)...

 

i want that, if i want to delete such kind of roles then first i should get a pop up Sure?Do u want to delete? - this msg i am getting.....after getting this message if any exception is caught..then is it possible to show message in dialog box(which i am getting in page)?? 

Rahul SharmaRahul Sharma

On button click call javascript function to show confirm box and in the confirm box show the error/exception asking the reply from user.

Amit Singh.ax1042Amit Singh.ax1042

could you please give me any example with code??