+ Start a Discussion
T-HanT-Han 

Task/Activity Formula

Scenario: I have Standard Task Object and want to add a Field like Formula or which ever suits to need.

 

Needed Result: The Field must tell if the Task was created in Account or Opportunity or any other object under the Account.

 

Example: 

Task 1

Object Label(Field) : Account(Object Label)

 

Task 2

Object Label(Field) : Opportunity(Object Label)

 

Please advice if there is an alternate work around or if there is something I am missing..?

 

Best Answer chosen by Admin (Salesforce Developers) 
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

You can do this by using trigger in the Task object. But you cannot use a formula field to do this kind of requirement. Because, you cannot create a formula field to check whether the whatID of task is OpportunityID or AccountID.

 

To achieve this using trigger, do the following steps,

  • Goto Setup>Customize>Activities>Activity Custom field.
  • Click on New field & create a text field.
  • To create a task trigger, Goto Customize>Activities>Task triggers.
  • Cclick on new one.

Try the following trigger.

 

trigger getRTobjLabel on Task (before insert, before update) {
   for(Task t : trigger.new){
      if(t.whatID != null){    //This condition should be checked before, because we can create a task without RelatedTO
          List<Opportunity> oppList = [select id,name from Opportunity where id =: t.whatID];
          List<Account> accList = [select id,name from Account where id =: t.whatID];
          
          if(oppList.size() > 0){
             for(Opportunity o : oppList){
                t.RelatedToObjectName__c = 'Opportunity'+'('+o.Name+ ')'; //This will give output as Opportunity(<Oppty. name>)
             }
          }
          else if(accList.size() > 0){
             for(Account a : accList){
                t.RelatedToObjectName__c = 'Account'+'('+a.Name+ ')';  //This will give output as Account(<Account name>)
             }
          }
          else{
               t.RelatedToObjectName__c = null;  //If the task is created for other than Oppty.. or Account, then this text field will be empty
          }
      }
   }
}

 

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

 

All Answers

Vinita_SFDCVinita_SFDC

Hello,

 

It is not possible to fetch object name on which Task was created with formula field. For this you will have to write a trigger on task and query on WhatID, like : SELECT WhatId FROM Task

 

After getting what ID, take first three characters of whatID to identify name of the object. Like 001 is for Accounts, 006 for opportunities and likewise for other objects. Now update task field with object name.

 

Refer: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_task.htm

 

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

You can do this by using trigger in the Task object. But you cannot use a formula field to do this kind of requirement. Because, you cannot create a formula field to check whether the whatID of task is OpportunityID or AccountID.

 

To achieve this using trigger, do the following steps,

  • Goto Setup>Customize>Activities>Activity Custom field.
  • Click on New field & create a text field.
  • To create a task trigger, Goto Customize>Activities>Task triggers.
  • Cclick on new one.

Try the following trigger.

 

trigger getRTobjLabel on Task (before insert, before update) {
   for(Task t : trigger.new){
      if(t.whatID != null){    //This condition should be checked before, because we can create a task without RelatedTO
          List<Opportunity> oppList = [select id,name from Opportunity where id =: t.whatID];
          List<Account> accList = [select id,name from Account where id =: t.whatID];
          
          if(oppList.size() > 0){
             for(Opportunity o : oppList){
                t.RelatedToObjectName__c = 'Opportunity'+'('+o.Name+ ')'; //This will give output as Opportunity(<Oppty. name>)
             }
          }
          else if(accList.size() > 0){
             for(Account a : accList){
                t.RelatedToObjectName__c = 'Account'+'('+a.Name+ ')';  //This will give output as Account(<Account name>)
             }
          }
          else{
               t.RelatedToObjectName__c = null;  //If the task is created for other than Oppty.. or Account, then this text field will be empty
          }
      }
   }
}

 

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

 

This was selected as the best answer