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 record button, cancelled still creating record

We have a button on Cases called Create Change.  If I click it it gives me the edit page of the new record.  If I click Cancel, its stillcreating the record.  How do I get Cancel work like a normal Cancel button?

 

public class SampleCaseButtonController {
    //Create a variable to hold the case that is accessible from the rest of the class
    private Case currCase = new Case();

    public SampleCaseButtonController(ApexPages.StandardController controller) {
       
        List<Case> caseList = [Select AccountId, /*Contact.Phone, Contact.Email, Contact.MobilePhone, ContactId,*/ Product__c, Support_Advocate__c From Case c Where Id = :controller.getId() LIMIT 1];
        //If cases are returned from the query
        if(caseList.size() > 0){
           currCase = caseList[0];
        }
    }
    
    public PageReference changeCase(){
    
        // Create a new Change Record
        Change__c newChange = new Change__c();
          
        // Fill in the values for the new record
        newChange.Account_Lookup__c = currCase.AccountId;
        
        // We put the case contact id field into a string because in the SOQL query,
        // we cannot compare the User ContactId field to an ID field directly. Doing so will
        // throw an "Invalid assignment of Schema.Sobjectfield to Id" error.  Exact reason
        // unknown but likely related to the special construction of the Portal fields in the 
        // User object.
       
        //String tmpId = currCase.ContactId;
        //if(tmpId != null)
        //{
        //newChange.Contact14__c  = [Select id From User Where ContactId = :tmpId LIMIT 1].Id;
        //}
        //newChange.Dealer_Account1__c    = currCase.Dealer__c;
        newChange.ProductChange__c      = currCase.Product__c;
        //newChange.Support_Advocate_Change__c  = currCase.Support_Advocate__c;   
        //newChange.Change_Owner14__c = currCase.Support_Advocate__c;
        //newChange.Contact_Phone__c =  currCase.Contact.Phone;
        //newChange.Contact_Mobile__c = currCase.Contact.MobilePhone;
        //newChange.Contact_Email__c = currCase.Contact.Email;
              
        
        // Insert the change record 
        Database.insert(newChange);
        
          //create a new Case Change Association
        Case_Change_Association__c newCCA = new Case_Change_Association__c();
         // Fill in the values for the new record
         newCCA.Case__c = currCase.Id;
         newCCA.Change__c = newChange.id;
         
        //Insert the new Case Change Association record
        Database.insert (newCCA);
            
        PageReference retPage = new PageReference('/' + newChange.id + '/e' + '?retURL=' + newChange.id);
        return retPage;
    
              }  
}

 

<apex:page standardController="Case" extensions="SampleCaseButtonController" action="{!changeCase}">
   
   </apex:page>

 

 

Marko LamotMarko Lamot

Your function changecase() first creates the 

Case_Change_Association__c

and immedietaly after that it returns with edit mode to that record.

And changecase() is called immediataly when VF page opens;

 

So it is obvious why cancel "doesn't work",

 

->

 

Rework your code in a way that you will have Save/Cancel button for NEW/EDIT. And then call

action="{!changeCase}"

function on "Save" button.  

 

 

 

 

 

 

StaciStaci

Thanks MarkoLamot

 

I'm not sure I understand what you mean, I'm fairly new to this.  I assume this is what I need on the apex page:

<apex:CommandButton action="{!changeCase}" value="Save"/>

 but I'm not sure how to rework the code to have a Save/Cancel button for NEW/EDIT.  Can you give me a step in the right direction?