3

I am having the following error that I haven't seen before:

System.VisualforceException: Update access denied for Contact, controller action methods may not execute

I am using a custom controller.

public without sharing class TestController {
    public Contact myCon {
        get {
            myCon = [SELECT Id, Name, FirstName, MobilePhone FROM Contact WHERE Id =: '003N000000SDkc6'];
            return myCon;
        } 
        set; 
    }

    public TestController() {   }

    public void test(){
        System.debug('@@ ' + JSON.serialize(myCon));
        update myCon;
    }
}

and a Visualforce Page:

<apex:page showHeader="true" sidebar="true" controller="TestController">
    <apex:form>
        <apex:inputText value="{!myCon.MobilePhone}" />
        <apex:commandButton value="save" action="{!test}" />
    </apex:form>
</apex:page>

The page is in a Salesforce site. I know that sites has limitation with editing, but I have similar code in other orgs, and I have been able to update opportunities.

I don't know what I am doing wrong or if there is an extra set up in the site that I am not aware off?

5

In public access setting of a Force.com site the maximum permissions that you can specify for Contact object are Read and Create. (Edit and Delete are not allowed).

Your page is directly providing edit functionality of Contact record, So you are facing this error. You are binding your contact object directly on page which is leading to problem.

As a workaround you can use code similar to below :

public without sharing class TestController {
    public Contact myCon {get ; set; }
    public String mobNumber {get;set;}
    public TestController() {
        myCon = [SELECT Id, Name, FirstName, MobilePhone FROM Contact WHERE Id =: '003N000000SDkc6'];
    mobNumber=myCon.MobilePhone ;
    }

    public void test(){
        System.debug('@@ ' + JSON.serialize(myCon));
        myCon.MobilePhone=mobNumber ;
        update myCon;
    }
}

On page make changes as:

<apex:page showHeader="true" sidebar="true" controller="TestController">
<apex:form>
    <apex:inputText value="{!mobNumber}" />
    <apex:commandButton value="save" action="{!test}" />
</apex:form>
  • Thank you. I have my code initially like that and didnt work. Well the contact: myCon was a Lazy loader and not in the constructor, I have edited my question for easily see the code. I am wondering why this behaviour? – manza Nov 12 '16 at 21:36
  • works with big objects too! – Ryan Sherry Dec 11 '19 at 20:11
  • Oh man! You really got me out of more days trying to track this down. Thanks. – Karuhanga Jan 4 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.