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
mallikammallikam 

redirecting to a URL through triggers

How can we  redirect a trigger to a standard salesforce URL? Like when the user updates a cutom field in Opportunity, I want him to go to the new Partner reated list page directly when he hits save. Usually you can only see the new partner page only when you click New in Partner subsection from the opportunity details page. But I want to mandate the partner related list page when user selects YES for a custom field in Opportunity page.

Best Answer chosen by Admin (Salesforce Developers) 
ThomasTTThomasTT

Very good point. saveURL, cancelURL, retURL are kinds of SFDC backyard parameters... it's very reasonable to hesitate to use them. However, most of all things discussed here are something about workarounds and they usually need a tricky way... (doesn't have to be, but... you know, nobody is perfect).

 

saveURL, cancelURL, retURL are URL parameter and URLs which SFDC redicrets to after saving / canceling / saving or any action?. So open Opportunity edit page, and add this parameter "saveURL=/001/o" (of cource, with &) and hit enterkey (/001/o is Account tab page). You'll see the same edit page, but the save button behavior is already different.  then, click save button. what happend? same as cancelURL and clicking cancel.

 

In this case, you need to specify cancelURL, too, otherwise, cancel button goes back to the VF Page B, and redirect to the edit page again.

 

There may be no manual... or maybe you can find it in manual for URLFOR.

 

Here is working sample. You know how to override Opportunity Edit event with VF Page "B", right?

 

 

 [VF Page "A"]

 

<apex:page controller="AController" action="{!redirec}">
</apex:page>

 

 [Controller for VF Page A]

 

public class AController {

public PageReference redirec() {
// It seems that Edit event provide the working Opportunity id with newid
id oppId = ApexPages.currentPage().getParameters().get('newid');
// If no parameter, stay there (for debug)
if(oppId == null) return null;

// If Opportunity is not found (no way...), try to go to the detail page
// YOU NEED TO PUT SOME LOGIC TO DETERMINE WHERE TO REDIRECT TO
PageReference pageDetail = new PageReference ('/'+oppId);
pageDetail.setRedirect(true);
Opportunity[] oppList = [select id, Name, AccountId from Opportunity where id = :oppId];
if(oppList.size() == 0) return pageDetail;

// Go to the account page
PageReference pageAccount = new PageReference('/'+oppList[0].AccountId);
pageAccount.setRedirect(true);
return pageAccount;
}
}

 

 [VF Page "B]

 

<apex:page standardController="Opportunity">
<script>
window.top.location.replace("{!URLFOR($Action.Opportunity.Edit, opportunity.id, [saveURL='/apex/A', retURL='/'+opportunity.id, cancelURL='/'+opportunity.id], true)}");
</script>
</apex:page>

 

 

 

 

 

Message Edited by ThomasTT on 09-24-2009 01:46 PM
Message Edited by ThomasTT on 09-24-2009 01:48 PM
Message Edited by ThomasTT on 09-24-2009 01:50 PM
Message Edited by ThomasTT on 09-24-2009 02:34 PM

All Answers

ThomasTTThomasTT

It was good of you to mention what you really wanted to do.

 

I don't think trigger is for what you want to do (imagine that you load data from dataloader... it will also hit the trigger).

 

Here is what I can think of:

 

1) Create a VF page "A", which

   a) examines if user should go to new Partner page or not based on updated Opportunity data (need a good idea for this)

   b) redirects to  new Partner page if user should go (if not, redirects to the Opportunity Detail page)

 

2) Create a VF page "B", which add a parameter "saveURL=/apex/A" and redirect to original Edit event

3) Override Edit event  with VF page"B".

 

 

So, when user click "edit" on Opportunity, VF page B add saveURL parameter and goes to the original Opportunity edit page, and user click "save", then VF Page A is called and redirects to Partner or Opportunity detail.

 

In VF page A, you need to find out the custom field is updated or not. If user always should go Partner, that's fine.

 

ThoamsTT

mallikammallikam

Thanks thomas for the idea!! I thought of VF pages too initially but I thought that I have to copy all the fields and etc

from opportunity page, if I go that way, which I thought is not very ideal. But, I think your idea is different. I want to try it. The only thing that I am trying to find now is the usage of saveURL...I just cannot find some kind of documentation or usage for saveURL anywhere..can you give an example for using saveURL? Btw, I did look into discussion boards, I did not get the idea by just looking at th pure code replies..

 

thanks!

ThomasTTThomasTT

Very good point. saveURL, cancelURL, retURL are kinds of SFDC backyard parameters... it's very reasonable to hesitate to use them. However, most of all things discussed here are something about workarounds and they usually need a tricky way... (doesn't have to be, but... you know, nobody is perfect).

 

saveURL, cancelURL, retURL are URL parameter and URLs which SFDC redicrets to after saving / canceling / saving or any action?. So open Opportunity edit page, and add this parameter "saveURL=/001/o" (of cource, with &) and hit enterkey (/001/o is Account tab page). You'll see the same edit page, but the save button behavior is already different.  then, click save button. what happend? same as cancelURL and clicking cancel.

 

In this case, you need to specify cancelURL, too, otherwise, cancel button goes back to the VF Page B, and redirect to the edit page again.

 

There may be no manual... or maybe you can find it in manual for URLFOR.

 

Here is working sample. You know how to override Opportunity Edit event with VF Page "B", right?

 

 

 [VF Page "A"]

 

<apex:page controller="AController" action="{!redirec}">
</apex:page>

 

 [Controller for VF Page A]

 

public class AController {

public PageReference redirec() {
// It seems that Edit event provide the working Opportunity id with newid
id oppId = ApexPages.currentPage().getParameters().get('newid');
// If no parameter, stay there (for debug)
if(oppId == null) return null;

// If Opportunity is not found (no way...), try to go to the detail page
// YOU NEED TO PUT SOME LOGIC TO DETERMINE WHERE TO REDIRECT TO
PageReference pageDetail = new PageReference ('/'+oppId);
pageDetail.setRedirect(true);
Opportunity[] oppList = [select id, Name, AccountId from Opportunity where id = :oppId];
if(oppList.size() == 0) return pageDetail;

// Go to the account page
PageReference pageAccount = new PageReference('/'+oppList[0].AccountId);
pageAccount.setRedirect(true);
return pageAccount;
}
}

 

 [VF Page "B]

 

<apex:page standardController="Opportunity">
<script>
window.top.location.replace("{!URLFOR($Action.Opportunity.Edit, opportunity.id, [saveURL='/apex/A', retURL='/'+opportunity.id, cancelURL='/'+opportunity.id], true)}");
</script>
</apex:page>

 

 

 

 

 

Message Edited by ThomasTT on 09-24-2009 01:46 PM
Message Edited by ThomasTT on 09-24-2009 01:48 PM
Message Edited by ThomasTT on 09-24-2009 01:50 PM
Message Edited by ThomasTT on 09-24-2009 02:34 PM
This was selected as the best answer
mallikammallikam
Thanks a lot Thomas for the great explanation and example code. I tried this and it works great. I realy appreciate your time. This is exactly what I am trying to achieve.
ThomasTTThomasTT

I'm glad that my 15 min was worth that much :)

 

ThomasTT

AkiTAkiT

This is pretty nifty way to create wizard type of behavior without needing to override & build whole page layout with VF. :smileyhappy:

 

Sadly enough it's not standard way and not supported for future releases, as far as what one hears from Salesforce support about modding the url params. 

ThomasTTThomasTT

... you scare me... but you are definitely right. Please use my code carefully and responsibly.

 

If SFDC changes the spec - even though it is completely legal and reasonable, a million people will keep crying a year... or not.

 

I would say, why they don't formalize the spec and improve it in public. It is one of nice ways to controll pages (not the best, but... possible and working).

 

ThomasTT

mallikammallikam

But, salesforce always says that all its future releases will support the old versions?? And basically, I did not understand whats wrong with this approach and what is it that they would disable or wont support in future regarding this?

 

thanks!

AkiTAkiT

I've seen examples of using retURL parameter in Salesforce.com official documentation, so I would pretty confidently use it in any customizations. About the others like saveURL I am not so sure and have never used, I would better try to confirm with SF.

 

I agree why not really freeze these to the specs and give confidence in using them as one can nicely do things with them. :manwink:

ThomasTTThomasTT

Yes. However, what mallikam wants to do is, if any change related to Partner is made, New Partner page is expected to be opend. If I were the deleloper/BA, I would want to automatically populate some field values from the Account. If so, I would have to use the URL parameter magic spell "(custom field id)=(field value)" and I don't think this is official function... (or is it?)

 

I even found a page about the magic spel

 

http://salesforce.phollaio.com/2007/04/02/how_to_obtain_a_field_id/

 

and I believe many people use this mechanism. I was scared when At said about SFDC's change of spec, because I remember this mechanism.

 

Well... this isn't the first problem in the framework world, but it's always good to be warned by someone else.

 

ThomasTT

 

 

AkiTAkiT
Right, I think this magic spell is quite common use case of the URL params. I am also using and remember it failing some time ago due to some changes & how script was interpreting empty (--none--) values. SF basically did not mention how to fix the script, but said to use triggers instead (of course not the same). Got it working pretty easily however and now running fine again, for the moment :smileywink:
ThomasTTThomasTT

I agree. It always is.

 

ThomasTT

mmrrmmrr

I got syntax error while trying to copy PageB, please help.

mmrrmmrr

Hi, I am getting syntax error while trying to copy PageB.  Please help...

emuelasemuelas

Hi ,

 

I have a  requirement: I need to Override 'Save' button on 'Event' to my VF page 'SendSMSToAttendees'.

 

Solution : I have created a VF page 'newEventpage' with the code as below, and override 'New' button of Event with the page.


<apex:page standardController="Event">
    <script>  
        window.top.location.replace("{!URLFOR($Action.Even​t.New, Event.id, [saveURL='/apex/SendSMSToAttendees', retURL='/apex/SendSMSToAttendees', cancelURL='/'+Event.id], true)}");
    </script>
</apex:page>

 

But I am not able to save the page. I am getting the error :


"Error: Field $Action.Event.New does not exist. Check spelling" .

 

I had a trial with Opportunities with the same logic and was working well.

 

 <apex:page standardController="Opportunity">
    <script>  
        window.top.location.replace("{!URLFOR($Action.Oppo​rtunity.New, opportunity.id, [saveURL='/apex/A', retURL='/apex/A', cancelURL='/'+opportunity.id], true)}");
    </script>
</apex:page>


I am not sure why it is not working for Event.

Can anyone help me in this matter?

 

Thanks
Ambily

emuelasemuelas

The issue is solved. i used NewEvent instead of New.

 

<apex:page standardController="Event">
    <script>  
        window.top.location.replace("{!URLFOR($Action.Even​t.NewEvent, Event.id, [saveURL='/apex/SendSMSToAttendees', retURL='/apex/SendSMSToAttendees', cancelURL='/'+Event.id], true)}");
    </script>
</apex:page> 

 

Thanks