Is it safe to hardcode document url in visualforce email template?
Hi All,
Is it safe to hardcode document url within <apex:image> tag in visualforce email template for embedding image as recommended in salesforce documentation
Hi Logesh, In my opinion, Hardcoded absolute URL's have to be avoided always. There may be org splits which salesforce performs periodically and your org may be placed on a different instance. Its not easy to search and replace all such hardcoded URL instances. Further, when you refresh your sandbox, the hardcoded url's do not change and hence do not work in the sandbox.
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution. If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
The URL will never change for that document, so it is safe to hard-code an ID value. This is true even if the email template is used in multiple organizations. This is because an emailable link would be authentication agnostic and thus it doesn't matter where the image is coming from. Just make sure that you don't delete that document where the image is stored.
If you want to render in word, you will have to put in the full url. For getting the docuemnt ID, you can either hardcode it, which will be fine after sandbox refreshes, or you could just query to get the id. Also for word, you can use these 4 lines to get the domain so you can dynamically get that:
In my opinion, Hardcoded absolute URL's have to be avoided always. There may be org splits which salesforce performs periodically and your org may be placed on a different instance. Its not easy to search and replace all such hardcoded URL instances.
Further, when you refresh your sandbox, the hardcoded url's do not change and hence do not work in the sandbox.
Its always recommended to use relative URL's.
The link below has some information:
http://wiki.developerforce.com/page/Best_Practices_When_Referencing_Server_Endpoints
Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
it is safe but when you will deploy your emal template to poduction then you need to change it so better if you keep it in custom setting.
If you want to render in word, you will have to put in the full url. For getting the docuemnt ID, you can either hardcode it, which will be fine after sandbox refreshes, or you could just query to get the id. Also for word, you can use these 4 lines to get the domain so you can dynamically get that:
baseUrl = String.valueOf(URL.getSalesforceBaseUrl());
integer startSub = baseUrl.indexOf('c.') + 2;
integer endSub = baseUrl.indexOf('.visual.');
domain = baseUrl.substring(startSub, endSub);
and you will need to get the org id, which is dead easy:
orgId = UserInfo.getOrganizationId();
so doing would work anywhere:
https://c.{!domain}.content.force.com/servlet/servlet.ImageServer?id={!docId}&oid={!orgId}
or
https://c.{!domain}.content.force.com/servlet/servlet.ImageServer?id={!docMap[docName]}&oid={!orgId}