function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Preethi77Preethi77 

How to track Task

Hi, For opportunity we have OpportunityHistory object to track some fields in opportunity. same as Lead(LeadHistory), case(caseHistory).. But we dont have Task history object for Task..So Please tel me how to track fields in task object....

If any idea would appreciate...

Vinita_SFDCVinita_SFDC

Hello,

 

Task and Events belong to object Activities, so for task history it is Activity History.

sandeep@Salesforcesandeep@Salesforce

This feature is provided by salesforce it self. if you have enable allow history or tracking on any Task object then you can get track it easily. One more thing this is tracked in a related list.

AroraAnupAroraAnup

I believe what you are looking for is the ability to track Field History for Task/Event fields, so that anytime a field value is changed, it is tracked and can be reported on (correct me if I am wrong)

 

Well, natively, Salesforce.com does not offer Field History tracking for Activity Fields. The Activity History table holds all Completed Activities and not the history of field level changes to open/closed activities.

What you CAN do is create a custom object (with the same fields as Activities) and then write a trigger that creates a new record in this object each time a field in the Activities is changed. You can then also report on this object. The key to this solution would be how intelligently the object's data model is designed. Do take note that you cannot link this object to Activities as you cannot create a Lookup on Activities.

 

However, the downside of this solution would definitly be development and ongoing administration, as each time you add a new field to Activities and want to track its changes, you would need to add it to this object as well as to your trigger. So think about this route only if this is a must have requirement for your

 

Hope this helps. Let me know how it goes. Good luck!

Preethi77Preethi77

The Activity History table holds all Completed Activities and not the history of field level changes to open/closed activities...

Yes, obviously correct....you are telling wirte one trigger and whenever Task  insert or update, and one custom object records inserted... yes, i can understand very clearly..Instead of ( write trigger  and create custom object ) if and idea Please post me... 

AroraAnupAroraAnup

I am afraid a custom object + trigger would probably be the only solution, as you want to track each of the field level changes made to the activity fields. Though we can also use custom objects instead of standard activities, but I would NOT recommend that as you will lose on a host of standard functionalities like Calendering, Outlook Sync, Reminders, Workflow Tasks, Pop up alerts and more.

Looks like this would probably be the only way out for you Preethi. Let me know if you need further help!

zachbarkleyzachbarkley

Hi All,

 

The code below will manipulate the Description Field on a Task to always stamp user name and date time to keep a history of this field. See Exmple Below:

 

####### COMMENTS ##########

John Doe - 2/10/2013 1:06 PM
my new comments


John Doe - 2/10/2013 1:02 PM
my first comments

 

Create Apex Class

 

/******************************************************************
Name : Task_HDL_BIBU 
Created By : zachbarkley
Created Date : Oct, 2013
Description : Track Comments Box on Task to record history - Stamps user and datetime
********************************************************************/
public class Task_HDL_BIBU{
    public void OnInsert(List<Task> newRecords,List<Task> oldRec){
        updateRecord(newRecords,oldRec); 
    }
    public void OnUpdate(List<Task> upRec,List<Task> oldRec){
        updateRecord(upRec,oldRec); 
    }
    private void updateRecord(List<Task> upRec,List<Task> oldRec) {
    	/******************************************************************
        DECLARE
        ********************************************************************/
        Boolean IsNew = false;
        String q;
        /******************************************************************
        * IDENTIFY IF OLD RECORDS HAS VALUE
        ********************************************************************/ 
        Boolean NewRecords=false;
        if(oldRec==null){
            NewRecords=true;
        }
        /*** UPDATE DATA *****************************************************/ 
        for(Task R:upRec){
        	/******************************************************************
		    * FUNCTION FOR ONLY NEW RECORDS  
			********************************************************************/ 
			Boolean NewRecordForRow=true;
        	if(NewRecords==false){
	        	for(Task oldRec1:oldRec){
	        		if(R.Id == oldRec1.Id){
	        			NewRecordForRow=false;
	        		}	
		        }  
	        }
        	Boolean CommentsChanged = false;
        	if(NewRecordForRow==true){
        		if(R.Description!=null) {
        			CommentsChanged=true;
        		}
        	}else{
        		for(Task oldRec1:oldRec){
	        		if(R.Id == oldRec1.Id){
	        			if(R.Description != oldRec1.Description){
			        		CommentsChanged=true;
	        			}
	        		}	
		        } 
        	}
        	DateTime Now = DateTime.Now();
			String NowStr = Now.format();
        	
        	Boolean FoundOld=false;
        	if(CommentsChanged==true){
        		if(NewRecords==false){
	        		for(Task oldRec1:oldRec){
		        		if(R.Id == oldRec1.Id){
		        			String DescLatest;
		        			Integer i = (R.Description).indexOf('####### COMMENTS ##########',0);
		        			if(i>0){
		        				List<String> DescList = (R.Description).split('####### COMMENTS ##########', 2);
        						DescLatest = DescList[DescList.size()-2];
		        			}else{
		        				DescLatest = R.Description;
		        			}
		        			
        					List<String> HistoryList = (oldRec1.Description).split('####### COMMENTS ##########', 2);
        					String DescHistory = HistoryList[HistoryList.size()-1];
        					
		        			R.Description = +'.\n\r\n\r\n\r\n\r####### COMMENTS ##########\n\r\n\r'+UserInfo.getName()+' - '+NowStr+'\n\r'+DescLatest+'\n\r\n\r'+DescHistory;
		        			//R.Description = +'.\n\r\n\r\n\r\n\r####### COMMENTS ##########\n\r\n\r'+UserInfo.getName()+' - '+NowStr+'\n\r'+R.Description;
		        			FoundOld=true;
		        		}
			        }
        		}
				if(FoundOld==false){
		        	R.Description = +'.\n\r\n\r\n\r\n\r####### COMMENTS ##########\n\r\n\r'+UserInfo.getName()+' - '+NowStr+'\n\r'+R.Description;
		        } 
        	}
        	
        }
        
    }
}

 

Create Task Trigger

 

trigger Task_BIBU on Task(before insert, before update) {
  Task_HDL_BIBU handler = new Task_HDL_BIBU();    
   if(Trigger.isInsert && Trigger.isBefore) {
    handler.OnInsert(Trigger.new,Trigger.old);
   }else if(Trigger.isUpdate && Trigger.isBefore) { 
    handler.OnUpdate(Trigger.new,Trigger.old);
   }
}

 

agarciaagarcia

If I try to edit a task without a previous comment then I receive this error..

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Task_BIBU caused an unexpected exception, contact your administrator: Task_BIBU: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Class.Task_HDL_BIBU.updateRecord: line 71, column 1

 

 

Any fix for this?