9

I created a formula field to know the created task is for contact or opportunity or lead. So I tried to create a formula field Based on whatId & whoId.

But it throws a syntax error. Can anyone tell me where I have been going wrong

For reference:

IF(
BEGINS(WhatId, “006”), "Opportunity",

IF(
BEGINS(WhatId, “001”), “Account”,

IF(
BEGINS(WhoId, “00Q”), “Lead”,

IF(
BEGINS(WhoId, “003”), “Contact”,””))))

2 Answers 2

11

Assuming your formula was copied directly from salesforce.com, the problem has to do with "Smart Quotes." Salesforce formulas don't recognize and as actual quotation marks. Try using the following:

IF(
BEGINS(WhatId, "006"), "Opportunity",

IF(
BEGINS(WhatId, "001"), "Account",

IF(
BEGINS(WhoId, "00Q"), "Lead",

IF(
BEGINS(WhoId, "003"), "Contact",""))))

Coincidentally, you could shorten your formula:

CASE(LEFT(WhatId, 3), "006", "Opportunity", "001", "Account",
CASE(LEFT(WhoId, 3), "00Q", "Lead", "003", "Contact", ""))
2
  • 1
    I am even confused why the OP needs a formula, when you hover over the related to field it does show you the actual sobject with minpage layout view.
    – Rao
    Commented May 16, 2016 at 18:41
  • 1
    @Rao Only in certain contexts, though. You can't do this in a report, for example.
    – sfdcfox
    Commented May 16, 2016 at 18:42
2

Honestly, I think you're better off writing a before trigger. The obvious advantage of the below strategy is that it scales to all objects you can use for these polymorphic lookups. I did check that you can't use What.Type or Who.Type in a formula.

For the Apex approach, you just need to call (using the handler pattern of your choice):

public static void updateWhatType(List<Task> records)
{
    for (Task record : records) record.WhatType__c = getType(record.WhatId);
}
public static void updateWhoType(List<Task> records)
{
    for (Task record : records) record.WhoType__c = getType(record.WhoId);
}
public static String getType(Id value)
{
    return (value == null) ? null : String.valueOf(record.WhatId.getSObjectType());
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .