+ Start a Discussion
Austin JRBAustin JRB 

APEX trigger that would copy comments from a Case to the activity history of a Person Account.

I need an APEX trigger that would copy comments from a Case to the Activity history of a Person Account.  Any existing code for this?



Vivek ViswanathVivek Viswanath

Just a suggestion this is a discussion forum I am not sure what you want do you want a readymade trigger?


Vivek Viswanathan

Message Edited by Vivek Viswanath on 08-14-2008 10:13 AM
Austin JRBAustin JRB
I would like to know if it is possible to copy Comments from a Case to the Activity history (completed task) of a Person Account and if so can I get the code for it.

Just out of shear curiosity, and I am genuinely interested, why is this better than just showing the Cases related list on the accounts page layout?  Permissions or something?

The answer, Not easily.  The reason, Triggers are not allowed on the CaseComments object.
If you could settle for only the comments that are left in the "Internal Comments" box of the screen that edits the Case object, then you could make this idea limp along with some ease.  Problem is, that editing/deleting the case comments after the fact would not be supported because the "Internal Comments" textbox only Adds New Case Comments.
If that is ok, then you could use a trigger on the Case object for the After Insert and After Update events.  Then, if the Internal Comments field is not blank, query the CaseComment objects to all of the ones with the same ParentID (caseID) and that do not have a new custom Boolean called something like "ConvertedToActivityHistory" = FALSE.  Then loop through them to create a new ActivityHistory object and set the "ConvertedToActivityHistory" = TRUE. 
This is a little heavy on DML usage as you are limited to 100 DML statements per save of each Case Object (1 Query, 1 Insert, 1 Update) so I hope you don't have cases out there right now with more than say 24 or so comments.  If so, you may have to initially set those CaseComment's "ConvertedToActivityHistory" Boolean = TRUE to prevent them from causing errors.
Is any of this useful to you?
I know you asked for code, but I would bet that this code does not currently exist, so you would have to create it or someone like me would have to create it for you.
Austin JRBAustin JRB

Yes, your answer is very helpful.  Basically, I understand that there is no way to put a trigger on Case Comments. 

Can you explain your question about "showing the Cases related list on the accounts page layout"?  We have Cases on that object, but we cannot see the Case Comments from there, just the Case Activity.  Do you know of a way to display the Case Comments on the Person Account?

Thanks for your help on this

Message Edited by Austin JRB on 08-19-2008 07:40 AM

At the time I wrote that portion I don't think I understood that CaseComments was a child object, so I was thinking that just showing a field on the Cases related list section of the Accounts page layout would have been enough.  I see now that you would have to open the case to see the case comments even if you put the cases related list into your Accounts page layout.

Sorry to get your hopes up. 

Austin JRBAustin JRB
No problem, I just thought you had some trick up your sleeve that I hadn't thought of.
Vivek V.ax312Vivek V.ax312


I have done something similar but we introduced a new custom field that replaces the comments on the case and maintained the history of this field.

Not sure if you are looking to do something similar




I think I may have found a solution for you.  Just not sure how best to incorporate it yet.  I was tinkering around with learning VisualForce and decided to use your problem here as focal point for learning.  Anyway, I'm not sure what you know about visual force but here is the quick version on how you might solve this issue.
  1. Install the package into your organization. (PM me for the link.)
  2. After the package is installed continue the steps below in salesforce.
  3. Click Setup, My Personal Information, Personal Information, Then click the Edit Button.
  4. Check the Development Mode checkbox and click save.
  5. In the Address bar of your browser, delete everything after "...salesforce.com/" and add to the back of it "apex/vfAccounts" and press the enter key. (should look something like this: https://na5.salesforce.com/apex/vfAccounts)
  6. Click on the link that says "Create Page vfAccounts".
  7. You should now see a screen that says "Congratulations This is your new page: vfAccounts"
  8. Click "Page Editor" in the far bottom left of your browser.  This will open up a code editor.
  9. Paste the following code into the code window and save it by clicking on the disk icon.
    <apex:page controller="AccountController" tabstyle="Account">
        <apex:detail relatedList="true" />
        <apex:pageBlock title="Case Comments">
          <apex:dataTable value="{!MyCaseComments}" var="key" cellPadding="4" border="1" width="100%">
            <apex:column width="180px">{!key.LastModifiedDate}</apex:column>
            <apex:column >{!key.CommentBody}</apex:column>
  10. Now you should see that the page is mostly blank with just an empty "Case Comments" box, but believe it or not you are done.
  11. To make it work you must add "?id=XXXX" to the end of the URL where XXXX = an Account ID.  Should look something like this https://na5.salesforce.com/apex/vfAccounts?id=0017000000NXQtG.  This page looks and behaves just like the original account page, but it you look at the bottom of the page you will find all of the related case comments in descending order.  I could have probably made it with link and such, but it's good enough for a simple start.

Here is the part that is missing though... I'm not sure how to make this page take over from the original Account page.  Maybe someone else here would have an idea on that.