Friday, July 29, 2011

#JavaScript Hack: Compose a New #Gmail Message with a Link to a #Salesforce Record

Today, I made tracks in putting an end to something that has irritated me for 3+ years. It’s a good day.

The Problem

Daily, I get emails like this: “Can you look at Opportunity 123? Something is wrong with it.”

After banging my head against my desk, I kindly reply “Would you mind sending me a link to the specific record you are viewing? Just copy and paste the url/webpage from your browser window. That way, I can make sure I’m viewing the exact same record that you are, since some have the same name.”

I’ve gotten a handful of my users trained to include the url in their initial email. But, the majority don’t. And I’m sure some of them aren’t quite sure how to copy/paste the url in the first place.

I needed an easy button.

The Idea

I’ve tweeted rants on this topic a few times. In the recent past, @SFDC_SteveMo sent me a link to a button he created that executed some JavaScript to compose a new email that automatically contained a link to the record.

It was a great idea, but unfortunately since it uses the standard mailto links, it opens the email in Outlook.


We use Google Apps for the Enterprise and I wanted the button to compose a new message to a specific email address (that we use for requests like this) in Gmail and fill in the Record Id. I also wanted to populate the subject line.

I don’t really know much about JavaScript, but after a bit of research, I found an example that I could hack. I tweaked it a bit and tested… and tweaked and tested… and sighed … and tweaked and tested…

To get my parameters right, I referred to this example. (eg: I discovered that “&SUBJECT” wasn’t passing correctly. The example showed me that I had to use “&su”.)

Finally, I had a working button.


Here’s what happens when I click on it:


Passing the Parameters

In order to pass the recipient address, subject line and default body text, I had to do the following:

Recipient address: ‘&’
Subject Line: ‘&su=My Subject Line’
Body Text: ‘&body=My text.’

To include the link the Salesforce record, I added (location.href), which was noted in @SFDC_SteveMo’s example.

Here’s the code in full:

javascript:popw='';Q='';x=document;y=window;if(x.selection) {Q=x.selection.createRange().text;} else if (y.getSelection) {Q=y.getSelection();} else if (x.getSelection) {Q=x.getSelection();}popw ='' + escape(document.title) + '&su=Question re: Opportunity' + '&' + '&body= I have a question about this Opportunity: ' + escape(location.href) + escape('\n') + escape('\n')  + 'My question is: ' ,'gmailForm','scrollbars=yes,width=680,height=510,top=175,left=75,status=no,resizable=yes');if (!document.all) T = setTimeout('popw.focus()',50);void(0);

I currently have the button as a Detail Page Link. I may change it to a Detail Page Button in the future.


Can you think of another way to use JavaScript with a custom button? Do share! (leave a comment below).


Jenna Baze is a rather geeky girl with average social skills and an affinity for: all things Google, Remember the Milk,, Inbox Zero, Etsy, natural light photography and finding creative ways to avoid doing dishes (which includes updating a handful of blogs).
Twitter: @RatherGeeky
Facebook: Rather Geeky Tips


  1. Great idea, Jenna! Now if only there was a workaround like this for Outlook users :(

  2. Hey Tyeson: The original idea from @SFDC_SteveMo *does* work for Outlook users. That's why I had to tweak it!

  3. Lol. I just skimmed your post quickly, looked at the images. Heck, I'm as bad as an end-user reading email instructions from their admin! I will try SteveMo's solution. Thanks!

  4. how is "email address"?subject

    different to

    "email address"&su ????

    in a mail-to link &su is producing warning message when opening for gmail but not for thunderbird or outlook

  5. @ Anonymous: I'm not sure why they are different, but that's just the way it is. It took some experimenting and looking at this example to realize that &SUBJECT didn't work... but &su did. I tested this method in gmail only.... perhaps if you shared what your warning message is, I'd be able to help.

  6. Thank you so much! Your post helped me.


Related Posts Plugin for WordPress, Blogger...