-1

I have a Parent Object let's call it X__c and I have 4 child objects Ax__c, Bc__c, Cc__c, Dc__c I'm trying to create a List of Wrapper class out of the following data structure: enter image description here

The above data structure would be saved in the following collections:

Map<X__c, List<Ax__c>>
Map<X__c, List<Bc__c>>
Map<X__c, List<Cc__c>>
Map<X__c, List<Dc__c>>

Wrapper Class:

public wrapclass {
    X__c parent;
    Ax__c childA;
    Bc__c childB;
    Cc__c childC;
    Dc__c childD;
}

I want to save all the combinations of all the child records related to the same parent record in a List<wrapclass>

So at the end of it I want my List<wrapclass> to look like this:

        wrapclasslist = [
    {
        parent = X__c,
        childA = Ax__c -1,
        childB = Bx__c - 2
    },
    {
        parent = X__c,
        childA = Ax__c -2,
        childB = Bx__c - 2
    },
    {
        parent = X__c,
        childA = Ax__c -2,
        childB = Bx__c - 1
    },
    {
        parent = X__c,
        childA = Ax__c -1,
        childB = Bx__c - 1
    }
];

I know this can be done using nested for loops but, I'm looking for a more optimized solution. Any help is appreciated

  • Can you explain the business case you're trying to solve? If it's just to save the records, what you have setup can be simplified. Without knowing why you need the wrapper, it's hard to provide further support. – techbusinessman Feb 19 '18 at 1:32
  • @techbusinessman it's similar to this: salesforce.stackexchange.com/questions/8899/… but, I don't want to use nested for loops – d_k Feb 19 '18 at 4:33
0

Design your wrapper class as

public wrapclass {
    X__c parent;
    List<Ax__c> childAList;
    List<Bc__c> childBList;
    List<Cc__c> childCList;
    List<Dc__c> childDList;
}

So Here you will tag multiple child records with same parent and can use simple nested for loop to iterate and get value from them.

  • Hi, thanks for your answer although my requirement is such that I need it to be one object of each child record in the wrapper class. – d_k Feb 16 '18 at 10:19
  • @d_k then your code looks ok. Are you facing any issue? – Tushar Sharma Feb 16 '18 at 11:22
  • I have edited my question to make it more clear please take a look at it. Thanks! – d_k Feb 16 '18 at 12:47

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.