Building Visualforce pages for Standard Pages : Part 3 – Case Edit Page

Building Visualforce pages for Standard Pages : Part 1 – Account Edit Page
Building Visualforce pages for Standard Pages : Part 2 – Contact Edit Page
Building Visualforce pages for Standard Pages : Part 3 – Case Edit Page

Continuing my series of posts about building Visualforce pages that mimic standard Salesforce pages, here’s the code for the Case Edit page. This one needs an extension class as well, since some of the functionality cannot  be achieved using just Visualforce. Checkout how the Assignment rules are being set in Apex.

Steps to use this template
1.
Create a Visualforce page (I used the name “CaseEdit”), and copy all the code below.
2. Create an Apex Class (I used the name “CaseEditExtension”), and copy all the code from the Apex extension below.
2. Save the page, and test it by visiting the URL below, making sure to pass an existing case ID as a parameter (refer to my article here if you are fuzzy about Salesforce URLs)  :-

https://{your org}.visual.force.com/apex/CaseEdit?id=005E000000HCekM&retURL=%2F005E000000HCekM

3. Create a custom “Edit” button for Case, that takes you to this page when clicked.

Code Snippet
<apex:page standardController="Case" showHeader="true" sidebar="true" extensions="CaseEditExtension">
    <apex:form id="myForm">
        <apex:sectionHeader title="Case Edit" subtitle="{!Case.CaseNumber}"/>
        <apex:pageBlock id="pgBlock" mode="edit" title="Case Edit">
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!Save}" />
                <apex:commandButton value="Save & Close" action="{!SaveAndClose}" />
                <apex:commandButton value="Save & New" action="{!SaveAndNew}" />
                <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection id="pgBlockSectionAcctInfo" title="Case Information" collapsible="false" columns="2" >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Case Owner</apex:outputLabel>                   
                    <apex:outputField id="caseOwner" value="{!case.ownerid}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Status</apex:outputLabel>
                    <apex:inputField id="caseStatus" value="{!case.Status}" />
                </apex:pageBlockSectionItem>
               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Case Number</apex:outputLabel>                   
                    <apex:inputField id="caseNumber" value="{!case.CaseNumber}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Priority</apex:outputLabel>
                    <apex:inputField id="casePriority" value="{!case.Priority}" />
                </apex:pageBlockSectionItem>               
               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Contact Name</apex:outputLabel>                   
                    <apex:inputField id="caseContact" value="{!case.ContactId}" />
                </apex:pageBlockSectionItem>               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Case Origin</apex:outputLabel>
                    <apex:inputField id="caseOrigin" value="{!case.Origin}" />
                </apex:pageBlockSectionItem>  
               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Account Name</apex:outputLabel>                   
                    <apex:inputField id="caseAccount" value="{!case.AccountId}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Type</apex:outputLabel>                   
                    <apex:inputField id="caseType" value="{!case.Type}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Case Reason</apex:outputLabel>                   
                    <apex:inputField id="caseReason" value="{!case.Reason}" />
                </apex:pageBlockSectionItem>      
            </apex:pageBlockSection>
           
            <apex:pageBlockSection id="pgBlockSectionAdditionalInfo" title="Additional Information" collapsible="false" columns="2">
            </apex:pageBlockSection>
           
            <apex:pageBlockSection id="pgBlockSectionDescriptionInfo" title="Description Info" collapsible="false" columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Subject</apex:outputLabel>                   
                    <apex:inputText id="caseSubject" value="{!case.Subject}" size="75" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Description</apex:outputLabel>                   
                    <apex:inputTextArea id="caseDescription" value="{!case.Description}" cols="75" rows="6" />
                </apex:pageBlockSectionItem>                
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Internal Comments</apex:outputLabel>                   
                    <apex:inputTextArea id="caseInternalComments" value="{!Comment}" cols="75" rows="6" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
            </apex:pageBlockSection>
           
            <apex:pageBlockSection id="pgBlockSectionOptional" title="Optional" collapsible="false" columns="1">
                <apex:inputCheckBox value="{!UseAssignmentRules}" label="Assign using active assignment rules" />
            </apex:pageBlockSection>           
        </apex:pageBlock>       
    </apex:form>
</apex:page>
Code Snippet
public with sharing class CaseEditExtension {
    public String Comment
    {
        get;
        set;
    }
    public String Description
    {
        get;
        set;
    } 
    public String Subject
    {
        get;
        set;
    }
    public Boolean UseAssignmentRules
    {
        get;set;
    }
   
    public Case cs;
    ApexPages.StandardController controller;
   
    public CaseEditExtension(ApexPages.StandardController con)
    {
        controller = con;
        this.cs = (Case) con.getRecord();
    }
   
    public PageReference Save()
    {
        CaseComment com = new CaseComment();
        if(Comment.Length() > 0)
        {
            com.commentBody = Comment;
            com.ParentId = cs.Id;
            if(UseAssignmentRules == true)
            {
                AssignmentRule  AR = new AssignmentRule();
                AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
               
                //Creating the DMLOptions for "Assign using active assignment rules" checkbox
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
                controller.getRecord().setOptions(dmlOpts);               
            }
            insert com;
        }
        String retURL = ApexPages.currentPage().getParameters().get('retURL');
        String CurrentId = ApexPages.currentPage().getParameters().get('id');
        PageReference redirectPG;
        if(retURL != null)
            redirectPG = new PageReference('/' + retURL);
        else if(CurrentId != null)
            redirectPG = new PageReference('/' + CurrentId);
       
        controller.Save();
       
        return redirectPG;
    }
   
    public PageReference SaveAndNew()
    {
        Save();
        return new PageReference('/500/e');
    }
   
    public PageReference SaveAndClose()
    {
        Save();
        String retURL = ApexPages.currentPage().getParameters().get('retURL');
        String CurrentId = ApexPages.currentPage().getParameters().get('id');
        PageReference redirectPG;
        if(retURL != null)
            redirectPG = new PageReference('/' + retURL);
        else if(CurrentId != null)
            redirectPG = new PageReference('/' + CurrentId);
       
        return redirectPG;
    }   
}

No comments:

Post a Comment