function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Robert Davis 16Robert Davis 16 

Error on Controller Extension Delete Method

I have at least two issues, 1) Having to press the <apex:commandLink> twice inorder to invoke deleteRecord method in Controller Extension, and 2) Method returning an error on Delete but DML statement working to Delete Record. (Error Message Received: Delete failed. First exception on row 0 with id 01Mj0000006bzD3EAI; first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: [] )

The goal was to create a tab in my visualforce page that displayed the Account Team Members with a link to their Chatter profile, email address, Company Name, title and Account Team Member Role. To be able to add members, edit members and delete members from this page.

Controller Extension:
public class MyProfilePageController2 {

    public List<AccountTeamMember> myTeam { get; set; }
    public Account acct {get; set;}
    public String acctID {get; set;}
    public String ATMID {get;set;}
    
    public MyProfilePageController2(ApexPages.StandardController controller) {
        Account acct = (Account) controller.getRecord();
        
        myTeam = [SELECT Id,AccountId,TeamMemberRole, User.Country,UserId, User.Name, User.Email, User.CompanyName, User.Title, User.FullPhotoUrl, User.SmallPhotoUrl FROM AccountTeamMember where AccountId =:acct.Id];
        
    }
    public PageReference addRedirect(){
        
        acctID = ApexPages.currentPage().getParameters().get('id');
       
        PageReference redir = new PageReference('/opp/salesteaminsert.jsp?retURL=%2F'+ acctID + '&id=' +acctID);
        redir.setRedirect(true);
        return redir;
    }
   public PageReference deleteRecord(){
        
       AccountTeamMember atm1 = new AccountTeamMember(id=ATMId);
       
       delete atm1;
       
       return null;
    }

}

Visualforce Page:
<apex:page StandardController="Account" extensions="MyProfilePageController2">

  <apex:tabPanel switchType="client">
   
    <!-- first tab - overview of the account with the standard detail component -->
    <apex:tab label="Overview" name="Overview">
      <apex:detail relatedList="false" /> 
    </apex:tab>
      
    <!-- second tab - iterate the contacts related list-->
    <apex:tab label="Contacts" name="Contacts">
      <apex:repeat value="{!Account.contacts}" var="contact">
        <apex:pageBlock title="{!contact.name}">
          <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
              <apex:outputLabel value="Name"/>
              <apex:outputLink value="{!URLFOR($Action.Contact.View, contact.id)}">
                <apex:outputField value="{!contact.name}" />
              </apex:outputLink>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem />
            <apex:outputField value="{!contact.Email}" />
            <apex:outputField value="{!contact.HasOptedOutOfEmail}" />
            <apex:outputField value="{!Contact.Phone}" />
            <apex:outputField value="{!Contact.DoNotCall}" />
            <apex:outputField value="{!contact.Fax}" />
            <apex:outputField value="{!contact.HasOptedOutOfFax}" />
          </apex:pageBlockSection>
        </apex:pageBlock>
           
        <!-- include the open activities for the contact via the standard relatedList component -->
        <apex:relatedList subject="{!contact}" list="OpenActivities" />
        <br/>
      </apex:repeat>
    </apex:tab>
      
    <!-- third tab - iterate the opportunities related list -->
    <apex:tab label="Open Opportunities" name="Open Opportunities">
      <apex:repeat value="{!Account.Opportunities}" var="opp">
      
        <!-- only show open opportunities -->
        <apex:outputPanel rendered="{!NOT(opp.IsClosed)}">
          <apex:pageBlock title="{!opp.Name}">
            <apex:pageBlockSection >
              <apex:pageBlockSectionItem >
                <apex:outputLabel value="Name"/>
                <apex:outputLink value="{!URLFOR($Action.Opportunity.View, opp.id)}">
                  <apex:outputField value="{!opp.name}" />
                </apex:outputLink>
              </apex:pageBlockSectionItem>
              <apex:pageBlockSectionItem />
              <apex:outputField value="{!opp.CloseDate}" />
              <apex:outputField value="{!opp.Amount}" />
              <apex:outputField value="{!opp.StageName}" />
              <apex:outputField value="{!opp.Probability}" />
            </apex:pageBlockSection>
          </apex:pageBlock>
           
          <!-- include the line items for the opportunity via the standard relatedList component -->
          <apex:relatedList subject="{!opp}" list="OpportunityLineItems" />
        </apex:outputPanel>
      </apex:repeat>
    </apex:tab>
    

    
  <!--Fourth Panel-->

  <apex:tab label="Account Team Members" name="Account Team">
     
<apex:outputPanel >
       <apex:form >  
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:commandButton value="Add" action="{!addRedirect}" />
                
            </apex:pageBlockSection>

            <apex:pageBlockTable value="{!myTeam}" var="r">
                <apex:column >
                    <a href="/acc/salesteamedit.jsp?id={!r.id}&retURL={!r.AccountId}">Edit</a>
                </apex:column>
                <apex:column>
                    <apex:commandLink value="Delete" action="{!deleteRecord}">
                       <apex:param name="myParam" value="{!r.id}" assignTo="{!ATMId}"/>
                    </apex:commandLink>
                </apex:column>
                <apex:column >
                    <apex:image id="profileImage" value="{!r.User.SmallPhotoUrl}" />
                </apex:column>
                <apex:column headerValue="Name">
                   <a href="/_ui/core/userprofile/UserProfilePage?u={!r.User.id}">{!r.User.Name}</a>
                </apex:column>
                <apex:column value="{!r.User.Title}"/>
                <apex:column value="{!r.TeamMemberRole}"/>
                <apex:column value="{!r.User.CompanyName}"/>
                <apex:column value="{!r.User.Email}"/>
                <apex:column value="{!r.User.Country}"/>  
            </apex:pageBlockTable>

      </apex:pageBlock>
      </apex:form>
</apex:outputPanel>
     
</apex:tab>
    </apex:tabPanel>
</apex:page>

Am I approaching this wrong?

Your Help is Appreciated.

 
Best Answer chosen by Robert Davis 16
Robert Davis 16Robert Davis 16
The following is the answer that appears to be working for me. I am grateful to the following Blog post: http://bobbuzzard.blogspot.com/2011/07/passing-parameters-to-apex-method-from.html
 
public class MyProfilePageController2 {

    public List<AccountTeamMember> myTeam { get; set; }
    public Account acct {get; set;}
    public String acctID {get; set;}
    public String ATMID {get;set;}
    private ApexPages.StandardController stdCtrl {get; set;}
    
    public MyProfilePageController2(ApexPages.StandardController controller) {
        stdCtrl= controller;
        setupAccountTeam();
                
    }
    public void setupAccountTeam(){
        myTeam = [SELECT Id,AccountId,TeamMemberRole, User.Country,UserId, User.Name, User.Email, User.CompanyName, User.Title, User.FullPhotoUrl, User.SmallPhotoUrl FROM AccountTeamMember where AccountId =: stdCtrl.getId()];

    }
    
    public PageReference addRedirect(){
        
        acctID = ApexPages.currentPage().getParameters().get('id');
       
        PageReference redir = new PageReference('/opp/salesteaminsert.jsp?retURL=%2F'+ acctID + '&id=' +acctID);
        redir.setRedirect(true);
        return redir;
    }
   public PageReference deleteRecord(){
        
       AccountTeamMember atm1 = new AccountTeamMember(id=ATMId);
       
       delete atm1;
       setupAccountTeam();
       
       return null;
    }

}