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
bouscalbouscal 

Custom Action - VF create child Case with parent values

I'd like an Action on the Case object to create a child case with several values populated from the parent Case.  I'm reviewing the code here and attempting to make some modifications.  Any direction will be appreciated.

Controller: 
public with sharing class CreateCaseExtension {
    private final SObject parent;
    public Case theCase {get; set;}
    public Case pCase {get; set;}
    public String lastError {get; set;}
    
    public CreateCaseExtension(ApexPages.StandardController controller) {
        parent = controller.getRecord();
        pCase = (Case) controller.getRecord();
        theCase = new Case();
        theCase.accountId = parent.accountid;
        theCase.contactId = parent.contactid;
        lastError = '';
    }
      
    public PageReference createCase() {
        createNewCase();
        theCase = new Case();
        theCase.AccountId = parent.accountId; 
        theCase.ContactId = parent.ContactId;
        theCase.parentid= pCase.id;
        theCase.Product_Group__c = pCase.Product_Group__c; 
        theCase.Product__c = pCase.Product__c; 
        theCase.Origin = 'Tech Support';
        return null;
    }
       
     private void createNewCase() {      
        try {
            insert theCase;
            
            FeedItem post = new FeedItem();
            post.ParentId = ApexPages.currentPage().getParameters().get('id');
            post.Body = 'created a case';
            post.type = 'LinkPost'; 
            post.LinkUrl = '/' + theCase.id;
            post.Title = theCase.Subject;
            insert post;
        } catch(System.Exception ex){
           lastError = ex.getMessage();
        }
    }   
}

VF Page: 
<apex:page standardcontroller="Case" extensions="CreateCaseExtension" showHeader="false">

    <script type='text/javascript' src='/canvas/sdk/js/publisher.js'/> 
    <style>
        .requiredInput .requiredBlock, .requiredBlock {background-color: white;} 
        .custompubblock div {display: inline-block;} 
        .custompublabel {width:54px;} 
    </style>
    <script> 
        function refreshFeed() { 
            Sfdc.canvas.publisher.publish({name : 'publisher.refresh', payload : {feed:true}}); 
        }
    </script>   
    <div> 
        <apex:form > 
            <apex:actionFunction action="{!createCase}" name="createCase" rerender="out" 
            oncomplete="refreshFeed();"/> 
            <apex:outputPanel id="out" > 
                <div class="custompubblock"> 
                    <div class="custompublabel">Case:</div><apex:inputField value="{!pCase.id}" 
                    style="margin-left:0;"/>&nbsp;&nbsp;&nbsp;
                    <div>Contact:&nbsp;</div><apex:inputField value="{!pCase.contactId}" />
                </div>
                <apex:inputField value="{!pCase.description}" style="width:538px;height:92px;margin-top:4px;" />
                <div class="custompubblock" style="margin-top:5px;"> 
                    <div>Status:&nbsp;</div><apex:inputField value="{!pCase.status}" />&nbsp;&nbsp;&nbsp; 
                    <div>Priority:&nbsp;</div><apex:inputField value="{!pCase.Product_Group__c}" />&nbsp;&nbsp;&nbsp; 
                    <div>Case Origin:&nbsp;</div><apex:inputField value="{!pCase.origin}" /> 
                </div> 
            </apex:outputPanel>
        </apex:form><br/>
        <button type="button" onclick="createCase();" 
        style="position:fixed;bottom:0px;right:0px;padding:5px 10px; 
        font-size:13px; font-weight:bold; line-height: 
        18px;background-color:#0271BF;background-image:-moz-linear-gradient(#2DADDC, #0271BF);background-repeat:repeat-x;border-color:#096EB3;" 
        id="addcasebutton">Create Case</button> 
    </div>  
</apex:page>