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
Carter85Carter85 

Open pdf static resource with button press in new window

I'm trying to open a pdf I've saved as a static resource in a new window off a button press.  The idea is to allow different representatives while dealing with customers to be able to pull up the specific form for a policy in a separate window if the need arises to quote directly from it.  At the moment my button code is something like the below just to test, but if anyone has a better idea how, either how to call it or where to save the forms to in the first place. to do it I'm open to suggestion, because the the moment the below is producing the error:

A problem with the OnClick JavaScript for this button or link was encountered:

missing : after property id

 

window.open('/viewStaticResource.apexp?id='+{MG_MAG_Claim__c.FID__c})", '_blank');

 

GlynAGlynA

I have done this with a simple VF page, something like:

 

<apex:page standardController="MyObject__c">
<script>
    window.open('{!$Resource.ThePDF}', '_parent');
</script>
</apex:page>

Create a custom button that launches this page in a new window, add the button to your layout, et voila!

 

I've tried putting the JS into a custom JS button, but the $Resource global variable isn't available in JS buttons, only in VF pages.  Go figure.

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

Carter85Carter85

I think your suggestion is what I'm looking for, however I'm running into a little bit of trouble accounting for all the potential variations.  It would be simple if every one of the instances would only need one specific resource, but as we have different forms for various products I'm trying to make sure the right one is assigned to each new entry on the claim object.  I have this at the moment for the new VF popup:

<apex:page standardController="MG_MAG_Claim__c"> <script> window.open({!MG_MAG_Claim__c.FID__c},'_parent'); </script> </apex:page>

 where {!MG_MAG_Claim__c.FID__c} equals something like: {!$Resource.F069}

 

 However, at the moment when I press the new button it does open the new popup, but it is simply blank, so I'm not sure if it's a syntax error or if I just can't manipulate a resource reference like that, so if you had any additional thoughts I'd welcome them.

GlynAGlynA

It won't work to have a merge field evaluate as another merge field.  The substitution is only done once.

 

Instead, try creating a different script block for each possible static resource PDF file.  Put each of those into it's own <apex:outputPanel> with the "rendered" attribute = to something like "{!IF(MG_MAG_Claim__c.FID__c==XXX)}", where XXX is whatever the field needs to equal in order to display that PDF file.  This way, only the desired script will be rendered and executed.

 

-Glyn

Carter85Carter85

Are there any settings I might have to allow for within salesforce aside from making the resource public to get the pdf to display properly in the window?  Because for some reason I'm still getting a blank page, while the pdf is auto downloaded to my desktop rather than opened to view, even when I go back and try your first suggestion with something like:

<apex:page standardController="MG_MAG_Claim__c">
<script>
    window.open('{!$Resource.F069}', '_parent');
</script>
</apex:page>

 I think your latest suggested method will work, but for this one wrinkle.

Carter85Carter85

Never mind, that was just caused by a brower issue with firefox I'll need to make allowances for somehow.

Carter85Carter85

Ok, so I've been trying to separate the outputpanels as you suggested with the render attribute, however, I think I'm missing something in my controller because when I try to grab the FID from the page it doesn't seem to be getting it with what I have so far, or for some reason it doesn't like rendering the static resource when there's a render attribute on the panel because the {!show} variable definitely contains the F069 value in my tests and if I take out the rendered test and just leave it

<apex:outputPanel>
<script>
window.open('{!$Resource.F069}', '_parent');
</script>
</apex:outputPanel>

 it renders no problem.

Thoughts?

VF at the moment:

<apex:page standardController="MG_MAG_Claim__c" extensions="ShowForm" action="{!showForm}">
<apex:form>
<apex:pagemessages />
<apex:outputPanel rendered="{IF({!show}==F069)}">
<script>
window.open('{!$Resource.F069}', '_parent');
</script>
</apex:outputPanel>
</apex:form>
</apex:page>

 Controller:

public with sharing class ShowForm{

        public MG_MAG_Claim__c claim    {get;set;}
        public String show {get;set;}
                        
    public ShowForm(){
    }
    
    public ShowForm(ApexPages.StandardController controller){
       claim = (MG_MAG_Claim__c)controller.getRecord();
    }    
    
    public PageReference showForm(){
        PageReference pageReference = null;
        if(claim != null){
                List<MG_MAG_Claim__c> nu = [SELECT FID__c FROM MG_MAG_Claim__c WHERE id=:claim.id];
                if(nu != null){
                             show = nu[0].FID__c;       
                }
                else{
                     ApexPages.Message msg = new ApexPages.Message( ApexPages.Severity.ERROR, 'Form Scan Not Found');                   
                     ApexPages.addMessage(msg);                                                              
                     }                                                                                                      
                     }
                     return(pageReference);
                     }
}

 

GlynAGlynA

Add quotes aroung the F069 in the rendered attribute and fix the syntax (you don't need the IF):

<apex:outputPanel rendered="{!show=='F069'}">

 Let me know if that does it.

 

-Glyn

 

 

 

Carter85Carter85

Unfortunately no, just opens a blank page.

Deepali KulshresthaDeepali Kulshrestha
Hi Carter85,
 
It is not feasible by static resource, but you can do it with the document object, upload the pdf in a document and then query the URL of pdf.

I suggest you visit this link:
https://developer.salesforce.com/docs/atlas.en-us.sfFieldRef.meta/sfFieldRef/salesforce_field_reference_Document.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Vishal Agrawal 35Vishal Agrawal 35
I want it  to do same in lighning component