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
StaciStaci 

create case, cancel still creates new case

I have a button that I need to create a case, but render the edit page of that new case, but if cancel is clicked its still creating the new case.  I can't quite figure out how to accomplish this altogether.  Please help!

Button:
/apex/CW_MAMtoIncident?caseID={!Case.Id}&retURL={!Case.Id}

Page: <apex:page standardController="Case" extensions="CW_MAMtoIncident" action="{!autorun}">
</apex:page>


Class:
public class CW_MAMtoIncident{
public Case caseID;
public Case newCase{get;set;}

    public CW_MAMtoIncident(ApexPages.StandardController controller) {
    }
   
     public pageReference autorun(){
       caseID = [SELECT Site_Code__c, Id FROM Case WHERE Id = :ApexPages.currentPage().getParameters().get('caseID')];
       Case newCase = new Case(Id = ApexPages.currentPage().getParameters().get('newid'));
       newCase.RecordTypeId = '01230000000uzis';
       newCase.ParentId = caseID.Id;
       if(caseID.Site_Code__c == 'CAR')
       {
           newCase.AccountId = 'xxxxxxxxxxxxxxxxxx';
       }else{
if(caseID.Site_code__c == '')
       {
           newCase.AccountId = '';
       }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
       //insert newCase;
       
       
        PageReference retPage = new PageReference('/' + newCase.Id + '/e' );
        return retPage;
       }
}
AshesAshes
Hi Staci,

You are calling Autorun method on page load. In Autorun you have written case creation login which is running on load of page. 
So while loading of the page itself your case is creating. 
StaciStaci
Hi Ashes, is there an easy way to remedy this?
StaciStaci
Ok, now I have the Cancel funtion working, but its not pulling the account.  Here's what I changed

Button:
/500/e?retURL={!Case.Id}&{!Case.RecordTypeId}=01230000000uzis&cas28_lkid={!Case.Id}&cas28={!Case.CaseNumber}&saveURL=/apex/CW_MAMtoIncident?caseID={!Case.Id}



Class:
public class CW_MAMtoIncident{
public Case caseID;


    public CW_MAMtoIncident(ApexPages.StandardController controller) {
    }
   
     public pageReference autorun(){
       caseID = [SELECT Site_Code__c, Id FROM Case WHERE Id = :ApexPages.currentPage().getParameters().get('caseID')];
       //Carlin
       //Case newCase = new Case();
       //newCase.RecordTypeId = '01230000000uzis';
       //newCase.ParentId = caseID.Id;
       Case newCase = new Case(Id = ApexPages.currentPage().getParameters().get('newid'));
       if(caseID.Site_Code__c == 'CAR')
       {
           newCase.AccountId = '001e000000AtHDo';
       }else{
if(caseID.Site_code__c == '')
       {
           newCase.AccountId = '';
       }}/*}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}*/
        update newCase;
       
       
        PageReference retPage = new PageReference('/' + newCase.Id + '/e');
        return retPage;
       }
}
StaciStaci
@Ashes, ok I take it back, the account is working but after Save, which I understand why.  How can I make the account render on the edit page before save???