+ Start a Discussion

Copy related list as string into text field on parent?

I have a situation where I need to report on two child objects on Accounts.


Is it possible to copy all the values of a related list as a text string and paste into a text field on the parent object?


For example, Commitment object has a lookup relationship to Accounts.  So I want to create a field on Accounts called Commitment list that looks like this:


Commitment Name 1 - Commit. 1 $ amount;

Commitment Name 2 - Commit 2 $ ammount;

Commitment Name 3 - Commit 3 $ ammount;



Where commitment name and $ amount are two fields on each related list record.


I was thinking I could use a trigger that's fired when a related list is added, deleted, or edited to update the text field.  I just don't know how to convert the list to a text string in the trigger.


Any ideas how I can do this?  This is a very urgent need for my org.


Thanks again!

Bhawani SharmaBhawani Sharma

Just loop through the list and create single string for all the related records. like:


for(Contact contact : [Select Name from Contact where AccountId = account.Id]}


parentName = parentName + contact.Name;



account.CustomField = parentName;



thanks for the reply.  I can't seem to get it to work though.


I tried to modify your code to suit my needs and keep getting compile errors.  I'm still pretty new to writing triggers.

trigger callParentTrigger on ChildObject__c (after insert, after update, after delete, after undelete) {
  map<id,account> acts = new map<id,account>();
    for(childobject__c o:trigger.new)
      acts.put(o.account__c,new account(id=o.account__c));
    for(childobject__c o:trigger.old)
      acts.put(o.account__c,new account(id=o.account__c));
  update acts.values();

Then, for the parent:


trigger listChildren on Account (before update) {
  for(Account a:[select id,(select id,name,amount__c from childobjects__r) from account where id in :trigger.new]) {
    a.ChildSummary__c = '';
    for(ChildObject__c o:a.Childobjects__r)
      a.ChildSummary__c += o.Name + ': $' + String.valueOf(o.Amount__c) + '\n';
Trigger.newMap.get(a.id).ChildSummary__c = a.ChildSummary__c; } }

 This is just a rough translation, but I've tested similar code in my test org just now, and it works like a charm.


ChildObject__c: The name of the custom child object.

ChildObject__c.Account__c: The custom relationship name to the parent object.

ChildObject__c.Amount: A currency field, as an example.

Account.ChildObjects__r: The custom relationship name to the child object (set in the Lookup field's edit page).

Account.ChildSummary__c: A text area or long text area (but not a Rich Text Area) that will store the requested text. Ideally this would be read-only in field level security and/or page layout, as appropriate. It doesn't technically matter, but you may get a user naive enough to ask why, when they edit this field over and again, the data doesn't seem to save...


This code should scale pretty well with minimum overhead, although you should be aware that an account with a ton of records might get bogged down during each save, so you should consider sorting by some criteria and limiting the query to a reasonable number of entries.