2

I want to redirect the user to a new page after a new record of a custom object CIP System is created from a Trigger. Is this possible?

Here is the code I was using:

trigger SaveRedirect on CIP_System__c (after insert) {
CIP_System__c[] newCip = Trigger.new;
PageReference pageRef = new PageReference('/' + 'newCip[0].Site_Survey__c');
pageRef.setRedirect(true);

return pageRef;



}

However, I keep getting the error Compile Error: Trigger return cannot specify a value

4 Answers 4

5

A trigger runs before or after a database operation to insert, update or delete a record. You're running an after insert trigger which means it executes after the record has been saved. It executes behind the scenes on the database, not in the View context or VisualForce display.

A trigger isn't intended to control the View or display. That's done through your VisualForce. You'll want to modify your VisualForce page to redirect following a save.

1

You shouldn't do this. Triggers are for indirect consequences of a database action (insert, delete etc).

If you want to check if the insert was successful then you can put a try/catch around the insert.

Remember that a trigger can be called with multiple objects (as your code shows). In this case you may have difficulty providing a meaningful redirect.

Triggers can also fire other triggers. What if another fired trigger also wanted to issue a redirect? Which one is correct?

0

Ugly hack if you need to redirect after saving from a std page

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008xYXIAY

0

This can be done with the help of an invisible lightning component on the record page.

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:handler name="change" value="{!v.simpleRecord}" action="{!c.onChange}"/>
    
    <aura:attribute name="record" type="Object" />
    <aura:attribute name="simpleRecord" type="Object" />
    <aura:attribute name="recordError" type="String" />
    
    <force:recordData aura:id="recordHandler"
                      fields="IsPrivate"
                      recordId="{!v.recordId}"
                      targetError="{!v.recordError}"
                      targetRecord="{!v.record}"
                      targetFields ="{!v.simpleRecord}"/>
    
</aura:component>

Controller

onChange : function(component, event, helper) {
        console.log('on change...');
        var urlEvent = $A.get("e.force:navigateToURL");    //Redirect from here
        urlEvent.setParams({
                "url": "/006/o"
        });
}

Go ahead and drop this component on the record page of any object. You will be redirected to the given URL in case of any change. Please note that we can add any conditional statement in the 'onChange' function of the controller.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .