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
CustomDataIntegrationsCustomDataIntegrations 

Hyperlink to User Saved Document

I am trying to create a hyperlink to a document saved to a User Folder.  The Document is an .html file that has been saved there.  The file contains some html like....

 

<html>
<body onLoad="javascript&colon; document.forms[0].submit();">
<form action="http://www.mysite/mypage.aspx" method="POST">
..... input tags with values....

 

If I can create this same functonality in another way, I would be open to that as well.  Basically I need to serve up this html that would submit the page right away.

bob_buzzardbob_buzzard

Is there a reason why you need to do this via a saved document?  I.e. could you do it via an apex:page action attribute?  

CustomDataIntegrationsCustomDataIntegrations

If I could do it via an apex:page action attribute, that would be great.  Can you give me an example of how I would go about that?

 

Thanks,

bob_buzzardbob_buzzard

Can  you give me a little more detail about what you are trying to achieve?  In your original post you are submitting the form back to an external page - is that the requirement or are you simply trying to submit some information back to Salesforce?

CustomDataIntegrationsCustomDataIntegrations

I am trying to submit some information back to an external page.  I would like to put a link on a page and have it serve up some .html that submits to an external page.  So, if that link directs to a different apex:page that handles loading the html and then automatically submits to an external page, that would be great.  The .html would consist of a few input components that have values assigned to them based on the user that is currently logged in.

bob_buzzardbob_buzzard

Cool.  In that case a page action wouldn't work unless you were happy with an HTTP GET request as opposed to a POST. An onload handler should.  You should just be able to embed the form into the Visualforce page and click it onload as you would for vanilla HTML:

 

Something like the following:

 

<apex:page showHeader="false" sidebar="false" standardstylesheets="false">
<html>
<body onLoad="document.forms[0].submit();">
<form action="http://www.mysite/mypage.aspx" method="POST">
 <input type="text" name="alias" value="{!$User.alias}"/>
</form>
</body>
</html>
</apex:page>

 

In this case your hyperlink (presumably from a record detail page or similar) can simply open the VF page and all should happen as you expect.

CustomDataIntegrationsCustomDataIntegrations

So this was working fine for me for some time, but now I am not sure why this doesn't work.  Now instead of submitting the html, it just opens up another instance of MainPage in a new window.  What am I doing wrong?

 

MainPage using controller "MainPageController"

<apex:commandLink action="{!RedirectToCreatePAF}" id="cmdCreatePAF" target="_blank">Create New PAF</apex:commandLink>

 

 

Here is the action (located in MainPageController) for the commandLink that is executed:

public PageReference RedirectToCreatePAF() {
        PageReference createpafpage = Page.CreateNewPAF;
        createpafpage.setRedirect(true);
        
        return createpafpage;
    }

 

 

Below is the page, CreateNewPAF, that I am calling.  It shares the same controller as the calling page.

 

<apex:page showHeader="false" sidebar="false" standardstylesheets="false" controller="MainPageController">
<html>
<body onLoad='javascript&colon; document.forms[0].submit();'>
<form action='https://www.mywebsite' method='POST'>
<input type='hidden' name='destinationpage' value='../summary.aspx'/>
<input type='hidden' name='useraction' value='transfer'/>
<input type='hidden' name='username' value='{!inpLoginValue }'/>
<input type='hidden' name='password' value='{!inpPasswordValue }'/>
<input type='hidden' name='subclientname' value=''/>
<input type='hidden' name='userdatabaseid' value='8d5e1c5f'/>
<input type='hidden' name='channelid' value='03fccd9b'/>
<input type='hidden' name='productid' value='b51e6d12'/>
</form>
</body>
</html>
</apex:page>

 

bob_buzzardbob_buzzard

I'm a bit lost here - where does the commandlink fit into this?

CustomDataIntegrationsCustomDataIntegrations

I editted my previous post to hopefully help you see what is going on.

bob_buzzardbob_buzzard

As you are using the setRedirect=true, I'd expect a new instance of the controller to be created for the target page, as that mandates a client side redirect.  Can you explain the expected behaviour to me?

CustomDataIntegrationsCustomDataIntegrations

I am using setRedirect=true.  I need to pull the current user (not the same as the salesforce user) from the controller into html before submitting the page onload.  So when I click a link, this link points to a different page and loads it in a new window.  This new page is using javascript to submit the page onload.

 

Should I be using setRedirect = false?  But wouldn't I lose any values in the controller with a new instance of the controller being created?

bob_buzzardbob_buzzard

Hmm.  That sounds about right.  So what happens when you click the button?  Does it open the expected page but the onload doesn't fire, or is it something else?

CustomDataIntegrationsCustomDataIntegrations

It actually opens up a new instance (or new page) of the MainPage that I was just on.

bob_buzzardbob_buzzard

The only thing that seems odd to me is the combination of an action method and a target attribute.  Target attributes are usually interpreted by the browser without hitting the controller, so it may be something related to that.

 

That said, as you are simply opening a new page, you don't need to hit an action method, you should simply be able to use:

 

<apex:outputLink value="{!$Page.CreateNewPAF}" id="cmdCreatePAF" target="_blank">Create New PAF</apex:outputLink>

  

and that will save you a round trip to the server.

CustomDataIntegrationsCustomDataIntegrations

This does work in opening up the new page, but it doesn't appear to be pulling in my values from the shared mainpagecontroller.

bob_buzzardbob_buzzard

It won't - this is a new window and thus has a new instance of the controller.

CustomDataIntegrationsCustomDataIntegrations

Hmm, this was working properly at one point.  ...Ok, so how do I get 2 properties from the first page controller to the second page controller.  I will create a second controller specific to the CreatePAFPage.

bob_buzzardbob_buzzard

I must admit I'm struggling to understand how this could have worked, given that each window maintains its own data via a separate viewstate/transaction etc.  Controllers can be retained across pages but that's where the same browser window is re-used.

 

You can pass parameters on the URL to the child window - would that do the trick?

bob_buzzardbob_buzzard

Although maybe I'm misunderstanding - you can use the same controller class to back both pages, but the actual controller instances will be specific to each browser window (essentially each rendered page).

CustomDataIntegrationsCustomDataIntegrations

So if I take away the target=blank, so that it just opens the new page in the same window... I now just get a new instance of the MainPage (calling page) in that window.   This is if I go with the commandlink route and make the trip back to the server and do the redirect from the server.  If I use your method with the outputlink, then it does open up that CreatePAFPage and redirect to the new page, but just does not pass my MainPageController page variables.

bob_buzzardbob_buzzard

I'm not sure what target=blank really means in conjunction with a form post - from the perspective of the browser its being asked to open a new window with the results of a request carried out by a different window, which doesn't really make sense to me.  Thus I'm not sure what behaviour to expect.

 

Without the target, the behaviour is correct - the controller instance is retained and a new page rendered in the same instance.

 

A link simply opens a new window with a URL to retrieve, so again that's behaving as expected.  This will receive a new instance of the controller.  

 

If you want to pass information to the newly opened window, you can either add them to the URL and store them in the controller or use directly on the page, or persist the information in an sobject and tell the new page the sobject id.