Both casosTrigger
and casosHijos
are Lists. The lists themselves do not have an Id. Even if they did, since they can contain multiple SObject records, which Id would it be?
A more appropriate choice here would be to use a Map. Maps allow you to correlate two pieces of information together. In your situation, I think a Map<Id, List<String>>
would be appropriate. The key of the map would be the Id of your triggered cases, and the value would be the list of all of its child case numbers.
Building that map would look like this
Map<Id, List<String>> parentCaseToChildCaseNumbers = new Map<Id, List<String>>();
// Your goal here is to find child cases, so you want to find cases where
// the _parent_ id is one of the cases in the trigger context variable
for(Case c :[SELECT Id, ParentId, CaseNumber FROM Case WHERE ParentId IN :trigger.newMap().keySet()]){
// A short and efficient way to populate maps is to check to see if the
// map already contains our target key.
// If it doesn't, add the key to the map with an empty value.
if(!parentCaseToChildCaseNumbers.containsKey(c.ParentId)){
parentCaseToChildCaseNumbers.put(c.ParentId, new List<String>());
}
// Because of the previous step, there will _always_ be something in the map
// that we can use (no need for an "else" block or a null check)
parentCaseToChildCaseNumbers.get(c.ParentId).add(c.CaseNumber);
}
Your job after that point is to simply loop over trigger.new
, and check if parentCaseToChildCaseNumbers.get(triggerCase.Id)
is null.
If it is null, then you want to set the Description to "Caso sin hijos".
If it's not, then you can easily add all of the child case numbers using String.join()
(which takes 2 arguments. 1) A list of strings. 2) The string you want to use to separate each string in the first list).
That is, if you have a List<String> myList = new List<String>{'a', 'b', 'c'};
String.join(myList, ', and ')
would output 'a, and b, and c'
for(Case caso :trigger.new){
List<String> numerosDeCasosHijos = parentCaseToChildCaseNumbers.get(caso.Id);
if(numerosDeCasosHijos != null){
// The list is not null, so combine its contents
caso.Description = String.join(numerosDeCasosHijos, ', ');
}else{
// The list is null, so use some default text instead of the list
caso.Description = 'Caso sin hijos';
}
// Another way to do this would be to use the ternary operator
// result = <boolean condition> ? <expression if true> : <expression if false>
//caso.Description = numerosDeCasosHijos != null ? String.join(numerosDeCasosHijos, ', ') : 'Caso sin hijos';
// Yet another way is to use one value as a default, and then
// update the value if needed.
// Saves a little bit of typing
//caso.Description = 'Caso sin hijos';
//if(numerosDeCasosHijos != null){
// caso.Description = String.join(numerosDeCasosHijos, ', ');
//}
}
Remember
To take advantage of a "before" trigger's ability to update records without using DML, you need to make your changes to the instances of the records contained in either Trigger.new
or Trigger.newMap
.
Also
This trigger may not be of much use. If you have child cases related to a parent case, they can be displayed on the parent case's record detail page through a related list (which you can modify to display the case number).