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
DeepthisfdcDeepthisfdc 

Problem with the Task Trigger

Hello Everyone,

 

I am new to the Salesforce development.Below is the trigger :

 

trigger trgUpdateComments on Task (before update)
{          
    DateTime d = system.now();
    for(task ta : trigger.new)
    {
        string dec = String.valueof(d) + '\n' + ta.Description ;
                ta.Description = dec;
         
    }    
}

 

What it does is that: whenever we enter any comment in the comments field on the tasks and save it.It will display the system date and time followed by the comment entered as below:


2012-09-27 10:25:41

Testing1

 

The problem is whenever I enter the second comment the format is as below:

 

2012-09-27 10:27:23

2012-09-27 10:25:41
Testing1

Testing2

 

But it should be as:

2012-09-27 10:27:23

Testing2

2012-09-27 10:25:41
Testing1

 

Can someone help me with this please.Thank you!!

 

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

To me this seems as though the new description is being place below the old description. If the user had put their new description at the top, your trigger should work as you currently have it.

 

IE:

Original:

     2012-09-27 10:25:41

     Testing1

 

User updates and puts:

     2012-09-27 10:25:41

     Testing1

     Testing2

 

Rather than :

     Testing2

     2012-09-27 10:25:41

     Testing1

     

The last example is how you want users to post their task description.... they are doing example #2 currently

All Answers

Alex.AcostaAlex.Acosta

To me this seems as though the new description is being place below the old description. If the user had put their new description at the top, your trigger should work as you currently have it.

 

IE:

Original:

     2012-09-27 10:25:41

     Testing1

 

User updates and puts:

     2012-09-27 10:25:41

     Testing1

     Testing2

 

Rather than :

     Testing2

     2012-09-27 10:25:41

     Testing1

     

The last example is how you want users to post their task description.... they are doing example #2 currently

This was selected as the best answer
DeepthisfdcDeepthisfdc

Thank you very much for the reply.I also tried the way you explained.But what should we do if the user enters the comment at the end of the comment box.What all changes can be made to the code?Please let em know. Thank you once again!!

Alex.AcostaAlex.Acosta

if that happens then you can compare your results from your record prior. I suggest looking at trigger.oldMap and pulling back your old value. You would have to think of a creative way to strip out the new information.

 

Example off the top of my head without really thinking it through and in sudo code:

 

String description = task.description; // <-- this will be based on the trigger.new value

 

String newlyAddedDescription = description.replace(trigger.oldMap.get(task.Id), ''); // this should replace our old value with blank and leave the new information

 

Task.Description = String.valueOf(system.now()) + newlyAddedDescription + trigger.oldMap.get(task.Id); // or something like that 

 

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm