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 

Save on Custom Object redirecting to Home tab

Right now I have a button on the Cases layout called Change Management.  When you click the button, it creates a Change record and a junction object, all works fine.  Problem we have is when you click Save after all that, would like it to behaving like the Save button on Cases where after you create a case, click Save, it goes back to the Case you just created.  Right now, when you click Save on the Change record, it returns to the Home tab.  Here's my code below, how can I get it to return to the Change record that gets created?

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, Dealer__c, 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.Contact_Approver2__c  = [Select id From User Where ContactId = :tmpId LIMIT 1].Id;
        }
        newChange.Dealer_Account4__c    = currCase.Dealer__c;
        newChange.ProductChange__c      = currCase.Product__c;
        newChange.Support_Advocate1__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');
        return retPage;
    
    }

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella

 

You need to pass retUrl parameter. Replace the following line

 

PageReference retPage = new PageReference('/' + newChange.id + '/e');

 

with

 

PageReference retPage = new PageReference('/' + newChange.id + '/e' + '?retURL=' + newChange.id);

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

 

All Answers

Kiran  KurellaKiran Kurella

 

You need to pass retUrl parameter. Replace the following line

 

PageReference retPage = new PageReference('/' + newChange.id + '/e');

 

with

 

PageReference retPage = new PageReference('/' + newChange.id + '/e' + '?retURL=' + newChange.id);

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

 

This was selected as the best answer
Puja_mfsiPuja_mfsi

HI,

use this 

PageReference retPage = new PageReference('/' + currCase.id);
return retPage;