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 

custom close button

I have a custom close button for our custom object Change.  You click the Close Change button and it opens an edit screen of the closed page layout.  If a user clicks Cancel instead of Save, the changes still take place.  How do I prevent the changes from happening if the users changes their mind and clicks Cancel?

 

Code:

public class CloseChangeButtonController {

    Change__c currChange;

    public CloseChangeButtonController(ApexPages.StandardController controller) {
        currChange = [Select Auto_Approved__c, Contact_Approved_Date_Time__c, Contact_Accepted__c, Change_Owner__c, Category__c, 
        Change_Manager_Rejected_Date_Time__c, Change_Manager_Rejected_Checkbox__c, 
        Change_Manager_Approved_Date_Time__c, Change_Manager_Accepted__c, Change_Implementer__c, Change_Manager__c, 
        Account_Lookup__c, Backout_Plan1__c, Implementation_End_Date_Time__c, Contact_Approver2__c, Dealer_Account4__c, 
        ProductChange__c, UrgencyChange__c, TypeChange__c, Implementation_Plan1__c, Test_Plan1__c, Target_Date_Time__c, 
        Subsystem__c, Subject__c, Implementation_Start_Date_Time__c, Description__c, Dealer_Rejected_Date_Time__c,  
        Dealer_Approver_Rejected_Comments__c, Dealer_Accepted__c, Dealer_Rejected_Checkbox__c, 
        Dealer_Approved_Date_Time__c, Contact_Rejected_Date_Time__c, Contact_Approver_Rejected_Comments__c, Contact_Phone__c, 
        Contact_Rejected_Checkbox__c, Contact_Mobile__c, Contact_Email__c, Support_Advocate1__c From Change__c Where Id = :controller.getId()];
    }
    
    public PageReference CloseChange(){
    
        if((currChange.Auto_Approved__c) || 
        (((currChange.Change_Manager_Accepted__c) || (currChange.Change_Manager_Rejected_Checkbox__c)) && 
        ((currChange.Contact_Accepted__c) || (currChange.Contact_Rejected_Checkbox__c))&&
        ((currChange.Dealer_Accepted__c) || (currChange.Dealer_Rejected_Checkbox__c))))
        {
        currChange.StatusChange__c = 'Completed';       
        currChange.RecordTypeId = '012f00000008WBb';
        // Insert the change record 
        Database.update(currChange);
        PageReference retPage = new PageReference('/' + currChange.id + '/e');
        return retPage;
             
        }
        else{
      PageReference retPage = new PageReference('/' + currChange.id);
        return retPage;
         }
             
      
    }
    
    

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma

I understand now what you are exactly trying to dod and I would say, do not update the record in closeChange method. Instead  of that what you can do is, you can autopopulate "Completed" in Status using URL parameter.

 

Go to you Change object and select the status field. From URL, you will find field id(.com/fieldId?set...)

 

From your CloseChange method, pass this in URL like

PageReference retPage = new PageReference('/' + currChange.id + '/e?fieldId=Completed');

All Answers

dphilldphill
Does your Cancel button call the same method as the save?
StaciStaci

The Cancel button is the Standard Cancel button in SF. 

dphilldphill
So.... I read your question like 5 times... and can't seem to figure out how that could be happening or why. Sorry.
MohammadMohammad

You might want to override the "cancel()" method of standard controller and create the same in your extension class to do your desired action..

Bhawani SharmaBhawani Sharma
Your CloseChange method is actually opening an edit screen, but before that, it is saving data in database.
Database.update(currChange);
PageReference retPage = new PageReference('/' + currChange.id + '/e');

You should perform save action only when required.
StaciStaci

How do I pull out the save and cancel action?

StaciStaci

How would I override the cancel() method?

MohammadMohammad

2 things.

 

1. as mentioned by Bhawani, why are you returning the edit page on close action? Just return '/id'.

 

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

I am not sure why are you using if and else and returning edit page.

 

2. if you are using puuting button name as "cancel" and action as "cancel", to override you need to create a method named "cancel()" and return the details page without making any DML or anything.

 

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

 

hope this helps.

StaciStaci

Here's what I'm trying to accomplish and I guess I'm not doing it right, I've only just learned Apex a month ago. 

 

We have a change record that we want to close, just like closing a case.   They click the Close Change button and I want it to go to the edit screen of the closed change layout, we have several fields that are needed to be required on closing which is why we are doing it this way. 

 

I want them to fill in the required fields on that edit screen and then click Save and have the changes made.  BUT in clicking the Close Change button I also want the Status to default to Complete.  BUT if they click cancel, I want the Status to go back to what it was and go back to the open change layout and have no changes made.

 

How would I accomplish this the right way?

Bhawani SharmaBhawani Sharma

I understand now what you are exactly trying to dod and I would say, do not update the record in closeChange method. Instead  of that what you can do is, you can autopopulate "Completed" in Status using URL parameter.

 

Go to you Change object and select the status field. From URL, you will find field id(.com/fieldId?set...)

 

From your CloseChange method, pass this in URL like

PageReference retPage = new PageReference('/' + currChange.id + '/e?fieldId=Completed');
This was selected as the best answer
StaciStaci

Thank you Tech Force!!  One little addition though.  How can I change record types within the page reference?

StaciStaci

Nevermind, I figured it out!

PageReference retPage = new PageReference('/' + currChange.id + '/e?RecordType=012f00000008WBb&00Nf0000000KRSP=Completed');

 

Thank you so much!