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
SayasoniSayasoni 

Help with Invalid parameter for function URLFOR on Empty Notes & Attachment

Hi Good People,

I am trying to download an attachment from a command button in Visualforce page. My functionality works fine on records that have an attachment.However, i get "Invalid parameter for function URLFOR

Error is in expression '{!URLFOR($Action.Attachment.Download, attId)}' in component <apex:page> in page mypage"  error on a record that does not have an attachement. How do i get to display this message on records that have no attachment when the Command button is clicked. ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'The document is unavailable!')); instead of getting the above error?

 

Below is part of my controller and Visualforce page that does the download:

public String attId{get;set;}
Candidate__c cand = [Select id,Name,First_Name__c,from Candidate__c where id = :PgId];
Attachment att = [Select id,parentId from Attachment  where parentId = :cand.id];
    if(att != null){
    	attId = att.id;
    	}  
   }  

 

<apex:commandButton action="{!URLFOR($Action.Attachment.Download, attId)}" styleClass="buttonStyle" style="width:100px;height:30px;background:#6699FF;" value="Display Document" rerender="false"/>        

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I would change the page so that if there are no attachments, the button is not rendered.  I think you may also need to defend against more than one attachment for a record.  Something like:

 

public String attId{get;set;}
Candidate__c cand = [Select id,Name,First_Name__c,from Candidate__c where id = :PgId];
List<Attachment> att = [Select id,parentId from Attachment  where parentId = :cand.id];
    if(!att.size() != null){
    	attId = att[0].id;
    	}  
   }  
<apex:commandButton action="{!URLFOR($Action.Attachment.Download, attId)}" styleClass="buttonStyle" style="width:100px;height:30px;background:#6699FF;" value="Display Document" rerender="false" rendered="{!NOT(ISBLANK(attId))}"/>   
<apex:outputText value="No attachments" rendered="{!ISBLANK(attId)}"/>

 

 

 

All Answers

bob_buzzardbob_buzzard

I would change the page so that if there are no attachments, the button is not rendered.  I think you may also need to defend against more than one attachment for a record.  Something like:

 

public String attId{get;set;}
Candidate__c cand = [Select id,Name,First_Name__c,from Candidate__c where id = :PgId];
List<Attachment> att = [Select id,parentId from Attachment  where parentId = :cand.id];
    if(!att.size() != null){
    	attId = att[0].id;
    	}  
   }  
<apex:commandButton action="{!URLFOR($Action.Attachment.Download, attId)}" styleClass="buttonStyle" style="width:100px;height:30px;background:#6699FF;" value="Display Document" rerender="false" rendered="{!NOT(ISBLANK(attId))}"/>   
<apex:outputText value="No attachments" rendered="{!ISBLANK(attId)}"/>

 

 

 

This was selected as the best answer
SayasoniSayasoni

Thanks Bob, that did it :-)

ShoaebKShoaebK
Hey Bob, i have the same problem, using apex:image kindly help me to solve this.
VF Code
<script>
<apex:includeScript value="{!URLFOR($Resource.FC_Chart,'/api/js/jquery-1.11.3.min.js')}"/>
        <script type="text/javascript" src="https://canvg.github.io/canvg/rgbcolor.js"></script>
        <script type="text/javascript" src="https://canvg.github.io/canvg/StackBlur.js"></script>
        <script type="text/javascript" src="https://canvg.github.io/canvg/canvg.js"></script>
        <script>
            window.onload=function(){
                alert('ok');
                var mainDiv=document.getElementById('chart_div');
                var svg = mainDiv.children[0].children[0].innerHTML;
                console.log('@@svg .... ' + svg);
                canvg(document.getElementById('canvas'),svg);
                var img = canvas.toDataURL("image/png"); //img is data:image/png;base64
                img = img.replace('data:image/png;base64,', '');
                var data = "bin_data=" + img;
                alert(data);
                alert(img);
              
                Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.VWSizingReport02Controller.saveChartAsAttachment}',
               '{!Proj.ID}',img,
                  function(result,event){
                     if(event.status){
                     alert("DOne");
                       console.log('@@@ success');
                       
                     }
                     else {
                       console.log('@@@ fail');
                     alert("Error");
                     }
                  } );
            };
</script>


<div id="chart_div">
                   <p class="valve-notes" >
                        <label class="valve-notes">Flow Curve Graph:</label>
                    </p>
                    
                    <apex:chart animate="false" resizable="true" height="480" width="680" data="{!chartData}">
                        <apex:legend position="bottom"/>
                        <apex:axis steps="10" type="Numeric" position="left" fields="percentCv" 
                                   title="% Cv"/>        
                        <apex:axis steps="5" maximum="100" type="Numeric" position="bottom" fields="percentOpen" 
                                   title="% Open"/>
                        <apex:lineSeries title="Maximum Flow" axis="left"  xField="maximumFlow" yField="percentCv"
                                         markerType="circle" markerSize="2" markerFill="#FF0000"/>
                        <apex:lineSeries title="Normal Flow" axis="left"  xField="normalFlow" yField="percentCv"
                                         markerType="circle" markerSize="2" markerFill="#17ff00"/>
                        <apex:lineSeries title="Minimum Flow" axis="left"  xField="minimumFlow" yField="percentCv"
                                         markerType="circle" markerSize="2" markerFill="#0063ff"/>
                        <apex:lineSeries smooth="4" title="Valve Inherent Characteristic" axis="left" xField="percentCv" yField="requiredCv" 
                                         markerType="circle" markerSize="3" markerFill="#00000"/>
                    </apex:chart>
                     <canvas id="canvas" style="display:none;"></canvas>
                </div>
               
              <apex:image url="{!URLFOR($Action.Attachment.Download, AttachmentId)}"/>

Apex Code

public static ID AttachmentId {get; set;}	
@RemoteAction
    global static void saveChartAsAttachment(String prjId,String attachmentBody){
     //  system.debug('@@@saveChartAsAttachment  oppId ='+ oppId);
     system.debug('Project ID: '+ prjId);
       List<Attachment> listAttachment =[Select Name,Id,Body from Attachment Where ParentId=:prjId];
       system.debug('Size of Attachment: '+listAttachment.size());
       Attachment att;
       if(listAttachment.size()>0){
          att =listAttachment[0];
          att.Body = EncodingUtil.base64Decode(attachmentBody);
          update att;
       
       }
       else{
       
           att = new Attachment();
           att.Name='spiderChart_VF';
           att.ParentId=prjId;
           att.ContentType='image/png';
           att.Body = EncodingUtil.base64Decode(attachmentBody);
           insert att;
    
       }
       String name='spiderChart_VF';
       List<Attachment> insAtts = [select id from Attachment where Name= 'spiderChart_VF' AND ContentType='image/png' AND ParentId=:prjId];
       Attachment insAtt;
        if(insAtts.size() > 0){
            insAtt = insAtts[0];
            AttachmentId = insAtt.Id;
        }

    }
ShoaebKShoaebK

Bob, i'm having this error for the above mentioned code.

Error: Error occurred while loading a Visualforce page.

Error: Invalid parameter for function URLFOR