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>
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.