添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Welcome to Support!

Search for an answer or ask a question of the zone or Customer Support.

Need help? Dismiss Show All Questions sorted by Date Posted

Show

sorted by

kgoderis kgoderis

Redirect to URL generated through Apex and Ajax using either a outputlink or button

Hello

as part of a VF page I have the following javascript defined:

      <apex:actionFunction name="buildURL" action="{!generateURL}" rerender="outlink,schedbutton">
          <apex:param name="sessionID" assignTo="{!sessionID}" value="" />
          <apex:param name="Partner_Server_URL_150" assignTo="{!ServerURL}" value="" />
      </apex:actionFunction>

whereby the buildURL is called, in Apex, each time a user selects an Account from a list. All it does is compile an URL and rerender some  VF blocks. When the user clicks a button or link, he gets redirected to a server which unfortunately does not accept UTF-8 encoded URLs. I prefer buttons, but here is what I have defined on the page:

Elsewhere I have defined a OutputLink as follows:

   <apex:pageblock title="test" id="outlink">
      <apex:outputLink value="{!generatedURL}" target="_self" id="outurl">
        link is {!generatedURL}
    </apex:outputLink> 
   </apex:pageblock>

I also have a button defined as follows:

  <apex:pageBlockButtons >
        <apex:commandButton onclick="redirectToScheduler()" value="Schedule Calls" id="schedbutton" />  
   </apex:pageBlockButtons>
    <script type="text/javascript">
   function redirectToScheduler() {
    window.location.href=document.getElementById("outlink").value;
   </script>

Problems:

- the URL works perfectly fine, except for the fact that the redirect does not happen in the "right-hand" frame in SF

- the button: does not work at all, but I am not a JavaScript expert either, but I think that the javascript is not picking up the ajax-modified value of the URL, but is rather taking the standard value as defined by the initial load of the page. Is there a way to read out value on a html page that were modified through Ajax?

any help is very much appreciated

Karel

Imran Mohammed Imran Mohammed

Hi,

In Visualforce, the way ids should be accessed is little different.

Try the below code and see.

<apex:pageBlockButtons >
        <apex:commandButton onclick="redirectToScheduler()" value="Schedule Calls" id="schedbutton" />  
   </apex:pageBlockButtons>
    <script type="text/javascript">
   function redirectToScheduler() {
    window.location.href=document.getElementById('{!$Component.outlink.outurl}').value;
   </script>
kgoderis kgoderis

Thanks

Tried it, but the value is "undefined", however, the $Component reference is valid (checked through JS aler() function)

K

Imran Mohammed Imran Mohammed

This should work.

Can you post your VF page code, so that i can see?

kgoderis kgoderis

  <apex:pageBlockButtons >
        <apex:commandButton onclick="location.replace('{!generatedURL}')" value="Schedule Calls" id="schedbutton" />  
   </apex:pageBlockButtons>

When I try the code above, nothing works. However, pasting the rendered part of location.replace.... in the console of Chrome it *does* redirect. Clicking the button does nothing at all

kgoderis kgoderis

Here is the VF "as is stands"

<apex:page controller="P_AccountSearchController" sidebar="true">
  <apex:form >
  <apex:pageMessages id="errors" />
  <apex:pageBlock title="Query my Accounts!" mode="edit">
  <apex:pageBlockButtons >
        <apex:commandButton onclick="location.replace('{!generatedURL}')" value="Schedule a Call" id="schedbutton" />  
   </apex:pageBlockButtons>
    <script type="text/javascript">
   function redirectToScheduler() {
        alert(document.getElementById("{!$Component.outlink.outurl}").value);   }
   </script>
   <apex:pageblock title="test - do not use. Karel." id="outlink">
      <apex:outputLink value="{!generatedURL}" target="_self" id="outurl">
        link is {!generatedURL}
    </apex:outputLink> 
   </apex:pageblock>
  <table width="100%" border="0">
    <td width="200" valign="top">
      <apex:pageBlock title="Parameters" mode="edit" id="criteria">
      <script type="text/javascript">
      function doSearch() {
        searchServer(
          document.getElementById("accountname").value,
          document.getElementById("zip").value,
          document.getElementById("city").value,
          document.getElementById("target").value,
          document.getElementById("accounttype").options[document.getElementById("accounttype").selectedIndex].value,
          document.getElementById("hasAddress").value,
          document.getElementById("hasLastCall").value,
          document.getElementById("hasNextCall").value
      </script> 
      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="name" value="" />
          <apex:param name="zip" value="" />
          <apex:param name="city" value="" />
          <apex:param name="target" value="" />
          <apex:param name="accounttype" value="" />
          <apex:param name="hasAddress" value="" />
          <apex:param name="hasLastCall" value="" />
          <apex:param name="hasNextCall" value="" />
      </apex:actionFunction>
      <apex:actionFunction name="scheduleServer" action="{!schedule}" rerender="results,debug2,errors">
          <apex:param name="sessionID" assignTo="{!sessionID}" value="" />
          <apex:param name="Partner_Server_URL_150" assignTo="{!ServerURL}" value="" />
      </apex:actionFunction>
      <apex:actionFunction name="buildURL" action="{!generateURL}" rerender="outlink,schedbutton">
          <apex:param name="sessionID" assignTo="{!sessionID}" value="" />
          <apex:param name="Partner_Server_URL_150" assignTo="{!ServerURL}" value="" />
      </apex:actionFunction>
      <table cellpadding="2" cellspacing="2">
        <td style="font-weight:bold;">Name<br/>
        <input type="text" id="accountname" onkeyup="doSearch();"/>
        <td style="font-weight:bold;">Postal Code<br/>
        <input type="text" id="zip" onkeyup="doSearch();"/>
        <td style="font-weight:bold;">City<br/>
        <input type="text" id="city" onkeyup="doSearch();"/>
        <td style="font-weight:bold;">Account Type<br/>
          <select id="accounttype" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!accounttypes}" var="type">
              <option value="{!type}">{!type}</option>
            </apex:repeat>
          </select>
        <td style="font-weight:bold;">Target<br/>
        <input type="checkbox" id="target" onchange="doSearch();" checked="on"/>
        <td style="font-weight:bold;">Accounts with Addresses<br/>
        <input type="checkbox" id="hasAddress" onchange="doSearch();" checked="on"/>
        <td style="font-weight:bold;">Accounts with Last Calls<br/>
        <input type="checkbox" id="hasLastCall" onchange="doSearch();" />
        <td style="font-weight:bold;">Accounts with Next Calls<br/>
        <input type="checkbox" id="hasNextCall" onchange="doSearch();" />
      </table>
      </apex:pageBlock>
    <td valign="top">
    <apex:pageBlock mode="edit" id="results">
        <apex:pageBlockTable value="{!accounts}" var="account">
           <apex:column width="25px">
               <apex:inputCheckbox value="{!account.checked}" onclick="buildURL('{!$Api.Session_Id}','{!$Api.Partner_Server_URL_150}')"/>
           </apex:column>
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputLink value="https://na7.salesforce.com/{!account.account.id}">{!account.account.name} </apex:outputlink>
            </apex:column>
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Account Type" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="recordtype.id" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputText value="{!account.accounttype}"/>
            </apex:column>
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Postal Code" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="zip_vod__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputText value="{!account.zip}"/>
            </apex:column>
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="City" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="city_vod__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputText value="{!account.city}"/>
            </apex:column>
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Last Call" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="LastCall" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputText value="{!account.lastCall}"/>
            </apex:column>
                        <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Next Call" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="NextCall" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputText value="{!account.nextCall}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
  </table>
    <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!debugSoql}" />
  </apex:pageBlock>    
      <apex:pageBlock title="Debug - SessionID" id="debug2">
      <apex:outputText value="{!SessionID}" />
  </apex:pageBlock>  
  </apex:pageBlock>
  </apex:form>
</apex:page>