+ Start a Discussion

Redirect on Save from Task to New Opportunity page

I wrote the code below hoping I can redirect once a Task is saved based on a checkbox.  If checked, user is redirected to the a new Opportunity in edit mode screen.  Not checked, just save the task.  I still have to put in the code to update the check to uncheck.  I can not get the page to redirect.  What am I missing?




trigger CreateNewOpty on Task (after insert) {

for(Task ta: Trigger.new){


NewOptyRedirect obj = new NewOptyRedirect();



Apex Code:


Public Class NewOptyRedirect {

public PageReference Redirect(string TaskId) {

String str='';
List<Task> taskList = [Select AccountId, Business_Segment__c, Description, Information_Discussed__c, Purpose_of_Call__c, Region__c, Test_Products__c From Task where id=: TaskId];
for(task t : taskList){
str='/006/e?retURL=%2F006%2Fo&accid='+replaceStr(t.AccountId) +'&opp3=CALL LOG OPPTY&opp5='+replaceStr(t.Purpose_of_Call__c)+'&00NG0000008u5yH='+replaceStr(t.Information_Discussed__c)+'&00NG0000008u5xd='+replaceStr(t.Test_Products__c )+'&0NG0000008vezX_lkold='+replaceStr(t.Information_Discussed__c)+'&00NG0000008vezX=Info Discussed: '+replaceStr(t.Information_Discussed__c)+' Comments: '+ replacestr(t.Description)+'&00NZ0000000cv0r='+ replacestr(t.Region__c)+replacestr(t.Business_Segment__c)+'&00NZ0000000cv0m='+replacestr(t.Business_Segment__c);

system.debug('here in the apex code-------------'+str);
//PageReference pageRef = new PageReference('http://www.google.com').view();
PageReference r= new PageReference('/apex/RedirectOpportunity');
return r;


public String replaceStr(String s){
if(s==null) s='';
return s.replace('&', '%26');





If I understand this right, you are trying to have an after update trigger cause a page redirect. If yes, this won't work.  VF controllers (or controller extensions) can do redirects, not triggers.  Think about it, triggers get invoked whenever a record gets saved and records can get saved via data loader, workflow updates, approval process updates, API calls -- none of which are connected to the user or any page.


One approach -- You'll need to create a VF page that intercepts the Save button; does the Task update, and then redirects. The VF page will replace the standard New/Edit page for Task.  Note that Tasks can be performed on many objects (Accounts, Opportunities, custom objects, ....) so your redirect logic may need to be aware of the Task's parent SObject  


Some relevant starter posts




and for overriding - http://advologixpm.screenstepslive.com/s/1777/m/AdvologixPM_Setup_Guide/l/27747-configure-standard-objects-event-task-button-overrides