I need help to get the unique names from the child object records and concatenate them on the text field on the parent object. I was able to concatenate but unable to unique the values. As an example, if there are 4 child records (apple, mango, mango, apple) to the parent record. I want to update as apple; mango on the parent object field.

list<id> parentIDstoUpdate = new list<id>();
        if(Trigger.isInsert || Trigger.isUpdate ){
            for (ChildObject__c co : Trigger.new) {

    map<id,string>  mapParentIdtoString = new Map<ID,String>();
    string str;

    for (ChildObject__c co: [SELECT Fruit__c, ParentId FROM ChildObject__c WHERE ParentId =:parentIDstoUpdate ]){
            str = mapParentIdtoString.get(co.ParentId);
            str = str + '; ' + co.Fruit__c;  

    list<ParentObject__c> recordstoUpdate = new list<ParentObject__c>();
    for (ParentObject__c p: [SELECT ID, Parent_Field__c FROM ParentObject__c WHERE id =:recordstoUpdate ]){ 
        p.Parent_Field__c = mapParentIdtoString.get(p.id);

    if (recordstoUpdate != null && recordstoUpdate.size() >0){
        update recordstoUpdate;
  • 2
    Please do not delete and re-post your questions, but rather edit the original. The former is abusive behavior and may reflect negatively on your site participation if it becomes a pattern. The latter demonstrates an effort to contribute quality content. – Adrian Larson Feb 13 '18 at 20:41

You should be able to use a set for something like this.

When you're looping through your child objects:

Set<String> uniqueFruits = new Set<String>();

for (SObject record : recordList) {

The set will automatically de-dup the strings. Then add them to the parent object:

String textField = '';

for (String s : uniqueFruits) {
    textField = textField + s + ',';

textField = textField.left(textField.length() -1); // Remove the last comma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.