URL Hack — 19/11/2016

URL Hack

Speed and efficiency are very important to a business.

In Salesforce, if a user wants to send a quick email to a lead via a predefined template, here is the long list of steps:

  1. Open lead page
  2. Click on “send an email” button
  3. In the new page, click “select template” button
  4. Pick the desired email template in a new pop up
  5. Possibly also need to change from user to an organisational email address

Is there a way to shorten the process and cut step 3 – 5 ?

Certainly, we can build a custom button on the record detail page which automatically fills all necessary fields in send email page for us.

Here is how it’s done:

Firstly, find the email template id from the URL https://<instance>.salesforce.com/<your email id>.

Then, (I use Google chrome browser) go to the standard send email page and right click on the from field, choose “inspect”.screen-shot-2016-11-19-at-3-35-20-pm

Exam the highlighted id in the browser console (in this case we have ID p26). screen-shot-2016-11-19-at-3-37-10-pm

Expand the selection and find the desired email address and copy the whole value for that option. screen-shot-2016-11-19-at-3-37-19-pm

By now we have got all the building blocks we need to hack the URL.

The last step is to build the custom URL for our button. We can build this by modifying the current page URL (send email):

Simply change https://<instance&gt;.salesforce.com/_ui/core/email/author/EmailAuthor to https://<instance&gt;.salesforce.com/_ui/core/email/author/EmailAuthor?p26=<the value you collect above>&template_id=<template id you copied from your template url> .

Done.

Try hitting enter, see the same page reloads with required fielded prefilled?

We can also extend the power of this method by including more ‘ids’ in the url, for example to auto CC someone in your company (&p4=henry@epic.com for example).

That’s it for today, hope this tutorial inspires you.

 

Lead Convert — 13/11/2016

Lead Convert

Today, let’s talk about lead conversion.

Say what? Lead conversion is a standard feature, is there something new? Affirmative.

  • Salesforce comes with an out of the box solution for field mapping, but what about lead’s related objects mapping?
  • Salesforce provides standard object generation after lead convert, but what if we want to create more than the standard accounts, contacts, and opportunities?

The answer is Apex Trigger.

The problem we want to solve:

  1. if a tick box in the lead page layout is ticked, some other type of opportunities should also be created in addition to the standard opportunity.
  2. all lead’s related objects need to be attached to the newly generated account.

Let’s go straight into the solution:

List<Opportunity> newOpps = new List<Opportunity>();
Map<ID, ID> leadToAccIdMap = new Map<ID, ID>();

for (Lead l : Trigger.New){
Lead oldLead = Trigger.oldMap.get(l.ID);
// only care about converted leads
if (l.isConverted == false) {continue;}

if (l.some_check_box__c == true){
Opportunity newOpp = new Opportunity(
recordtypeid = 'Some ID',
AccountId = l.ConvertedAccountId,
StageName = 'Closed Won',
CloseDate = Date.Today(),
name = 'Some Name',
);
newOpps.add(newOpp);
}
leadToAccIdMap.put(l.id, l.ConvertedAccountId);
}
insert newOpps;

// now the related objects
List<someObject__c> es = [
select id, leadId, accountId
from someObject__c
where leadId in: leadToAccIdMap.keySet()
];

for (someObject__c e : es){
e.accountid = leadToOppIdMap.get(e.leadId);
}
update es;

Here we have the working solution, but what about the test class? How do we easily test a lead convert operation?

It’s great that Salesforce does provide this for us already:

Database.LeadConvertResult lcr = Database.convertLead(lc);

That’s it, again hope this tutorial is helpful for you.

Start with something simple — 11/11/2016

Start with something simple

The very first ‘technical’ blog doesn’t need to be something complicated right?

I think I might just start with one of the very first problems I had – displaying an account’s contact details in the page layout for a custom object.

Few things I considered before making any changes to the org:

  • What’s the relationship like?
  • Anything salesforce already provided?
  • Visualforce page for sure
  • Controller needed ?

I have tried several approaches including writing complicated SOQL queries to find related contacts to the account (join queries).

However, halfway through the implementation, I realized this could be a common problem for other companies as well and decided to ask our best friend, Google .

Let’s skip the talk and go straight into the solution:

<apex:relatedList list="Contacts" subject="{!xxx__c.account__c.Id}>
<apex:relatedList>

That’s it! I never knew the solution can be as elegant as only 2 lines of code.

This gives the ideal result, but when clicking on the related list inside the page, contact page will actually be opened within the little window within the current page.

That’s a bit annoying hey? Luckily, this can be easily resolved by adding

<base target="_blank" />

to the page.

That’s all for today, hope this article is a good starting point and is also helpful.

First blog post ever — 09/11/2016