+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Update Parent with comment field from every child record Class

Hello,
I am familiar with updaing a Parent record with a field from a child but not sure how to update the parent with a vlaue from each child.
There is a comment__c field on each Task__c record.
If there are 3 task associated to the Parent(export__c) I need the comment field on the Parent to house the comment from each child
For example:
task 1 comment = test for price
task 2 comment = ask for quote
task 3 comment = sending invoice

The commen ton the parent needs to look llike
test for price
ask for quote
sending invoice

No sure how to grab the values and then force a line break between each.
The relationship between the 2 objects is a lookup.

I would like to add this method into an existing class 

Thanks,
P


 
Best Answer chosen by Phuc Nguyen 18
ayu sharma devayu sharma dev
Hello 

Please try the below code:
 
public class TaskTriggerHelper{

    public static void getTasksCommentOnExp(){

        Set<Id> expIds = new Set<Id>();
        List<Task__c> taskList = Trigger.isInsert || Trigger.isUpdate ? Trigger.New : Trigger.Old;
        List<export__c> expToUpdate = new List<export__c>();

        for( Task__c tk : taskList ){
            expIds.add( tk.export__c /*Lookup field value of Export__c*/ );
        }

        for( export__c exp : [ SELECT Id, comment__c, 
                                ( SELECT comment__c FROM Tasks__r ) FROM export__c WHERE Id IN: expIds ] ){
            String expComments = '';
            for( Task__c tk : exp.Tasks__r ){
                expComments += tk.comment__c != null ? tk.comment__c+'\n' : '';// Adding Task Comment if its not null
            }             
            if( expComments != null && expComments.endsWith('\n') ){
                expComments = expComments.removeEnd('\n');// Removing the last line breaker
                exp.comment__c = expComments;
                expToUpdate.add( exp );
            }       
        }
        if( expToUpdate.size() > 0 ){
            update expToUpdate;
        }

    }

}
 
trigger Trigger_Task on Task__c (after insert,after update, after delete) {

    if( Trigger.isInsert || Trigger.isAfter || Trigger.isDelete ){
        if( Trigger.isAfter ){
            TaskTriggerHelper.getTasksCommentOnExp();
        }
    }

}

You don't need to clear the field separately as we are already declaring variable expComments with a blank value and assigning it to Comments__c after adding values to it. 

Please try the above code. Let me know if any problem arises.

Regards
Ayush Sharma

All Answers

ayu sharma devayu sharma dev
Hello P

I have made a code for you, try to adjust this inside your existing code.
 
List<export__c> expToUpdate = new List<export__c>();

for( export__c exp : [ SELECT Id, comment__c, 
                        ( SELECT comment__c FROM Tasks__r ) FROM export__c ] ){
    String expComments = '';
    for( Task__c tk : exp.Tasks__r ){
        expComments += tk.comment__c != null ? tk.comment__c+'\n' : '';// Adding Task Comment if its not null
    }             
    if( expComments != null && expComments.endsWith('\n') ){
        expComments = expComments.removeEnd('\n');// Removing the last line breaker
        exp.comment__c = expComments;
        expToUpdate.add( exp );
    }       
}
if( expToUpdate.size() > 0 ){
    update expToUpdate;
}

If any problem arises let me know.

Ayush
 
Phuc Nguyen 18Phuc Nguyen 18

Hello Ayush,
Should this be in a before or after update? 
I am assuming an update since the comment is enered after the Task is created? 
Do I need to clear the comment field on the parent record before inserting the new comment(s)?
I am assuming this will also work in an after delete? 

Thanks,
P

ayu sharma devayu sharma dev
Hello 

Please try the below code:
 
public class TaskTriggerHelper{

    public static void getTasksCommentOnExp(){

        Set<Id> expIds = new Set<Id>();
        List<Task__c> taskList = Trigger.isInsert || Trigger.isUpdate ? Trigger.New : Trigger.Old;
        List<export__c> expToUpdate = new List<export__c>();

        for( Task__c tk : taskList ){
            expIds.add( tk.export__c /*Lookup field value of Export__c*/ );
        }

        for( export__c exp : [ SELECT Id, comment__c, 
                                ( SELECT comment__c FROM Tasks__r ) FROM export__c WHERE Id IN: expIds ] ){
            String expComments = '';
            for( Task__c tk : exp.Tasks__r ){
                expComments += tk.comment__c != null ? tk.comment__c+'\n' : '';// Adding Task Comment if its not null
            }             
            if( expComments != null && expComments.endsWith('\n') ){
                expComments = expComments.removeEnd('\n');// Removing the last line breaker
                exp.comment__c = expComments;
                expToUpdate.add( exp );
            }       
        }
        if( expToUpdate.size() > 0 ){
            update expToUpdate;
        }

    }

}
 
trigger Trigger_Task on Task__c (after insert,after update, after delete) {

    if( Trigger.isInsert || Trigger.isAfter || Trigger.isDelete ){
        if( Trigger.isAfter ){
            TaskTriggerHelper.getTasksCommentOnExp();
        }
    }

}

You don't need to clear the field separately as we are already declaring variable expComments with a blank value and assigning it to Comments__c after adding values to it. 

Please try the above code. Let me know if any problem arises.

Regards
Ayush Sharma
This was selected as the best answer