+ Start a Discussion
dafunkdafunk 

Problem with URL.getFileFieldURL - fails on fieldName parameter

I am querying a list of attachmens and displaying the list below the file upload section of the page. 

In the list I want a URL to view the file in a new window. (The built in RelatedList does not perform the same).

On the controller I attempt to get the URL with :

@RemoteAction
global static String getFileURL(String id){
try {
                   fileURL = URL.getFileFieldURL( id ,'AttachmentBody');
                   return fileURL;
}catch(Exception ex){
                   fileURL = ex.getMessage();
                   return fileURL;
}

}

The call to URL.getFileFieldURL fails with an Invalid Parameter value for the fieldName - this is exactly as per the documentation code

Any ideas would be helpful

thanks

Joe

 

Best Answer chosen by dafunk
dafunkdafunk

I fixed this by looking at how SF builds the URL to a 'View File' link. This is not the best thing with hard coding part of the path.

I am sure this will be fixed in a future release meanwhile it works.

 

@RemoteAction
global static String getFileURL(String id){

try {
fileURL = URL.getSalesforceBaseUrl().toExternalForm();
fileURL += '/servlet/servlet.FileDownload?file=' + id;
return fileURL;
}catch(Exception ex){
fileURL = ex.getMessage();
return fileURL;
}

}

 

All Answers

Vinita_SFDCVinita_SFDC

Hello Joe,

 

Please check if a valid ID is being passed to the method(using system.debug), for testing you can try providing a value like:

 


String fileURL =
  URL.getFileFieldURL(
    '087000000000123' ,
    'AttachmentBody');

dafunkdafunk

I queried the Attachments to confirm that I had the right IDs - 

DEBUG LOG
28.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous:
Execute Anonymous: String u = '';
Execute Anonymous: u=URL.getFileFieldURL( '00Pd0000006502NEAQ' ,'AttachmentBody');
Execute Anonymous: System.debug('!@#:' + u);
10:02:53.047 (47578000)|EXECUTION_STARTED
10:02:53.047 (47590000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
10:02:53.048 (48906000)|FATAL_ERROR|System.HandledException: Invalid parameter value "AttachmentBody" for parameter "fieldName".

 

Same issue - 

 

dafunkdafunk

I fixed this by looking at how SF builds the URL to a 'View File' link. This is not the best thing with hard coding part of the path.

I am sure this will be fixed in a future release meanwhile it works.

 

@RemoteAction
global static String getFileURL(String id){

try {
fileURL = URL.getSalesforceBaseUrl().toExternalForm();
fileURL += '/servlet/servlet.FileDownload?file=' + id;
return fileURL;
}catch(Exception ex){
fileURL = ex.getMessage();
return fileURL;
}

}

 

This was selected as the best answer
Joost KarstenJoost Karsten
This is still an issue. 
Joost KarstenJoost Karsten
Oh i see, its specific for File. It won't work on Attachment
LVSLVS
@Joost Karsten were you able to use this for Files? I am not able to.