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
Linda 98Linda 98 

Get attachment id dynamically

Hi,

i am having an URL field on account object.
For now i hard coded the value as https://c.na14.content.force.com/servlet/servlet.FileDownload?file=00Pd000000JHoPF
(I am getting image whose name starts with  or contains 'bill' in its name from notes and attachment section and using that.)
I would like to make it dynamic.

My idea is: to create a new text field on account object which holds the attachmentid and create a formula fields which joins URL value and attachment id .
Or else ho can i use it??

I need to use this field /URL in visualforce template.So its required to generate that field in this format:
'https://c.na14.content.force.com/servlet/servlet.FileDownload?file= +attachmentid'

Please help.!!!!!!!



ezdhanhussainezdhanhussain
HI you can do some thing like this..
//vf code
 <apex:image url="https://c.ap1.content.force.com/servlet/servlet.FileDownload?file={!at}" height="100" width="100"/>

//controller code

  public id getat()
    {
    list<Attachment> a=[select id from Attachment order by id desc limit 1];
    return a[0].id;// returns id value 
    }
As you said your attachement name starts with or contains "bill" filter your query using LIKE operator or do a wildcard search in query
Linda 98Linda 98
Yes. thnk you...I can do this like this but i am not using vf page..I want this on a vf email template..please suggest a way..
My vf email template doesnt have component reference and class.


ezdhanhussainezdhanhussain
In Visualforce email template you can get values dynamically by putting {!relatedto.fieldyouwant} inorder to get attachment id you have to select relatedto field of email template to attachment
Linda 98Linda 98
But how can i built this URL in template...??Basically my req is to show image content in email template.So i had created a formula fiedl with image URL .Thus i am able to display image content on my detail page.And i am using that field.So my crteria is to get that field build on account detail page.
Please suggest a way!!!
ezdhanhussainezdhanhussain
You don't need to built a URL Seperately in email template you can just copy paste the attachment url like this in template

https://c.ap1.content.force.com/servlet/servlet.FileDownload?file= 

now after equal to give {!relatedto.id}

Your vf template code shall look something like this
<messaging:emailTemplate subject="Test Template" recipientType="Contact" relatedToType="Attachment">
<messaging:plainTextEmailBody >
https://c.ap1.content.force.com/servlet/servlet.FileDownload?file={!relatedto.id}
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

Linda 98Linda 98
But the related to type is not attachment.
I have other merge fields which i had to show in my template.Those are related to other object.
Tis is just a part of template.

so i cant use https://c.ap1.content.force.com/servlet/servlet.FileDownload?file={!relatedto.id}   

This is how my template is:

<messaging:emailTemplate subject="Your  report" recipientType="User" relatedToType="Customobject__c" >

    <messaging:attachment renderAs="pdf" filename="{!relatedTo.name}"> 
<table border="0" cellspacing="0" cellpadding="0" width="150%" id="table1">
      <tr>
       <td align="left"><font face="Arial" >
      <b>Expense Report for  {!relatedTo.Name} </b></font><br/>
       </td>   
      </tr>
  </table>
  <hr/>
<table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1">
  <tr>  
   <td>
Report
   </td>
  </tr>
</table>
<table style="width:100%">
<tr>
<td>
<font face="Arial"> Report Name &nbsp;&nbsp; {!relatedto.Name} <br/><br/></font>
</td>
Picture you had requested <td> <img src='{!relatedto.URL_Picture__c}'/><br/><br/>  (this is how i am using now.URL_Picture__c is the field on my custom object which holds the URL of image.)
For now i hardcoded the value in record so able to display image content in template.

Please suggest a way.I need thi sASAP
Thank you.