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
Sapn ThakurSapn Thakur 

Set Redirect

Hi All,

 

We are having one custom object called 'XXX'. We are creating new XXX object record depending on certain conditions when we update opportunity using trigger.

 

When we create custom object record on updating of Opportunity, we want to redirect the page to the new custom object record not to the Opportunity.

 

Please let me know how we can do this.

 

Thanks,

Sapna

MohandaasMohandaas

Sapna,

 

If I am right you are creating the custom object 'xxx' record through a trigger which will happen on Save.

 

To redirect to the detail page of 'xxx' record you have to write an controller extension for opportunity where you can perform both creating xxx record and redirect to custom object detail page.

 

xxx record = new xxx();

insert record;

pagereference detail = new pagereference('/'+record.id);

return detail;

kiranmutturukiranmutturu

its not possible to return or using the page reference in the trigger instance....

zachelrathzachelrath

Sapn, 

 

You cannot achieve the page-redirection functionality you're looking for through Apex triggers, because triggers have no control over the User Interface. The best way to achieve what you are looking for is by having your Opportunity Edit page be a Visualforce page and overriding the Save action. You can leave your business logic in the trigger code if you want it to be enforced on ALL Opportunity updates, but if you only want it to be enforced on Opportunity updates that occur through the UI, then it probably makes more sense to remove it from the Trigger.

 

FIrst, you will need to create a controller extension for Opportunity (as Cloud Nine has said), with a page action method to call instead of the standard controller's save() method. We will just call this save(). Your custom logic---which will create a new XXX record if certain conditions on the Opportunity record are met---could go here if you ONLY want it to be enforced on Opportunity updates that occur through the UI.

 

 

 

public with sharing class EditOpportunityControllerExtension {

   private final ApexPages.StandardController ctl;

   // Constructor
   public EditOpportunityControllerExtension(ApexPages.StandardController stdController) {
      // Save the standard controller so that we can use its methods later
      ctl = stdController;
   }
	
   // Attempts to save the Opportunity.
   // If a related XXX object was created as well,
   //	we send the User to the XXX object's detail page.
   // Otherwise, just send the User to the Opportunity's Detail page
   public PageReference save() {
		
      PageReference opportunityDetailPage;
      try {
         opportunityDetailPage = ctl.save();
      } catch (Exception ex) {
         // If we caught exceptions, add them to the current page
	 ApexPages.addMessages(ex);
      }
		
      // If our Opportunity was successfully saved,
      //   we will have a non-null PageReference
      if (opportunityDetailPage != null) {
         // Get the actual record for our newly updated / created Opportunity
         Opportunity opp = (Opportunity) ctl.getRecord();
			
         // See if the XXX object has been created
         XXX__c relatedObject; 
         try {
	    relatedObject = [SELECT Id FROM XXX__c WHERE Related_Opportunity__c = :opp.Id LIMIT 1];
	 } catch (Exception ex) {}
			
	 if (relatedObject != null) {	
	    // We found a valid related XXX record,
	    //   so, send the User to the Detail page for this XXX__c record
	    return (new ApexPages.StandardController(relatedObject)).view();
	 } else {
	    // Apparently, no related XXX__c record exists,
	    //	so just send the User to the Opportunity Detail page
	    return opportunityDetailPage;
	 }
      } else {
	 return null;
      }
   } // end save()
} // end class

 

 

You will then need to override the Opportunity object's Edit action with something like the following page:

 

 

 

<apex:page standardController="Opportunity" extensions="EditOpportunityControllerExtension">

   <apex:pageBlock title="Edit Opportunity">
	
      <apex:pageBlockButtons location="top">
         <apex:commandButton action="{!cancel}" value="Cancel"/>
         <apex:commandButton action="{!save}" value="Save"/>
      </apex:pageBlockButtons>
	
   <!--  REST OF PAGE -->
	
   </apex:pageBlock>

</apex:page>

 

 

Sapn ThakurSapn Thakur

Hey thanks all for the reply.

 

I don't want to create visualforce page for the edit opportunity and override the save action. I wanted this to be achieved using apex trigger only but that is not possible.. :(