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
RobBirdRobBird 

Task informatio​n on a Lead

Good afternoon!! I am trying to create an Apex trigger so when someone enters comments into the Task comments section (Description), those comments are also entered into the history comments section (History_Comments__c) on the Lead. I would like it so each time a Task is closed, the comments for that Task are entered onto a new line in the history comments section.

 

So it would look like this in the history on the Lead (the comments would be what is entered onto the Task) :

 

Talked to the borrower, they stated they were not interested

 

Borrower called back, stated they in fact interested in our product

 

Borrower called back again, stated he in fact hates our product and hopes it falls into the ocean

 

I hope this makes sense

 

Thank you for your time!

 

Rob

Jeremy_nJeremy_n

This shouldn't be that difficult. Here's what I would do:

 

 

trigger Lead_recordHistory on Lead (before insert, before update) {
	
	if (Trigger.isInsert) {
		for (Lead l : Trigger.new) {
			if (l.Description != null) {
				l.History_Comments__c = l.Description;
				l.Description = null;
			}
		}
	}
	
	if (Trigger.isUpdate) {
		for (Lead l : Trigger.new) {
			if (l.Description != null) {
				l.History_Comments__c = l.Description + '\r\n\r\n' + l.History_Comments__c;
				l.Description = null;
			}
		}
	}
}        

 This assumes it's okay to blank out the Description field each time. This isn't too bad an idea; if you don't give the user access to edit the History Comments field, this would be a convenient one-look way to see history. Also, just add Userinfo.getName() and system.now() into the string, and you can see who wrote what when.

 

Good luck,

 

Jeremy

 

RobBirdRobBird

That is great! I will try it out. Now to test the trigger, I have what is below, but it does not seem to be working and I am sure I am missing something here :

 

 

 

@isTest

 

Task task = new Task () {

task.Description =

insert

 

'blah blah blah'; task;

insert

}

}

task;

private class CopyLeadInformation { void myTest() {

 

statictestMethod void myTest () {

Task

task = new Task ();

task.WhoId = lead.id;

 

Jeremy_nJeremy_n

When you copy your code over, use the little Code Insert button (has a C on it), that will put it into a formatted box. I can't really tell what your code is doing.

 

Thanks,

 

Jeremy