Home > Blockchain >  Salesforce - How to split list in Apex
Salesforce - How to split list in Apex

Time:07-31

I have a list of a custom object Product like this: Product:[type=fruit, productValue=apple], Product:[type=fruit, productValue=pinapple], Product:[type=vegetable, productValue=potato],Product:[type=vegetable, productValue=carrot]

I want to obtain two lists due to the first value (type=fruit/vegetable), in Apex WITHOUT using a for cycle. Expected result: fruitList=['apple','pinapple'] and vegetableList = =['potato','carrot']

How can I split them?

Thanks

CodePudding user response:

I suggest you can create a Wrapper class for Product with two variable type and productValue with String data type, as written below -

public class Product{
    String type;
    String productValue;
}

Then use the Wrapper instance to get the list of type and productValue easily with the help of JSON.serilize() and JSON.deserilize()

Please, let me know if it helps you out or not.

Thanks.

CodePudding user response:

There is no way to split them without a loop. Unless you do it before you obtain the list. I would suggest either 2 queries to get the list of products, or just do the loop to split them up. Depends on where you are with governor limits.

  • Related