+ Start a Discussion
sales4cesales4ce 

How to catch Privilege based exceptions?

Hi,

 

I am trying to know if we can catch privilege based exceptions.

 

For example: Assume that , I have a Button ('Testing')on Lead Detail page that calls my Apex Class. The Apex class enforces sharing.The class has Update Operation .

The user has "Read only" Permission to the Lead Record. The user when clicks the Button('Testing'), as described above, would definitely raise an exeption saying insufficient privileges.

 

How can i catch these kind of exceptions and display a message that says "Contact your system Admin".

Any sample code/ ideas is highly appreciated.

 

Thanks,

Sales4ce

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Hey

 

It throws a DMLException is that correction? If so you'd catch that excpetion in your action method and then add a Page Message to the page, and return a null page reference from that method.

 

You can get more on this here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_message.htm?SearchType=Stem&Highlight=pages|page|Page|PAGE|Pages||messages|message|Messages|Message|Messaging|MESSAGE|messaging

 

Wes

All Answers

WesNolte__cWesNolte__c

Hey

 

It throws a DMLException is that correction? If so you'd catch that excpetion in your action method and then add a Page Message to the page, and return a null page reference from that method.

 

You can get more on this here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_message.htm?SearchType=Stem&Highlight=pages|page|Page|PAGE|Pages||messages|message|Messages|Message|Messaging|MESSAGE|messaging

 

Wes

This was selected as the best answer
sales4cesales4ce

Thanks Wes, that certainly helped me.

But, any idea on how do we write a test method that covers DMLException?

 

Thanks again,

Sales4ce

WesNolte__cWesNolte__c

Yep, to test your scenario create a user with the profile type and use System.runas(myUser) to execute a section of code with that users permission levels.

 

Wes

sales4cesales4ce

Thanks again for your help on this.

 

I tried doing the same. but in this process i have had couple of questions.

 

1) I would query for the user and would "runas" that user in my test method and then deploy to the production org. what happens when the user is deactivated, does my apex class become "Invalid"?

 

2) Also, i wrote my Test methods in my Apex class. So whenever my apex class is called, does the test methods are also validated each time my class is called. If Yes, then my assumption in step 1 would be right.   Is this the case?

 

Thanks,

sales4ce

Pradeep_NavatarPradeep_Navatar

You can handle the Privilege based exception after inserting the records :

 

            Database.saveresult sr = Database.insert(u4,dlo);

            if(sr.isSuccess() == true)

            {

                                                   // your login here

            }

            else{// you can handle exception here.}                  

 

Hope this helps.

WesNolte__cWesNolte__c

Hey 

 

I would suggest creating a utility method (for test classes) that you use to create Users with appropriate profiles and roles. This helps avoid the issue you've described as well as gives you full control over the test scenario.

 

An invalid class can cause issues in a test class, but not the other way around so the answer to your second question is no.

 

Hope this helps.

 

Wes