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

Updating Case Status when creating a new case comment

In our world, case owners work a case, create a case comment, and then have to separately edit the case to change the status to either "Open - Waiting on Feedback" or "Closed - Waiting on Feedback." The case submitter follows a similar workflow where they use comments to communicate back to the case owner and also have to edit the case to set the status back to Open after they add a comment.


Does anyone have any suggestions on ways to allow users to change the case status in the same window when they are creating a new case comment? 


Possible ideas (but not sure how to accomplish):

 - Custom field on the case comments window to set the status (either the actual status field or a field that fired a workflow rule to update the real status field once the case comment was saved).

 - Custom button to "Save and Update Status" on the case comments window.

 - Custom button on the case to "Create Comment and Update Status."


If anyone has any code snippets or can point me to other examples of how this can be accomplished, that would be great!




You can create a trigger that will update the Case status after a comment is created.  You can also look to create a Visual Force page that will capture the comment and allow the user to change the status all from the same screen.



Here is some code that should help you out:



<apex:page Controller="newEnhancemnetController" tabStyle="Case" id="thePage"> <script> function confirmCancel() { var isCancel = confirm("Are you sure you wish to cancel?"); if (isCancel) return true; return false; } </script> <apex:sectionHeader title="New Enhancemnet Approval" subtitle="for change board Case"/> <apex:form > <apex:pageBlock mode="edit"> <apex:pageMessages ></apex:pageMessages> <apex:pageBlockButtons > <apex:commandButton action="{!Save}" value="Save"/> <apex:commandButton action="{!Cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Change Current Case Status" columns="1"> <apex:inputField value="{!}"/> <apex:outputField value="{!Case.subject}"/> <apex:pageBlockSectionItem > <apex:outputLabel value="{!$ObjectType.Case.fields.Status.label}" for="casestatus" style="font-weight:bold"/> <apex:selectList value="{!casestatus}" id="casestatus" size="1" title="Status" required="true"> <apex:selectOptions value="{!items}"/> </apex:selectList> </apex:pageBlockSectionItem> <apex:inputField value="{!Case.status}" required="true"/> <apex:inputField value="{!Case.description}"/> </apex:pageBlockSection> <apex:pageBlockSection title="Add Enhancement Approval Information" columns="1"> <apex:inputField id="eaAprovedFor" value="{!EnhancementApproval.Approved_For__c}" required="true"/> <apex:inputField id="eaEnvironment" value="{!EnhancementApproval.Environment__c}" required="true"/> <apex:inputField id="eaValidFrom" value="{!EnhancementApproval.Valid_From__c}" required="true"/> <apex:inputField id="eaValidTo" value="{!EnhancementApproval.Valid_To__c}" required="true"/> </apex:pageBlockSection> <apex:pageBlockSection title="Add a Case Comment"> <apex:inputField id="caseComment" value="{!CaseComment.CommentBody}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>


the controller



public class newEnhancemnetController { String casestatus; Case oCase; Enhancement_Approval__c oEnahncemnetApproval; Contact contact; CaseComment casecomment; public Case getCase() { oCase = [select id, subject, status, description from Case where id = :ApexPages.currentPage().getParameters().get('id')]; if(oCase== null) oCase= new Case(); return oCase; } public Contact getContact() { if(contact== null) contact= new Contact(); return contact; } public String getCaseStatus() { if(casestatus==null){casestatus=oCase.Status;} return casestatus ; } public void setCaseStatus(String casestatus ) { this.casestatus = casestatus ; } public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); for (CaseStatus cs : [Select MasterLabel from CaseStatus order by sortorder]){ options.add(new SelectOption(cs.MasterLabel,cs.MasterLabel)); } return options; } public CaseComment getCaseComment () { if(casecomment== null) casecomment= new CaseComment(); return casecomment; } public Enhancement_Approval__c getEnhancementApproval() { if(oEnahncemnetApproval== null) oEnahncemnetApproval= new Enhancement_Approval__c(); return oEnahncemnetApproval; } public PageReference cancel() { PageReference casePage = new ApexPages.StandardController(oCase).view(); casePage .setRedirect(true); return casePage ; } public PageReference Save() { oCase.Status=this.casestatus; update oCase; Enhancement_Approval__c oEA; oEA = new Enhancement_Approval__c(); oEA.Approved_For__c = oEnahncemnetApproval.Approved_For__c; oEA.Case__c = ApexPages.currentPage().getParameters().get('id'); oEA.Environment__c = oEnahncemnetApproval.Environment__c; oEA.Valid_From__c = oEnahncemnetApproval.Valid_From__c; oEA.Valid_To__c = oEnahncemnetApproval.Valid_To__c; insert oEA; if (casecomment.CommentBody != null){ casecomment.ParentId = ApexPages.currentPage().getParameters().get('id'); insert casecomment; } PageReference casePage = new ApexPages.StandardController(oCase).view(); casePage.setRedirect(true); return casePage ; } //*************************************************************************** //** Test Method //** //*************************************************************************** public static testMethod void testChangeBoardApproval() { pageReference p = Page.ChangeBoardApproval; p.getParameters().put('id', [select id from Case limit 1].id); test.setCurrentPage(p); newEnhancemnetController enhancmenetController = new newEnhancemnetController(); //enhancemnetController.cancel(); enhancmenetController.getCase(); enhancmenetController.getitems(); enhancmenetController.getcasestatus(); enhancmenetController.getEnhancementApproval(); enhancmenetController.getContact(); enhancmenetController.getCaseComment();; } }


 Just ignore my customer oject.