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
aapt.devaapt.dev 

How to save mass edited records in a VF page

Hi
 
I'm trying to mass edit Contacts related to an account but am having problems with saving the changes to the records.
 
My APEX class:
 
public class accountContactList {
    public accountContactList(ApexPages.StandardController controller) {

    }
   
  public Account getAccount() {
    return [select id, name,
             (select id, name, firstName, lastName, title, phone, mobilePhone, email from Contacts limit 5)
             from Account where id =
             :ApexPages.currentPage().getParameters().get('Id') ];
}

public String getName() {
  return 'Account Contact List';
  }
 
  public PageReference save() {
        return null;
    }
}
 
My VF page:
 
<apex:page standardController="Contact" extensions="accountContactList" id="thePage"
 <apex:pageBlock title="Mass Edit Contacts">
 </apex:pageBlock>
 <apex:form >
  <apex:pageBlock >
   <apex:pageMessages /> 
    <apex:pageBlockButtons >
     <apex:commandButton action="{!save}" value="Save" id="theButton"/>
    </apex:pageBlockButtons>
    <apex:pageBlockTable value="{!account.Contacts}"
                         var="contact">

   <apex:column value="{!contact.name}" width="25" />
      <apex:column headerValue="Title" width="100">
      <apex:inputField value="{!contact.title}"/>
     </apex:column>
          <apex:column headerValue="Phone" width="15">
      <apex:inputField value="{!contact.phone}"/>
     </apex:column>
     <apex:column headerValue="Mobile" width="15">
      <apex:inputField value="{!contact.mobilephone}"/>
     </apex:column>
     <apex:column headerValue="E-Mail" width="25">
      <apex:inputField value="{!contact.email}"/>
     </apex:column>

         </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
 </apex:page>
 
I am getting the following error message:
 
Id value 0018000000Mif1y is not valid for the Contact standard controller (the ID being that of the account)
 
Any guidance would be much apprecaited from this newbie
 
Thanks in advance
DManelskiDManelski
If you're trying call your VF page from a list button on the account record, selecting records from a contacts related list, then you'll need to create a VF page that uses the standard controller on contact and extension for the standardSetController to grab the selected records. I've written something similar to mass update a bunch of opportunities from the contacts or accounts related lists.  My VF page is just calling an action however, but there's no reason why you couldn't throw the set of selected contact records into your VF page and then edit and save them using standard controller methods.


Code:
public class ONEN_EXT_UpdateOpportunities {

 public List<Opportunity> selectedOpps;

 public ONEN_EXT_UpdateOpportunities(ApexPages.StandardSetController controller) {
       this.selectedOpps = (List<Opportunity>)controller.getSelected();
 }
 
 public pageReference MarkAsWon() {
  
  List<Opportunity> OppsToUpdate = new List<Opportunity>();
  for (Opportunity newOpp : selectedOpps) {
   newOpp = new Opportunity (
    id = newOpp.id,
    StageName = ONENW_Constants.OPP_DEFAULT_WON_NOT_THANKED_STAGE
   );
   OppsToUpdate.add(newOpp);
  }
  
  if (OppsToUpdate.size() > 0) {
   update OppsToUpdate;
  }
  PageReference p = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
        p.setRedirect(true);
        return p;
  
 }
}

 And the VF page:
Code:
<apex:page standardController="Opportunity" recordSetVar="opportunities" extensions="ONEN_EXT_UpdateOpportunities" action="{!MarkAsWon}"> 
</apex:page>

 One caveat, I'm relatively new to this stuff myself and there may be an entirely easier way just using the standard controller



Message Edited by DManelski on 01-01-2009 11:14 PM
DManelskiDManelski
Upon further review, I think you can just use a standard controller, you don't need any custom controllers/extensions.  Here's a good working example of a similar use case to yours:

http://www.x2od.com/2008/11/13/project-change-owner-button-in-visualforce.html


Message Edited by DManelski on 01-01-2009 11:20 PM
AlexYAlexY
The final solution, using standard controller (no apex required)
<apex:page standardController="Contact" recordSetVar="contacts" tabStyle="Contact" sidebar="false">

<apex:form >
<apex:pageBlock title="Mass Edit Contacts" mode="edit">
<apex:pageMessages />
<apex:pageblockButtons location="top">
<apex:commandButton value="Save" action="{!Save}" />
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!selected}" var="contact">
<apex:column headerValue="Name">
<apex:outputField value="{!contact.name}"/>
</apex:column>
<apex:column headerValue="Title">
<apex:inputField value="{!contact.title}"/>
</apex:column>
<apex:column headerValue="Phone">
<apex:inputField value="{!contact.phone}"/>
</apex:column>
<apex:column headerValue="Mobile Phone">
<apex:inputField value="{!contact.mobilePhone}"/>
</apex:column>
<apex:column headerValue="Email">
<apex:inputField value="{!contact.email}"/>
</apex:column>
<apex:column headerValue="Inactive">
<apex:inputField value="{!contact.Inactive_Contact__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>