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
Jose María Nicolás ArfelisJose María Nicolás Arfelis 

Email template based on two object which doesn't have any relationship and causing error

Hi all,

we have an email template built with Visualforce with "relatedToType="ObjectA__c":
 
<messaging:emailTemplate subject="Answer to your Request for Information" recipientType="Contact" relatedToType="ObjectA__c">
ObjectA was created to store all Resquests for Informations coming to Salesforce from the university web.

On the other hand, ObjectB was created to store all links the university, our client, has.
For ObjectB these fields were created: Name of Link, URL of Link.

ObjectA and ObjectB have no relationship, means, no Master-Detail and no Lookup.

However, I would like to refer to the field URL of the Link of ObjectB in the email template this way:
 
<messaging:emailTemplate subject="Answer to your Request for Information" recipientType="Contact" relatedToType="ObjectA__c">
...
<apex:outputLink value="{!IF(ObjectB__r.Name_of_Link__c = 'URL Admission Form', '{!ObjectB__r.URL_of_Link__c}', '')}" id="Admission">Admission Form</apex:outputLink>

...
The objective is for future manual changes in the value of the field URL of Link, to do these changes directly in the tab for ObjectB and not in the email template.

I tried using the code above, but it doesn't work.
Even refering to a formula field doesn't work.

I would appreciate your tips as I am struggling hours and hours trying to solve this :(.
Best Answer chosen by Jose María Nicolás Arfelis
sathishkumar periyasamysathishkumar periyasamy
Hi Jose,

As discussed - you are trying to store website URL in the custom object and utilize the those in visualforce email template. I would recomment to use Custom Label to store website URL for different scenario for each email template. And utilize below code in the VF email template.

Sample Code:
{!$Label.AdmissionURL}
{!$Label.RequestForInformationURL}

Please use below like to create new custom label:
https://codecracksblog.wordpress.com/2016/09/11/how-to-create-custom-label/

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Jose,
For such scenarios we do the following
1) Create a custom Component (Just for the Object B Part of you email template)
2) This custom Component will have its own controller (Therefore you can build your query to populate the value that you want)
3) Use the custom Component in your vf email template.
This way you can write your custom logic and pull the information from unrelated object.

There is nice example available at the below link for more reference. Hope this helps.

Link: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm
Jose María Nicolás ArfelisJose María Nicolás Arfelis
The subject is: Email template based on two objects which don't have any relationship and causing error. I can not correct it because of No edit options here.
@Prateek, thank you for your answer. I will try to implement it.
 
Jose María Nicolás ArfelisJose María Nicolás Arfelis
In the link you sent me I have noticed that they give an example using two objects which do have relationship: Account and Opportunity.
My objects don't have any relationship. Do you think, this would work using two objects, which don't have any relationship?
Prateek Singh SengarPrateek Singh Sengar
Hi Jose,
This option works for unrealed objects as well. The only thing that you need to ensure is that in your custom component controller, the query you write for fetching information from the object B returns unique result. Since there is no relationship you will to ensure in your soql that it returns only 1 record as per your use case. 
sathishkumar periyasamysathishkumar periyasamy
Hi Jose,

As discussed - you are trying to store website URL in the custom object and utilize the those in visualforce email template. I would recomment to use Custom Label to store website URL for different scenario for each email template. And utilize below code in the VF email template.

Sample Code:
{!$Label.AdmissionURL}
{!$Label.RequestForInformationURL}

Please use below like to create new custom label:
https://codecracksblog.wordpress.com/2016/09/11/how-to-create-custom-label/
This was selected as the best answer
Jose María Nicolás ArfelisJose María Nicolás Arfelis
thanks Sathish, good recommendation. It works for me!