Pages

Thursday 2 February 2012

system.security.NoAccessException : Update access denied on Pricebookentry

In my current assignment, I got a very strange issue which made me frustrated .

My task was to customize the "Add Product" functionality on Opportunity. Every thing was working fine until it was tested with the profile wherein no CREATE or EDIT permission was granted for PRICEBOOK sObject.
My own code rewarded me the exception

system.security.NoAccessException : Update access denied on Pricebookentry

at the final stage of adding products (on hitting the SAVE button).

I was puzzled because I used only one DML statement in my controller extension that was insert statement against OpportunityLineItem. I commented the insert statement and was still getting the same exception. If I provide CREATE or EDIT permission for PRICEBOOK then I was not getting the exception.

Bad luck, that I could not grant permision to some profiles as security was also a concern. I needed to play with my code and finally I resolved it after a hard work.

I used a Relationship query to get data from product2,pricebookentry,Pricebook2 objects. This gives me a list of pricebookentry.I used this  list of PriceBookEntry and bind it to PageBlockTable on my visualforce page. But when I hit save, I suspect the getter method of that list was generating the exception.

I never thought that Wrapper class  can save me in this situation.

Now I think I should share the resolution. Instead of binding the PageBlockTable with the list of pricebookentry, I created a wrapper class in following format :


 public class WrapPirceBook
    {
       Public Product2 prod {get;set;}
       public Decimal UnitPrice {get;set;}
       public ID Product2Id;
       public ID Pricebook2Id {get;set;}   
       public ID mainID {get;set;}   
        
       public WrapPirceBook(Product2 pro,Decimal u,ID p,ID c,ID ids) 
       {
            prod=pro;
            UnitPrice=u;
            Product2Id=p;
            Pricebook2Id=c;
            mainID =ids;
        }
    }
Now I am not using pricebookentry directly, instead I am doing all the stuff but without direct access of pricebookentry. After this implementation, when I hit save, I was on Cloud9. I was very happy to see that when I click on save, it saved all the selected products with the Opportunity.

I used wrapper class to resolve the issue but still I was using pricebookentry instead of Product2. I wanna say special thanks to my MANAGER who directed me to use Product2 instead of pricebookentry in the wrapper class.

Hope this helps if someone gets the same or similar problem. However, I am open if any body wants my help to get more knowledge about this functionality.

Sunday 15 January 2012

Close Popup and refresh parent window


It is very common requirement to open a pop up by clicking a button . Once you are done with pop up, close the pop up and refresh the parent window so that user will be able to see the update on parent window at the same time.

Here is the sample code for it:

<apex:commandbutton action="{!save}" id="button" oncomplete="javascript:CloseAndRefresh()" value="Update"/>

and here is the java script function

<script language="JavaScript" type="text/javascript">
function CloseAndRefresh(){
window.opener.location.href="/{!$CurrentPage.parameters.id}";
      window.top.close();
   
  }
</script>

Note : I would suggest to check this setting Setup-> Personal Setup  ->  My Personal Information -> Personal Information
Here check for "Development Mode". I have seen at various places and at community as well that if this check box is checked then popup would not close. Here, I would suggest to turn this checkbox off while you are testing this.