what am I doing wrong so that it only displays the new role? multiple roles should only be displayed if multiples are selected in the muli-select picklist. also, is it possible to have a space appear after the semicolon when multiple roles are selected?

  • Welcome to SFSE! Could you please edit your post to ask a specific question? (See How to Ask). We can't help you if we don't know what's wrong. – David Reed Sep 17 at 21:30
  • sorry about that, @DavidReed! – testing Sep 17 at 21:34
  • This is starting to get into "this should be a new question" territory. You didn't mention anything about an end date in the initial question (which as I understood, was simply about getting the multipicklist values from all child records and aggregating them on the parent record). In your new question, it'd help if you went into some more detail about the "selection" and "end date" that you're talking about. Providing some examples (e.g. parent 1 => child1, child2, child1 roles = a;b, child2 roles = b;c, parent should have a;b;c) would also help. – Derek F Sep 18 at 22:03
  • One more note, when working with child records in Salesforce, it's always a good idea to plan what should happen if there are multiple child records for a given parent (and how they should interact / what the expected result should be on the parent record) – Derek F Sep 18 at 22:04
  • ok my apologies, i'll ask as a new question – testing Sep 18 at 23:22

Seems like you're on the right track to me. A few things to note:

  • When you're querying (and later accessing) data on a related record, you use __r at the end instead of __c
  • Specifically for parent-child subqueries, you use the child relationship name. This can be found by a few different methods (apex, looking at the relationship field on the child object, etc...), but is typically the plural of the object name (e.g. Role_Histories__r)
  • multipicklist fields have their values separated by semicolons (not commas, at least not as far as I know)

As for what your code is missing to accomplish your goal...

Usually, when you have nested data like this, you'll need a nested loop to actually process things.

A multipicklist field can have values repeated (e.g. opt1;opt2;opt2;opt2;opt3), but I'm not sure if duplicate values are persisted or not. To me, it would feel better to not put duplicate values in in the first place.

My preferred way to handle situations like this are to use a Set<String> to make sure there aren't any duplicates, then use String.join(list, separator) to stitch everything together.

A quick example:

for(Object1__c parent :parentList){
    Set<String> valuesSet = new Set<String>();

    for(Object2__c child :parent.children__r){

    // String.join() takes a list, not a set
    // Thankfully, we can pass a set into the list constructor
    String result = String.join(new List<String>(valuesSet), ';');

    parent.field__c = result;
  • thanks for the reply! where should i be adding this example into my trigger? i'm not able to check and play around with it until tomorrow but i will definitely be trying again given this great info! also, i did mean to say semi-colons, not commas. thanks again i'll try this out tomorrow. – testing Sep 17 at 22:52
  • @testing Working through things logically... the first half (or so) of your trigger is setting up your query for parentReps (good job avoiding a query inside a loop, by the way!). You need the update parentReps.values(); at the end. Thus, this would take the place of your final for loop. – Derek F Sep 17 at 23:33
  • going to update my initially question with my updated loop now. i'm a little confused on whether i labeled all my variables and what not right. i'm also getting an error on line 17 saying the variable 'parentList' doesn't exist, could you look it over for me? thanks! – testing Sep 18 at 15:17
  • @testing My example wasn't meant to just be copy/pasted into place. You'll need to make adjustments to it to fit with what you have. For example, parentList => parentReps.values(). – Derek F Sep 18 at 15:20
  • please see my edit, the code is working almost perfect. would appreciate your help! – testing Sep 18 at 20:57

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.