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
CapCbRMCapCbRM 

Need Help with Save on Custom Controller

Hi,

 

I am pretty new to Visualforce... I have the developer guide printed out in front of me trying to get my head wrapped around everything.

 

I currently have a custom controller that pulls a bunch of records from a custom object called Milestone__c based on the Opp ID of the current page.That works fine, I am just trying to get a save button to work so I/whoever can update the milestones quickly. I used the "An Approach to Inline Editting" tutorial and that works fine but now am thinking that a save button on the list would be better. Can anyone help me with this?

 

I really want to make the Status field a drop down where you can edit multiple then save. Right now it is a drop down but I can't figure out the Save function, I've tried a bunch of different approaches based on posts but have been unsuccessful.

 

VF:

 

<apex:page Controller="TestMilestones" showHeader="false" > <apex:form > <apex:pageBlock title="Milestones" > <apex:pageBlockButtons location="top"> <apex:commandButton action="{!save}" value="save" id="saveButton"/> </apex:pageBlockButtons> <apex:outputPanel id="projectList"> <table> <tr> <th style="width:25px; "> </th> <th style="width:25px; "> </th> <th style="width:150px; ">Name</th> <th style="width:68px; ">Status</th> <th style="width:30px; ">Duration</th> </tr> <apex:repeat value="{!projects}" var="p"> <tr style="height:22px"> <apex:outputPanel id="editRow" layout="none" rendered="{!p.Id == editProject.Id}"> <td><apex:commandLink action="{!cancelEdit}" rerender="projectList">Cancel</apex:commandLink></td> <td><apex:commandLink action="{!saveEdit}" rerender="projectList">Save</apex:commandLink></td> <td><apex:inputField rendered="{!p.Id == editProject.Id}" value="{!editProject.Name}"/></td> <td><apex:inputField rendered="{!p.Id == editProject.Id}" value="{!editProject.Status__c}"/></td> <td><apex:inputField rendered="{!p.Id == editProject.Id}" value="{!editProject.Duration__c}"/></td> </apex:outputPanel> <apex:outputPanel id="viewRow" layout="none" rendered="{!p.Id != editProject.Id}"> <td style="font-style:italic; font-size:10px"> <apex:commandLink action="{!del}" onclick="return confirm('Are you sure you want to delete this project?')">Del <apex:param name="delid" value="{!p.Id}"/> </apex:commandLink> </td> <td style="font-style:italic"> <apex:commandLink action="{!edit}" rerender="projectList">Edit <apex:param name="editid" value="{!p.Id}"/> </apex:commandLink> </td> <td>{!p.Name}</td> <td style="background-color:yellow"><apex:inputField id="stat" value="{!p.Status__c}" /></td> <td><CENTER>{!p.Duration__c}</CENTER></td> </apex:outputPanel> </tr> </apex:repeat> </table> <CENTER><STRONG>**Click on status to update it</STRONG></CENTER> </apex:outputPanel> </apex:pageBlock> </apex:page>

 

 Controller:

 

public class TestMilestones { public TestMilestones(ApexPages.StandardController controller) { } public Milestone__c newProject { get; set; } public Milestone__c editProject { get; set; } public TestMilestones() { newProject = new Milestone__c(); } public Milestone__c[] getProjects() { return [SELECT Name, Duration__c, Status__c FROM Milestone__c WHERE Opportunity__c = :ApexPages.currentPage().getParameters().get('id') ORDER BY CreatedDate DESC]; } public String getParam(String name) { return ApexPages.currentPage().getParameters().get(name); } public PageReference add() { try { INSERT newProject; // if successful, reset the new project for the next entry newProject = new Milestone__c(); } catch (Exception e) { ApexPages.addMessages(e); } return null; } public PageReference del() { try { String delid = getParam('delid'); Milestone__c project = [SELECT Id FROM Milestone__c WHERE ID=:delid]; DELETE project; } catch (Exception e) { ApexPages.addMessages(e); } return null; } public PageReference edit() { String editid = getParam('editid'); editProject = [SELECT Name, Duration__c, Status__c FROM Milestone__c WHERE Id=:editid]; return null; } public PageReference cancelEdit() { editProject = null; return null; } public PageReference saveEdit() { try { UPDATE editProject; editProject = null; } catch (Exception e) { ApexPages.addMessages(e); } return null; } }

 

 Any help would be greatly appreciated

 

 

Best Answer chosen by Admin (Salesforce Developers) 
CapCbRMCapCbRM

SPD,

 

After some more reading and testing I finally figured it out:

 

public class PullMilestones{

public Milestone__c[] milestones = new Milestone__c[0];

public PullMilestones(ApexPages.StandardController controller) {

}


public PageReference save() {
UPDATE milestones;
return null;
}


public Milestone__c[] getMilestones() {
//Milestone__c[] milestones;

milestones = [SELECT ID, Name, Duration__c, Status__c, Due_Date__c, Start_Date__c FROM Milestone__c WHERE Opportunity__c = :ApexPages.currentPage().getParameters().get('id')
ORDER BY CreatedDate];

return milestones;

}

}

 I did have to use Opp as the standardController and make a Save save method as shown above.

 

Problems were;

  1. I didn't have a blank array declared at the top 

  2. I didn't have the query be pulled into a variable

  3. Wasn't returning the variable  (in this case 'milestones')

Finally, just had to add  an UPDATE to the milestones variable within the save method and return null, boom done :)

 

Hopefully this will help someone in the future

Message Edited by CapCbRM on 03-25-2010 05:51 AM

All Answers

SPDSPD

1) You havent implemented the save method in your controller....

 

2) In your visualForce page you are using controller = "TestMileStones" ...

 

 

so  what i suggest is that you either implement the Save method in your controller ...Or to have a standard salesforce Save...you need to use standardController attribute of apex:page...i.e

 

<apex:page standardController = "Milestone__c" extensions = "TestMilestones">

CapCbRMCapCbRM

If I change to the standardController with Milestone__c and the extension I get an error on the Opp page where it will be displayed:

 

Content cannot be displayed: Id value 006Q0000003hx4D is not valid for the Milestone__c standard controller

 

I then change it to standardController Opportunity (with extension) and it displays the milestones correctly but the save button does not work, it just reRenders the data and get rid of the Edit link that was there before.

 

Is {!save} the correct method to call from the standardController?

 

Edit: But can I save the milestones if the standardController is Opportunity?

 

 

 

Message Edited by CapCbRM on 03-24-2010 08:14 AM
SPDSPD

Im sorry i misread your post...yes you will need to make standard controler as opportunity

 

you will probably need to implement the save method in your code...coz i dont think it is possible to save milestones if you use standard controller as opportunity...

 

 

CapCbRMCapCbRM

SPD,

 

After some more reading and testing I finally figured it out:

 

public class PullMilestones{

public Milestone__c[] milestones = new Milestone__c[0];

public PullMilestones(ApexPages.StandardController controller) {

}


public PageReference save() {
UPDATE milestones;
return null;
}


public Milestone__c[] getMilestones() {
//Milestone__c[] milestones;

milestones = [SELECT ID, Name, Duration__c, Status__c, Due_Date__c, Start_Date__c FROM Milestone__c WHERE Opportunity__c = :ApexPages.currentPage().getParameters().get('id')
ORDER BY CreatedDate];

return milestones;

}

}

 I did have to use Opp as the standardController and make a Save save method as shown above.

 

Problems were;

  1. I didn't have a blank array declared at the top 

  2. I didn't have the query be pulled into a variable

  3. Wasn't returning the variable  (in this case 'milestones')

Finally, just had to add  an UPDATE to the milestones variable within the save method and return null, boom done :)

 

Hopefully this will help someone in the future

Message Edited by CapCbRM on 03-25-2010 05:51 AM
This was selected as the best answer
SPDSPD
great...
MiaCMiaC

Hi CapCbRM,

 

I understand that you have posted here a while ago and I happened to bump into your reply. We have quite an exact same issue as you have. However, the suggested implementation doesn't quite seem to work and would probably need a hand from you to see what is the cause. Our data model is similar to yours in a manner that:

 

Employee = Opportunity

Team Assignment = Milestone (Child object to Opportunity)

 

Our view requires the following:

 

- inline editing for the records displayed

- once updated, we would want to have a 'Save' button to save the updated values

 

Note: The default commandbutton {!save} doesn't seem to be doing the work and all the updates were not saved and it was ignored

 

Following is the code that we have written.

 

Controller:

public class MyCustomController {

    public Team_Assignment__c[] teamAssignment = new Team_Assignment__c[0];

    public MyCustomController(ApexPages.StandardController controller) {
    }
    
    public PageReference save() {
     UPDATE teamAssignment;
     return null;
    }
    
    Public Team_Assignment__c[] getCurrentTeamAssignments() {
       teamAssignment = [Select id, Account__c, Account__r.Name, Allocation__c, Start_Date__c, End_Date__c from Team_Assignment__c where Employee__c = :ApexPages.currentPage().getParameters().get('id') and End_Date__c = NULL];
       return teamAssignment;
    }

}

 

VF:

 

<apex:page standardController="SFDC_Employee__c" extensions="MyCustomController">
   <apex:form id="theForm">
    <apex:tabPanel switchType="client" selectedTab="teamassign" id="theTabPanel">

    <apex:tab label="Team Assignments" name="teamAssignment" id="tabOne">
    <apex:pageBlock id="thePageBlock">
    <apex:pageBlockButtons >
        <apex:commandButton action="{!edit}" id="editButton" value="Edit"/>
        <apex:commandLink action="{!save}" rerender="theTabPanel">Save</apex:commandLink>
    <apex:commandbutton onclick="window.open('{!URLFOR($Action.Team_Assignment__c.New, $ObjectType.Team_Assignment__c)}');" value="New Team Assignment"> </apex:commandbutton>
    </apex:pageBlockButtons>
    
    <apex:pageBlockSection title="Current Team Assignments" columns="1" collapsible="false">
        <apex:pageBlockTable value="{!CurrentTeamAssignments}" var="each" id="thePageBlockTable1">
        <apex:column width="400" headerValue="Account">
            <apex:outputField value="{!each.Account__c}" id="A1"/>
            </apex:column>
            <apex:column width="150" headerValue="Allocation">
            <apex:outputField value="{!each.Allocation__c}" id="A2"/>
        </apex:column>
        <apex:column width="150" headerValue="Start Date">
        <apex:outputField value="{!each.Start_Date__c}" id="A3"/>
        </apex:column>
        <apex:column width="150" headerValue="End Date">
        <apex:outputField value="{!each.End_Date__c}" id="A4"/>
            </apex:column>
        <apex:inlineEditSupport showOnEdit="saveButton, cancelButton"  hideOnEdit="editButton" event="ondblclick"  changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
      </apex:pageBlockTable>
     </apex:pageBlockSection>  

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

 

Will appreciate your help on this. Thanks.

anand rastogianand rastogi
Hi,

while executing the below code iam getting an error
Error: savecontroller Compile Error: Variable does not exist: con at line 12 column 34
pls help

 

<apex:page standardController="Rigistration__c" extensions="savecontroller" recordSetVar="newtab" tabStyle="Rigistration__c">
  <apex:form >
    
     <apex:sectionHeader title="Create User" help="/help/doc/user_ed.jsp?loc=help"/>
    
     <apex:pageblock >
     <h1 style="text-align:center;color:green;"> Fill the Required Informatoin</h1>
      <br/><br/>
      <apex:pageblockButtons >
        <apex:commandButton value="save" action="{!save}" style="background:yellow" dir="bottom" id="btnsave"/>
        <apex:commandButton value="cancel" action="{!cancel}" style="background:yellow" dir="bottom"/>
         </apex:pageblockButtons>
      
      
      
       <apex:pageblocksection >
       <apex:inputfield value="{!Rigistration__c.UserName__c}"/>
         <apex:inputfield value="{!Rigistration__c.UserNickName__c}"/>
          <apex:inputfield value="{!Rigistration__c.Email__c}" required="true"/>
           <apex:inputsecret value="{!Rigistration__c.Password__c}"/>
            <apex:inputsecret value="{!Rigistration__c.ConfrimPassword__c}"/>
      
              </apex:pageblockSection>
         </apex:pageblock>
     </apex:form>
    </apex:page>



controller

public class savecontroller

   {

  public string save{set; get;}
  Rigistration__c rig;
 

    public savecontroller(ApexPages.

StandardSetController controller)
   
    {
       this.rig=(Rigistration__c)con.gerRecord();
    }

    public PageReference save()

     {

     if(rig!=null)
     insert rig;
     PageReference pr=new PageReference( '/apex/newpage');
     pr.setRedirect(true);
     return pr;

     }

    }