Find your content:

Search form

You are here

How do i specify a formula field in task to display mobile

 
Share

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?


Attribution to: Prady

Possible Suggestion/Solution #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.


Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #2

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;

}

Attribution to: techtrekker

Possible Suggestion/Solution #3

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.


Attribution to: abdn
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4334

My Block Status

My Block Content