4

I want to show a mobile on the tasks. So i thought of creating a formula field on tasks, But i am not sure on how do i set the formula as the task could be related to a contact or lead.

How do i set it based on the WhoId so if a lead is selected then the mob of the lead is displayed or if a contact is selected then the mobile of contact is displayed?

6
  • Is mobile your custom field ? Commented Nov 13, 2012 at 4:54
  • 1
    yes.. its a custom field
    – Prady
    Commented Nov 13, 2012 at 6:08
  • Since its a custom field you can create a formula field that picks value from your custom field and it will pick up the value directly from the task object inspite of contact or lead.Also i assume that Mobile your custom field is on Task. Commented Nov 13, 2012 at 6:32
  • If you have a field on contact or lead you can go for a trigger.After Winter 13 release we can get the sObject based on Id. Commented Nov 13, 2012 at 6:35
  • 2
    "Mutant fields" like WhoId (or in many cases even record's OwnerId which can point to User or Queue) don't work nicely with formulas. Salesforce will let you use only fields that surely exist on all possible lookup targets - pretty much only Id and Name. So some coding solution will be needed - either trigger as @MohithKumar suggests or some field prepopulation in the new Task's URL...
    – eyescream
    Commented Nov 13, 2012 at 7:14

3 Answers 3

5

You cannot reference polymorphic lookup fields (WhoId & WhatId) in a formula because they could be pointing to one of many possible entities.

You will need a before insert, before update trigger to query for those WhoIds, query for the Mobile and then set it.

Trigger TaskBefore on Task(before insert, before update){
    Map<Id, List<Task>> whoIds = new Map<Id, List<Task>>{};

    For (Task t : trigger.new)
        If(t.WhoId != null){
            List<Task> tasks = whoIds.get(t.WhoId); //this should be t.WhoId (not task.WhoId)
            If (tasks == null){
            tasks = new List<Task>{};
            whoIds.put(t.WhoId, tasks);
        }
        tasks.add(t);
    }

    For (Lead ld : [Select Id, Name, Mobile from lead where Id in :whoIDs.keySet()])
        For(Task t : whoIds.get(ld.id))
            t.Mobile__c = ld.Mobile;

    For(Contact con : [Select Id, Name, Mobile from Contact where Id in :whoIds.keySet()])
        For(Task t : whoIds.get(con.id))
            t.Mobile__c = con.Mobile;

}
1

If you have a field on contact or lead you can go for a trigger.After Winter 13 release we can get the sObject based on Id.

Here is an example

Id accid=[Select Id  from Account Limit 1].ID;
Schema.Sobjecttype inspecttype=accid.getSObjectType();
system.debug('Object Token '+inspecttype);

From the whoId of Task you can determine the sObject Type and then from the sObject you can pull the custom field value and populate on new field on task.

0

The accepted answer above works when Task is created or Updated BUT suppose a Task created/updated 3 days ago and today someone change the field value on Contact. The new value will not be populated on Task. It will hold wrong information. So I think, to replicate formula field functionality, a trigger on Contact will also be required.

Or another workaround is to create a Contact lookup field on Task and populate it with WhoId if WhoId is related to Contact. Then you can create a formula field on this new Contact lookup field.

You must log in to answer this question.

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