+ Start a Discussion

Navigate to new page using apex.


I'm trying to make my apex code navigate automatically to a given account page (after some processing, the method comes up with the account id). My apex code is invoked by an update trigger. Is this possible?

I don't know of a way to get the update trigger to talk to the browser directly, but the user is probably clicking a button or something to get to the edit form from which he or she invokes your update trigger, so you can override that and set "retURL=" in the query string. 

After the trigger finishes, the person has to go somewhere where you can read back whatever work you did in the trigger, so point them to /apex/mypage?id=<record being edited> and then, in your trigger, make sure to write the Account's Id to a hidden field on <record being edited>.  By the time mypage loads the database transaction is all done, so you read back and just redirect immediately.  The Visualforce page 'mypage' can be done with just a <apex:page/> tag -- I'll send an example if it would help.

(Edit/PS:  If there's high contention for <record being edited> or if the hidden field would leak sensitive information, you can have your update do an insert of a new object of some custom type that has a lookup to <record being edited>.  Set the owner of the new object as the LastModifiedBy user on <record being edited>.  In the VF page, before you redirect, delete the custom object.)

Message Edited by MikeGoelzer on 10-02-2008 05:51 PM
Thanks a lot for your advice Mike. I found a different way to make this work, but your approach seems more efficient.

Here's the whole picture:
I'm cloning and object and adding some information to the clone. To achieve this, I use a custom button and the ajax toolkit to force an update on a control field in the record to clone. This update fires a trigger which passes the object to an apex class. This class then uses the clone method and adds information to the new record, including the parent record ID in a hidden field (similar to what you suggest). Until this step everything was simple and easy to implement. But next I needed apex to navigate to the new record.

The solution I came up with is this: The same button that launches it all, includes a final script which requests a search for the clone. It actually looks for the parent record ID (which a have available on my script) in the object's hidden field. This returns the clone's id, and then it's easy to point the browser to that record.
Of course, for this to work the clone must be created and added to the database before my script tries searching for it. On my tests it always finds it, but I'm not sure sfdc transactions will always be fast enough.

As I mentioned, I find your approach more efficient and more reliable than mine. So I will definitely try to implement it.

Thanks again.
David VPDavid VP
Another way you could probably tackle this is to point your button to a VisualForce page with :

<apex:page .... action='{!yourclonemethod}'>

If you let your 'clonemethod' do whatever it needs to and then return a PageReference to the page you want to get to the VF page will redirect and you won't have to worry about timing issues etc ...


Hi David,

Using Visualforce to invoke the method which clones and adds data to my object, leads me to the situation I had when invoking the same method from the ajax toolkit: I need to "manually" load the object to clone by running a select query specifying each and all of its fields, which is rather cumbersome. On the other hand, when the method is invoked by a trigger, it passes the object already loaded with all it's fields. Then I just have to apply the .clone method to it.
David VPDavid VP
I see what you mean.

I still wouldn't risk running into the timing issues.
Building a query that selects all the fields in an object is really easy in the Force.com IDE : You just tick the checkbox next to the 'Fields' in your schema browser and it will generate the entire thing for you.

of course : It's your call :smileytongue: