+ Start a Discussion
Linda 98Linda 98 

Get attachment id dynamically


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.!!!!!!!

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.

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!!!
You don't need to built a URL Seperately in email template you can just copy paste the attachment url like this in template


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 >

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">
       <td align="left"><font face="Arial" >
      <b>Expense Report for  {!relatedTo.Name} </b></font><br/>
<table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1">
<table style="width:100%">
<font face="Arial"> Report Name &nbsp;&nbsp; {!relatedto.Name} <br/><br/></font>
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.