+ Start a Discussion
RovRov 

Dynamic referece to File ID in Apex Class

In the Documents, I have a public Folder called 'Images' where I have 6 images. How can I avoid hardcoding the file_id and instead get it dynamically.

Object__c obj = new Object__c();

obj.Test__c = '<img alt="comment image" src="/servlet/servlet.FileDownload?file=015n00000004vra" ></img>';

I know I can create custom setting but again that would mean hardcoding the value.

I want to Query the the folder  from Documents and get respective fileid from the folder.

Thanks!
Best Answer chosen by Rov
Marty C.Marty C.

Hello, rov, the best way to achieve this is to first store your folder name as a constant. This way it's easy for you to update your code later if the folder name changes. Then, add the folder name as a criterion when querying for your desired documents, as seen in this example[1].

[1]: this example (http://pastebin.com/9rqpVVhP)

All Answers

Marty C.Marty C.

Hello, rov, the best way to achieve this is to first store your folder name as a constant. This way it's easy for you to update your code later if the folder name changes. Then, add the folder name as a criterion when querying for your desired documents, as seen in this example[1].

[1]: this example (http://pastebin.com/9rqpVVhP)

This was selected as the best answer
RovRov
Thanks Marty. I tried doing the same thing before posting my question and for some reason failed. The example you pointed out is great!! exactly what i wanted.

This is what i did to get the file id

String IMAGE_FOLDER = 'Prohect_Images';
List<Document> docs = [SELECT Id, Name, Type FROM Document WHERE Folder.Name = :IMAGE_FOLDER];
system.debug('>>>>>>>>>>>' +docs.size());
for(Document d: docs){
  
    if (d.name == 'Image_comment'){
     string commentimage = d.id;  
      system.debug('comment' +d.id);  
    }else if (d.name == 'Image_attachment'){
     string attachmentimage = d.id;  
         system.debug('attach' +d.id);
    } else if (d.name=='Image_email'){
     string emailimage = d.id;          
         system.debug('email' +d.id);
    }
   

}
RovRov
I have one more question .. I did the following to get the image id which is great!!

<pre>
String IMAGE_FOLDER = 'Project_Images';

List<Document> docs = [SELECT Id, Name, Type FROM Document WHERE Folder.Name = :IMAGE_FOLDER];

system.debug('>>>>>>>>>>>' +docs.size());

for(Document d: docs){
 
    if (d.name == 'Image_comment'){
     string commentimage = d.id; 
      system.debug('comment' +d.id); 
    }else if (d.name == 'Image_attachment'){
     string attachmentimage = d.id; 
         system.debug('attach' +d.id);
    } else if (d.name=='Image_email'){
     string emailimage = d.id;         
         system.debug('email' +d.id);
    }
  

}

</pre>

Now if i pass the string as shown below I don't see the image

obj.Test__c = '<img alt="comment image" src="/servlet/servlet.FileDownload?file=commentimage" ></img>';


However if i hardcode I see the image

obj.Test__c = '<img alt="comment image" src="/servlet/servlet.FileDownload?file=0676FGHJ567" ></img>';


Seems like i need escape characters or the correct syntax.

Any suggestions? Thanks>
Marty C.Marty C.

Hello, rov, it seems like you may benefit from learning more about getter methods[1] in Visualforce, and also about merge fields[2] for Visualforce. Applying these two concepts, you should be able to use the following in your Visualforce page is:

<apex:image value="{!URLFOR($Action.Document.Download, d.Id)}"/>

You may also want to place an apex:repeat[3] component on your Visualforce page, if you're planning to provide download links for a variable number of documents.

[1]: getter methods (http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_quick_start_controller_getter_methods.htm?SearchType=Stem)
[2]: merge fields (http://help.salesforce.com/apex/HTViewHelpDoc?id=pages_merge_fields.htm&language=en_US)
[3]: apex:repeat (http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_repeat.htm)

RovRov
Though late but learnt something .... to avoid harcoding the image id I can also  do this -

My_Custom_Settings__c customsetting=My_Custom_Settings__c.getValues('nameundermanage');

string imageid = = '/servlet/servlet.FileDownload?file=' +customsetting.Image_Id__c;

//obj.Test__c = '<img alt="comment image" src="/servlet/servlet.FileDownload?file=0676FGHJ567" ></img>';

obj.Test__c = '<img alt="comment image" src="  '+   imageid  +'    " ></img>';