You need to sign in to do that
Don't have an account?
hemm
Obtaining a VF page from a Page Layout without popping up a new window
I have created a VisualForce page that generates a file. The contenttype parameter has a mime type for the file. When I run the VF page on its own, it works great. The file open/save as dialog comes up.
Now I want to be able to click a button on a Page Layout (not a VF page, but a Page Layout), run the VF page that generates the file and have the user prompted to open/save the file. And I'd like the user to stay on the same record they clicked the button from. I also don't want any windows popping up when teh button is clicked. I want the user experience to be as if they are downloading a file; only it's a VF page generating a file on the fly. I tried this using onClick JavaScript using 2 approaches. Neither is working. I could use some help on this or some other suggestions. The worst case is that I use a button that opens a small window and hopefully add JavaScript to the page that will automatically close it (can i do that?)
Approach 1: AJAX toolkit's remoteFunction method. When I run this, a big debug window pops up and says the page is being redirected to some odd URL.
Approach 2: Making a JavaScript XMLHttpRequest. This requires me to add a Remote Site for the force.com VisualForce site. Also, I get a 302 error due to that same redirect.
A completely different approach (Approach 3) that I was thinking could work would be to embed a new VisualForce page into my Page Layout. That VF page would only have a button on it. I am hoping that that page could make the call to the main VF page using the PageReference object. I can't seem to get it to work. I went through so many iterations of code I can't post a good example on this one.
Message Edited by hemm on 11-15-2008 11:30 PM
Now I want to be able to click a button on a Page Layout (not a VF page, but a Page Layout), run the VF page that generates the file and have the user prompted to open/save the file. And I'd like the user to stay on the same record they clicked the button from. I also don't want any windows popping up when teh button is clicked. I want the user experience to be as if they are downloading a file; only it's a VF page generating a file on the fly. I tried this using onClick JavaScript using 2 approaches. Neither is working. I could use some help on this or some other suggestions. The worst case is that I use a button that opens a small window and hopefully add JavaScript to the page that will automatically close it (can i do that?)
Approach 1: AJAX toolkit's remoteFunction method. When I run this, a big debug window pops up and says the page is being redirected to some odd URL.
Code:
{!requireScript("/soap/ajax/14.0/connection.js")}
sforce.connection.sessionId="{!$Api.Session_ID}";
sforce.connection.remoteFunction({
url : "https://namespace.na4.visual.force.com/apex/vfpage?id={!Lead.Id}",
async: true,
onSuccess : function(response) { }
});
Code:
xmlhttp=null; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp!=null) { xmlhttp.onreadystatechange=state_Change; xmlhttp.open("GET","/apex/vfpage?id={!Lead.Id}",true); xmlhttp.send(null); } else { alert("Your browser does not support XMLHTTP."); } function state_Change() { if (xmlhttp.readyState==4) { if (xmlhttp.status==200) { // Got it! } else { alert("Problem retrieving vCard data: " + xmlhttp.status); } } }
A completely different approach (Approach 3) that I was thinking could work would be to embed a new VisualForce page into my Page Layout. That VF page would only have a button on it. I am hoping that that page could make the call to the main VF page using the PageReference object. I can't seem to get it to work. I went through so many iterations of code I can't post a good example on this one.
Message Edited by hemm on 11-15-2008 11:30 PM
(BTW - this could be a lot simpler, inlining the functions. Also, you might consider modifying it to look for the form named "exportAction" since once the form is submitted it'll just complain about duplicate tags, etc...)
Add the button to your page layout and save it.
How it works
The script gets called when you press the button. Since Salesforce won't allow us use getElementById() for an on-click JavaScript, we'll use a more rudimentary thing to grab any available tag - in our case a "<td>" tag. You could also grab div's or spans, or whatever, but it doesn't really matter... Once you've got a tag, the script just appends the form tag contents. This contains your page and a way to parse the ID that you're passing in. Finally, the script calls the form that if just embedded with the ID as a parameter.
Once the form calls your page, your VF page executes. If the response truly doesn't have any HTML/TEXT/XML data, and contains only a content type of "application/octet", you'll end up remaining on the same page and triggering the download dialogue. If "developer mode" is on, the response object will contain HTML data and that will trigger the browser to display that stuff and redirect you off of your calling page.
I'm curious how this'll work out for you - could you post your approach and if this worked for you back here?
Message Edited by Less on 11-17-2008 01:05 AM
Also, if I add &core.apexpages.devmode.url=1 to the end of the URL, it doesn't matter whether or not someone is in Development Mode. I found that hack on the forums and it seems to turn off development mode on a specific page. It's unsupported, but the hack was posted by someone at salesforce, so I am using it.
Below is how I modified your code that you posted. I highlighted the spots where I modified your code.
Glad to help out and glad that it worked for you and glad to learn that you can force developer mode off!
BTW, you can catch the second click of the button using: