0

I have a visualforce (for customer portal) that allows to create Accounts, Contacts and Opportunities. To simplify the code I have a variable type SObject where I populate the fields for the created record (either Account, Opportunity or Contact) and then save the changes, however when I try to open the visualforce I'm getting "Insuficient privileges". I'm currently logged as an admin.

Any idea what could it be?

This is my controller:

public SObject createdRecord {get;set;}

public CustomerPortalController() {

        if (ApexPages.currentPage() == Page.CustomerPortalAccounts) {
            this.createdRecord = Schema.getGlobalDescribe().get('Account').newSObject();
        } else if (ApexPages.currentPage() == Page.CustomerPortalContacts) {
            this.createdRecord = Schema.getGlobalDescribe().get('Contact').newSObject();
        } else if (ApexPages.currentPage() == Page.CustomerPortalOpportunities) {
            this.createdRecord = Schema.getGlobalDescribe().get('Opportunity').newSObject();
        } else if (ApexPages.currentPage() == Page.CustomerPortalSistemas) {
            this.createdRecord = Schema.getGlobalDescribe().get('Sistemas__c').newSObject();
        }
}

And this is the VF:

<apex:form styleClass="form-horizontal">
   <div class="form-body">
        <div class="row">
           <div class="col-md-6">
               <div class="form-group">
                   <label class="control-label col-md-3">Nombre de la cuenta:*</label>
                   <div class="col-md-9">

                       <!-- If I comment this line below I don't get any errors, but when is not commented I get Insuficient privileges -->
                       <apex:inputField value="{!createdRecord['Name']}" styleClass="form-control"/>

                   </div>
               </div>
           </div>
       </div>
   </div>
   <div class="form-actions">
       <div class="row">
           <div class="col-md-6">
               <div class="row">
                   <div class="col-md-offset-3 col-md-9">
                       <apex:commandButton value="Guardar" action="{!saveRecord}" styleClass="btn green"/>
                       <a href="/apex/CustomerPortalAccounts" class="btn default" role="button">Cancelar</a>
                   </div>
               </div>
           </div>
           <div class="col-md-6"> </div>
       </div>
   </div>
</apex:form>
3
  • Insufficient privilege errors are not usually related to code
    – glls
    Commented Aug 10, 2017 at 20:55
  • I'm not sure if this is what is causing your error, but PageReference variables compare by reference, so your if/else conditions are (probably) never going to be true. You'll probably either need to parse the URL, pass in a query string parameter or find another way to indicate the current page to the controller. Commented Aug 11, 2017 at 1:31
  • @IllusiveBrian you are absolutely right. It wasn't a privilege problem, createdRecrord was never initialized so it was null and that was the problem. Not sure why a NPE wasn't thrown but adjusting the if/else to check for the URL did the trick. Commented Aug 11, 2017 at 17:20

2 Answers 2

1

PageReference variables use referential comparison by default. What that means is that when you use ==, you are comparing whether the two variables reference the same object literally, it is not a comparison of whether they are for the same Visualforce page. Because of this, your createdRecord never gets populated and is null, which I believe will throw a Visualforce equivalent of a NullPointerException when you attempt to dereference it on the page. Depending on your user's permissions, this can cause the "Insufficient Privileges" page rather than showing the actual exception.

0

Are you using customer community license (or the old high volume license)? This license cannot access the Opportunity object at all. Even though Apex runs in the system context and could access it, Visualforce enforces the user license restrictions and so your page throws the insufficient privileges error.

You'll have to use a wrapper object to display or edit opportunity data. See these earlier threads:

Is this a good mix: Authenticated Site - Customer Portal License - Opportunities

VF controller system context versus Sites Guest User object permissions

You must log in to answer this question.

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