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
AussieBattlerAussieBattler 

Redirect from a Trigger

Hi,

I want to redirect a user to a custom object if certain conditions are met.

So to that end, I have a trigger that is set off after a record is inserted or updated and this trigger calls a function in a class that handles the redirection. Everything works, all the code in the trigger and class is executing as I would expect, but instead of redirecting the user to the new screen it returns them to the newly saved record (as it would normally behave).

Any ideas where I am going wrong?

Trigger Code:
Code:
trigger testTrigger on Object__c (after insert,after update) {

Object__c[] obj = trigger.new;
if (obj[0].Picklist__c == 'Change') {
String objId = obj[0].uniqueID__c;
redirectClass.redirectFn(objId); 
}
}

Class Code:
Code:
public class redirectClass{

public static PageReference redirectFn(string uID) {
        Test__c sID = [SELECT Id FROM Test__c WHERE uID__c =:uID LIMIT 1];
 string currURL = '/' + sID.Id;
 PageReference redirect2 = new PageReference(currURL);
 redirect2.setRedirect(true);
 return redirect2;    
 }
}

 

 

Ron HessRon Hess
triggers are not connected to the browser in any way at all, they may fire when an API call is run for example.

it's true they can block a save by using addError(), but the trigger is not controlling how the page will handle that, the browser is.


if you called that code from a Visualforce page (controller) it would work.
AkiTAkiT
I've seen redirect happen after trigger in some Appexchange app.

Some combo of apex trigger and s-control - Any clue how it is possible to do?