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
Adrian-EAdrian-E 

Custom Controller Help

Hi All,

I would like display all open cases where priority = "high" on one page with the "subject" field already open for editing.

Can anyone help me by showing me what the custom controller would look like and how the page would be built?

PS: This is more like a report but with the fields all open for editing. I would then make the changes to each field and hit "submit" to post all changes at once
ClaiborneClaiborne
It may be simpler to just use the in-line editing ability of salesforce.com.

First, go to Setup, User Interface. Be sure that In-line Editing and Enhanced Lists are both checked/enabled.

Then in the Cases tab, create a new list called High Priority.

Set the criteria for the list (like Status <> Closed and Priority = High), and set the fields to include in the list (Case Number, Account, Contact, Subject, etc.).

When the list is displayed, you can edit the subject by clicking on it. Also, you can edit multiple cases at the same time by selecting them and then editing a single field. When you save, you are prompted to edit the immediate record or all checked records.

If you still need a Visual Force page, let me know.


Adrian-EAdrian-E

Thanks, I was not familair with the Enhanced Lists feature.

My only concerns are that the new display takes a lot longer to load on my computers (using IE7 and slow CPU) and that I have no button to "mass edit" records.

If its not too much trouble I would be most appreciative to see how you would build the visual force page. I am particularly interested in seeing how you bring in data with certain criteria into a page. This was something which I did not entirely understand from the training documentation on Vforce.

 

Thank you!

MyGodItsColdMyGodItsCold
Actually, you can build Visualforce pages with enhancedList capabilities. See:

http://community.salesforce.com/sforce/board/message?board.id=Visualforce&thread.id=5522

I'm sure there will be a way to get the custom controller to do what's needed.
Adrian-EAdrian-E
Thanks for the suggestion.
I am still keen on learning how to do the custom controller thing as part of a broader learning goal.
If anyone can help, much appreciated
ClaiborneClaiborne
OK, here is the code for the custom controller:

Code:
public class EditCases {
    List<Case> cases;
    String headerMessage;
    Boolean canEdit = true;
    
    public EditCases() { 
        cases = getcases();+
        headerMessage = getHeaderMessage();
        canEdit = getCanEdit();         
    }   

    public List<Case> getcases() {
        if (cases == null) {
            cases = [
                select Id, CaseNumber, Priority, Status, Subject, 
                    Account.Name, CreatedDate
                from Case
                where Status <> 'Closed' and Priority = 'High'
                order by CaseNumber];
                
            return cases;
        }
        else {
            return cases;
        }
    }

    public String getHeaderMessage() {
         String message = '';
         if (cases.size() == 0) {
             message = 'There are no cases to edit.';
             canEdit = false;
         }    
         else {
             canEdit = true;
         }
         return message;      
    }
   
    public Boolean getCanEdit() {
        return canEdit;
    }
    
        // Action that is executed if Save button is clicked
    public PageReference save() {
            // Return to Cases tab page after editing
        PageReference returnPage = new PageReference('/500/o');
        
            // Save all changes
        update cases;

        return returnPage; 
    }
    
        // Action that is executed if Cancel button is clicked.
    public PageReference cancel() {
            // Return to Cases tab page after editing    
        PageReference returnPage = new PageReference('/500/o');
        return returnPage;
    }
}

This is the code for the Visual Force page:

Code:
<apex:page controller="EditCases" tabstyle="Case">
<apex:sectionHeader title="Edit Cases" subtitle="Cases with High Priority" />
<apex:form >
<apex:pageBlock mode="edit" title="Edit Case Subject">
<p><apex:outputText value="{!headerMessage}" /></p>
<p></p>
<apex:pageblockSection id="Cases" columns="1" title="Available Cases" rendered="{!canEdit}">
   <apex:pageblocktable align="center" value="{!Cases}" var="Case" title="Cases" width="100%">
         <apex:column headerValue="Case Number" width="15%"><apex:outputLink value="/{!Case.ID}">{!Case.CaseNumber}</apex:outputlink></apex:column>
         <apex:column headerValue="Date/Time Opened" width="15%" value="{!Case.CreatedDate}" />
         <apex:column headerValue="Account" width="15%"><apex:outputField value="{!Case.Account.Name}" /></apex:column>
         <apex:column headerValue="Status" width="15%"><apex:outputField value="{!Case.Status}"/></apex:column>     
         <apex:column headerValue="Subject" width="40%"><apex:inputField value="{!Case.Subject}"/></apex:column>
   </apex:pageblockTable>
</apex:pageblockSection>
<p/>
<apex:pageBlockButtons >
   <apex:commandButton action="{!save}" value="Save Cases" rendered="{!canEdit}" />
   <apex:commandButton action="{!cancel}" value="Cancel" />
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

You can create these in a developer account. Note the name you use for the visual force page. For example, call it editcases.

Once that is done, you need to create a custom button/link that links to:

/apex/editcases

The custom button can be on the home page or on the Cases Detail page or on the the Cases List page.

There is not test code included, but you will need that to move the code to production.

Try all this and come back with more questions.
 

Adrian-EAdrian-E

Thanks a million!

I just tested this and it works perfectly!

Exactly what I needed to accomplish my goal + you have taught me the foundations of creating custom controllers.

Thanks again!

Adrian-EAdrian-E
Now it asks me to create a test scenario in order to package the apex classes for import into my dev account.

Any good references to where I can learn about test scenrios?
ClaiborneClaiborne
Boy, you want everything . . .

Here is the updated controller code, with the test code.

And try reading the examples in the Visual Force Pages reference.

Code:
public class EditCases {
    List<Case> cases;
    String headerMessage;
    Boolean canEdit = true;
    
    public EditCases() { 
        cases = getcases();+
        headerMessage = getHeaderMessage();
        canEdit = getCanEdit();         
    }   

    public List<Case> getCases() {
        if (cases == null) {
            cases = [
                select Id, CaseNumber, Priority, Status, Subject, 
                    Account.Name, CreatedDate
                from Case
                where Status <> 'Closed' and Priority = 'High'
                order by CaseNumber];
                
            return cases;
        }
        else {
            return cases;
        }
    }

    public String getHeaderMessage() {
         String message = '';
         if (cases.size() == 0) {
             message = 'There are no cases to edit.';
             canEdit = false;
         }    
         else {
             canEdit = true;
         }
         return message;      
    }
   
    public Boolean getCanEdit() {
        return canEdit;
    }
    
        // Action that is executed if Save button is clicked
    public PageReference save() {
            // Return to Cases tab page after editing
        PageReference returnPage = new PageReference('/500/o');
        
            // Save all changes
        update cases;

        return returnPage; 
    }
    
        // Action that is executed if Cancel button is clicked.
    public PageReference cancel() {
            // Return to Cases tab page after editing    
        PageReference returnPage = new PageReference('/500/o');
        return returnPage;
    }
    
    public static testMethod void tesEditCases() {
            // Create the necessary records to test automatic order creation
            // Create a case
            // This assumes that the pick list values used exist for
            //   -  Priority (High)
            //   -  Status (New)
            //   -  Origin (Phone)
        Case testCase = new Case(
            Priority = 'High',
            Subject = 'Test Subject',
            Status = 'New',
            Origin = 'Phone');
        insert testCase;
        
             //**************************
            // Test EditCases controller
            //**************************           
        PageReference pageRef = Page.EditCases;
        Test.setCurrentPage(pageRef);
        
            // Create an instance of the controller
        EditCases editCasesController = new EditCases();
        
            // Get the list of cases from the controller
        List<Case> casesToEdit = editCasesController.getCases();
            
            // Verify that there is at least one case to edit
        system.assertEquals(casesToEdit.size() > 0, true);
        
            // Retrieve the header message
        String editCasesMessage = editCasesController.getHeaderMessage();
        
            // Verify that the header message is blank
        system.assertEquals(editCasesMessage, '');

            // Check the Cancel action
        String canceleditCasesPage = editCasesController.cancel().getUrl();
        
            // Check the save action
        String editCasesPage = editCasesController.save().getUrl();
    }   
}

 

Adrian-EAdrian-E
I am in your debt!
 
Thank you so much!