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
rtyanasrtyanas 

Using Field Values in URL to View SalesForce Document

 

In a VF page I have a SelctList and an OutputLink with a field value as part of the definition of the URL using the select list variable "listDocsSQL"

 

      <apex:pageBlock>
        <apex:selectList id="documentsList" value="{!listDocsSQL}" multiselect="false" style="width:300px" size="5">
            <apex:selectOptions value="{!DocListOptions}" />
        </apex:selectList>
      </apex:pageBlock>

      <apex:pageBlock>
         <apex:outputLink id="docLink" value="https://c.cs10.content.force.com/servlet/servlet.FileDownload?file={!listDocsSQL}" >
          Doc Link
	  <apex:commandButton value="Open Doc" action="{!openSelection}" />
        </apex:outputLink>
      <apex:pageBlock>

 

I put a button in to set the "listDocsSQL" variable to the selected "id" value and open the URL but, found, using a debug statement, that the variable is set as it should be when the option is selected using the set method for "listDocsSQL" that part seems to work...

 

snippet ..

public class DocumentSearchController{ public String listDocsSQL {get; set;} public String filterResources{get; set;} String filterRes; String filterResID; /**
This method filters the list depending on the user text input **/ public List<Document> getlistDocsSQLOptions() { System.debug('getListDocs filterRes: '+ filterRes); filterResources = '%'+ filterResources +'%'; return (List<Document>) [select id, Name, url, Type, FolderId, BodyLength from Document where (Type = 'pdf') AND (Name LIKE :filterResources ) ]; } public List<SelectOption> getDocListOptions(){ List<SelectOption> options1 = new List<SelectOption>(); List<Document> docListSelectOption = getlistDocsSQLOptions() ; for(Document doc : docListSelectOption) { System.debug('getDocListOptions name: '+ doc.name +' filterRes: '+ filterRes +', id: '+ doc.id); options1.add(new SelectOption(doc.id, doc.name)); } return options1; } public PageReference openSelection() { System.debug('openSelection listDocsSQL: '+ listDocsSQL ); // https://c.cs10.content.force.com/servlet/servlet.FileDownload?file=015J000000058sW return null; }

 

It does not work as expected when I try to open the URL after selecting the item in the select list, the URL link will open the selected document only when I first push the button.  This is not expected.

 

I do not want to have the button on the page.  Is there a way I can select the item from the list, then open the URL without having to push the button first?

 

Thanks.

 

 

Edwin VijayEdwin Vijay

Add a onchange function to the select list.. something like this.. Please check the syntax.. might be wrong

<apex:pageBlock>
        <apex:selectList onchange="openurl({!listDocsSQL})"id="documentsList" value="{!listDocsSQL}" multiselect="false" style="width:300px" size="5">
            <apex:selectOptions value="{!DocListOptions}" />
        </apex:selectList>
      </apex:pageBlock>

      <apex:pageBlock>
         <apex:outputLink id="docLink" value="https://c.cs10.content.force.com/servlet/servlet.FileDownload?file={!listDocsSQL}" >
          Doc Link
	  <apex:commandButton value="Open Doc" action="{!openSelection}" />
        </apex:outputLink>
      <apex:pageBlock>
<script>
function openurl(strval)
{
window.open("https://c.cs10.content.force.com/servlet/servlet.FileDownload?file="+strval);
}
</script>