+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

Return to original page

I have created a button to call apex class as suggested in https://help.salesforce.com/articleView?id=000006031&type=1. However, the code does not return to original page.  What am I missing? 

Here is the VF Page 
<apex:page standardController="Contact_Preference__c" extensions="myContactPreferenceControllerExtension" action="{!autoRun}">
   
    <apex:sectionHeader title="Auto-Running Apex Code"/>
    <apex:outputPanel >
        You tried calling Apex Code from a button.  If you see this page, something went wrong.  You should have 
        been redirected back to the record you clicked the button from.
       
    </apex:outputPanel>
</apex:page>

Here is the controller
public class myContactPreferenceControllerExtension {

    private final Contact_Preference__c cp;
    
    public myContactPreferenceControllerExtension(ApexPages.StandardController stdController) {
        this.cp = (Contact_Preference__c)stdController.getRecord();
    }

    public String getGreeting() {
        return 'Hello ' + cp.name + ' (' + cp.id + ')';
    }
    
 
    
    // Code we will invoke on page load.
    public PageReference autoRun() {
 
        String theId = ApexPages.currentPage().getParameters().get('id');
 
        if (theId == null) {
            // Display the Visualforce page's content if no Id is passed over
            return null;
        }
 
        for (Contact_Preference__c o:[select id, name,first_name__c,Email__c from Contact_Preference__c where id =:theId]) {
            // Do all the dirty work we need the code to do
            // 
            o.First_Name__c='FirstName';
            o.Last_Name__c='LastName';
           // o.Email__c='someone@somewhere.com';
          
            Blob targetBlob = Blob.valueOf(o.Email__c);
			Blob hash = Crypto.generateDigest('MD5', targetBlob);
            o.Email__c=EncodingUtil.convertToHex(hash)+'@somewhere.com';

           update o;
        }
 
        // Redirect the user back to the original page
        PageReference pageRef = new PageReference('/' + theId);
        pageRef.setRedirect(true);
        return pageRef;
 
    }
 
}

 
Raj VakatiRaj Vakati
// Redirect the user back to the original page
        PageReference pageRef = new PageReference('/apex/<YOUR_PAGENAME>?id=' + theId);
        pageRef.setRedirect(true);
        return pageRef;

 
Raj VakatiRaj Vakati
public class myContactPreferenceControllerExtension {

    private final Contact_Preference__c cp;
    
    public myContactPreferenceControllerExtension(ApexPages.StandardController stdController) {
        this.cp = (Contact_Preference__c)stdController.getRecord();
    }

    public String getGreeting() {
        return 'Hello ' + cp.name + ' (' + cp.id + ')';
    }
    
 
    
    // Code we will invoke on page load.
    public PageReference autoRun() {
 
        String theId = ApexPages.currentPage().getParameters().get('id');
 
        if (theId == null) {
            // Display the Visualforce page's content if no Id is passed over
            return null;
        }
 
        for (Contact_Preference__c o:[select id, name,first_name__c,Email__c from Contact_Preference__c where id =:theId]) {
            // Do all the dirty work we need the code to do
            // 
            o.First_Name__c='FirstName';
            o.Last_Name__c='LastName';
           // o.Email__c='someone@somewhere.com';
          
            Blob targetBlob = Blob.valueOf(o.Email__c);
			Blob hash = Crypto.generateDigest('MD5', targetBlob);
            o.Email__c=EncodingUtil.convertToHex(hash)+'@somewhere.com';

           update o;
        }
 
        // Redirect the user back to the original page
        PageReference pageRef = new PageReference('/apex/<YOUR_PAGENAME>?id=' + theId);
        pageRef.setRedirect(true);
        return pageRef;
 
    }
 
}

 
Yogesh BiyaniYogesh Biyani
@RajV,

Thanks for the reply. 

I realized that the code is loading the page correctly but the issue was it opening a new window instead. After updating the Button behavior to 'Display' in the existing window this issue was resolved.

Yogesh
 
Raj VakatiRaj Vakati
Cool