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
JJJenkinsJJJenkins 

Trigger to populate concatenated string

I have two custom objects.

 

Order and Assigned

 

Assigned has a lookup to order and a lookup to the user.

 

On the order object have a text area field I want to populate a list of assignees from the assigned object user lookup.

 

How do I do this?

 

Thanks,

stcforcestcforce

Your requirement is expressed a little ambiguously (or i'm really tired). I am a little uncertain whetehr you want to populate a list of assignees with the text field value stored on the parent or populate the test field on the parent with some data extracted from a list of related assignees.

 

If you want to populate a child based on the parent, then define a workflow or trigger on the child to use the lookup relationship. If you want to populate the parent with details from the children, then create a trigger on the parent to query the children and iterate across them to find some value to fill in some field.

 

nb: If your two objects have existing instances, then you may want to run some code to find records and perform the necessary updates to have the workflow/trigger fire and place objects in desired state.

JJJenkinsJJJenkins

Thanks - sorry for the lack of detail.  I wrote it while really tired.

 

Order is the parent and Assigned in the child.

 

They are not connected with a master-detail they are just connected via lookupfield.

 

On the assigned object there is a field with a look up to the user.  For every assigned record I want to take the user look up name and concantenate all of the "assignees" and populate a text field on the parent object "Order".

 

 

JJJenkinsJJJenkins

Here is what I have but it is just returning null values at this point.  I can't figure out why.

 

trigger MultiAssignPO on Assigned_To__c (after insert, after update) {
   set<id> PoIds = new set<id>();
   
   //lets get all of the IDs of the POs we will work with
   for (Assigned_To__c AsToObj :Trigger.new)
       {
           PoIds.add(AsToObj.Purchase_Order__c);
       }
   //now lets get a list of POs we will work with
   List<Purchase_Order__c> relatedPOs = [SELECT id, Assigned_To_Multi__c
                                            FROM Purchase_Order__c
                                            WHERE id in :PoIds]; 
       //iterate over the list of POs                                     
       for(Purchase_order__c p :relatedPOs)
           {
               string name ='';
               
               //iterate over the assigned to
               for(Assigned_To__c a :trigger.new)
                   {
                       //now we check if the assigned to belongs to the PO we are iterating over in the first loop, if it is, add it to the string name
                       if(a.Purchase_Order__c == p.id)
                           {
                               name+=a.Assigned_To__r.name + ' ; ';
                           }
                   }
                //set the value of the multi-assigned in the PO   
                p.Assigned_To_Multi__c = name;   
           }
           
           //make a DML statement to update the POs
           update relatedPOs;

}

 

stcforcestcforce

correct me if i'm wrong, but won't this overwrite the entries in the parent object for those children that are not in the currently updated set? Don't you need to iterate across the entire collection rather than just the set that are being inserted or updated at that point.

 

stcforcestcforce

1) i meant that having the complete set of parent ids, search for all children with matching parent ids, rather than what you are using, which is a  subset.

2) consider using a map based implementation. It would read more cleanly and should be fairly optimised for searching (that is, not too much additional processing expense).

3) add debug statements, examine the various states

4) i can't believe you need this functionality - usually people use a related list on the page layout or build the code that calculates the string into the VF controller that displays the object.

5) you can modify it so that if the trigger is an insert, just append the name rather than having to do a more extensive search of all children for that parent.