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
Dave ScottDave Scott 

Custom button on Contact object

Hi all, hope you can help!

Please could someone help me create a custom button the Contact object that will send a template email to that particular contact and record the email in Activity History.

Our company has two brands and therefore two different templates, so we need to make sure the correct branded template is sent out to the contact.  The brand is determined by a custom picklist field on the Contact object called 'Brand__c' and has only two values 'Brand X' and 'Brand Y'.

How can this be done?

Many thanks,
Dave
Best Answer chosen by Dave Scott
Krishna SambarajuKrishna Sambaraju
Hi Dave,

Sorry, you also need the change the id of the contact id according to your contact's id. Sorry my bad. Here is how it should be.
var brand = '{!Contact.Brand__c}';
if(brand == 'Brand X')
{
	window.location = '/_ui/core/email/author/EmailAuthor?p2_lkid={!Contact.Id}&rtype=003&retURL=%2F{!Contact.Id}&template_id=template_id_brand_x'
}
if(brand == 'Brand Y')
{
	window.location = '/_ui/core/email/author/EmailAuthor?p2_lkid={!Contact.Id}&rtype=003&retURL=%2F{!Contact.Id}&template_id=template_id_brand_y'
}
Change the template ids accordingly and try it now.

 

All Answers

Krishna SambarajuKrishna Sambaraju
Create a custom detail page button on contact. Select the behaviour as "Execute Javascript" and in the text area use the following code.
if({!Contact.Brand__c == 'Brand X')
{
window.location = '/_ui/core/email/author/EmailAuthor?p2_lkid=003d000002otSkO&rtype=003&retURL=%2F003d000002otSkO&template_id=template_id_brand_x'
}
if({!Contact.Brand__c == 'Brand Y')
{
window.location = '/_ui/core/email/author/EmailAuthor?p2_lkid=003d000002otSkO&rtype=003&retURL=%2F003d000002otSkO&template_id=template_id_brand_y'
}
Change the template_id parameter in the above code with the relevant template id of the brand.

Hope this helps.
Krishna SambarajuKrishna Sambaraju
There is a syntax error in the above code, use the code below.
var brand = '{!Contact.Brand__x}';
if(brand == 'Brand X')
{
	window.location = '/_ui/core/email/author/EmailAuthor?p2_lkid=003d000002otSkO&rtype=003&retURL=%2F003d000002otSkO&template_id=template_id_brand_x'
}
if(brand == 'Brand Y')
{
	window.location = '/_ui/core/email/author/EmailAuthor?p2_lkid=003d000002otSkO&rtype=003&retURL=%2F003d000002otSkO&template_id=template_id_brand_y'
}

Hope this helps.
 
Dave ScottDave Scott
Thanks for this Krishna, when I first tried this I got an error stating:
Error: Field Contact.Brand__x does not exist. Check spelling.

But then I realised Brand__x was probably a typo and corrected it to Brand__c.

After I input the id values for each of the brands template the code succesfully saved, but when I tested the button on the Contact page, i got the following error message:
Data Not Available 
The data you were trying to access could not be found. It may be due to another user deleting the data or a system error. If you know the data is not deleted but cannot access it, please look at our support page.

Any ideas on what I can do to resolve this?

Your help is greatly appreciated,
Many thanks,
Dave
Krishna SambarajuKrishna Sambaraju
Hi Dave,

Sorry, you also need the change the id of the contact id according to your contact's id. Sorry my bad. Here is how it should be.
var brand = '{!Contact.Brand__c}';
if(brand == 'Brand X')
{
	window.location = '/_ui/core/email/author/EmailAuthor?p2_lkid={!Contact.Id}&rtype=003&retURL=%2F{!Contact.Id}&template_id=template_id_brand_x'
}
if(brand == 'Brand Y')
{
	window.location = '/_ui/core/email/author/EmailAuthor?p2_lkid={!Contact.Id}&rtype=003&retURL=%2F{!Contact.Id}&template_id=template_id_brand_y'
}
Change the template ids accordingly and try it now.

 
This was selected as the best answer
Dave ScottDave Scott
Thanks Krishna, this seems to work great.

Is there anyway to get the email to Send when the button is clicked, rather than having to take the user into the email composer to then click the Send button?

Many thanks once again, really appreciate your help on this one.
Dave
Krishna SambarajuKrishna Sambaraju
You have to create a custom class (webservice) with a method to send the email, pass the arguments to this method and execute the method. Are you familiar with coding using apex?
Dave ScottDave Scott
No, I'm not, but I think i'll just stick with what you've managed to help me with!

Many thanks,
Dave