+ Start a Discussion
Anshul Kapoor 13Anshul Kapoor 13 

to show cartesian product in apex code

Hi all,

Please someone solve my issue:

I have four fields:
'A' = 54321, 'B' = 8888888, 'C' = 201701, 201702,201703,201704 & 'D' = HI 

The Result, I need to store into the field 'E' = 54321_8888888_201701_HI, 54321_8888888_201702_HI, 54321_8888888_201703_HI, 54321_8888888_201704_HI

In field A, there will  always be 1 element. The E field always has the Permulation of four combination of fields A, B, C & D. For the above case: 1 * 1 * 4 * 1 = 4

For the same case, A = 98765, B = 111111, 222222, 333333, 444444, C = 201705,201706, 201707, 201708, D = HI,PR,ML
The Result into E = 
98765_111111_201705_HI,
98765_111111_201706_HI,
98765_111111_201707_HI,
98765_111111_201708_HI,
98765_111111_201705_PR,
98765_111111_201706_PR,
98765_111111_201707_PR,
98765_111111_201708_PR,
98765_111111_201705_ML,
98765_111111_201706_ML,
98765_111111_201707_ML,
98765_111111_201708_ML,
98765_222222_201705_Hi,
98765_222222_201706_HI,
98765_222222_201707_HI,
98765_222222_201708_HI,
98765_222222_201705_PR,
98765_222222_201706_PR,
98765_222222_201707_PR,
98765_222222_201708_PR,
98765_222222_201705_ML,
98765_222222_201706_ML,
98765_222222_201707_ML,
98765_222222_201708_ML,
98765_333333_201705_Hi,
98765_333333_201706_HI,
98765_333333_201707_HI,
98765_333333_201708_HI,
98765_333333_201705_PR,
98765_333333_201706_PR,
98765_333333_201707_PR,
98765_333333_201708_PR,
98765_333333_201705_ML,
98765_333333_201706_ML,
98765_333333_201707_ML,
98765_333333_201708_ML,
98765_444444_201705_HI,
98765_444444_201706_HI,
98765_444444_201707_HI,
98765_444444_201708_HI,
98765_444444_201705_PR,
98765_444444_201706_PR,
98765_444444_201707_PR,
98765_444444_201708_PR,
98765_444444_201705_ML,
98765_444444_201706_ML,
98765_444444_201707_ML,
98765_444444_201708_ML

The E field has 48 combinations separated by comma. I have shown the result into vertical form for understanding.
Best Answer chosen by Anshul Kapoor 13
Anshul Kapoor 13Anshul Kapoor 13
Hi Gaurav,

For my query the solution is:

for (String VrNr : lstVrNr){
                for (String LvlDsc : lstLvlDsc){
                    for (String WkNr : lstWkNr){
                        for (String GeoLocDsc : lstGeoLocDsc){
                            if (!b){
                                BE.Event_Calculation_Helper__c = BE.Event_Calculation_Helper__c + ', ' + VrNr + '_' + LvlDsc + '_' + WkNr + '_' + GeoLocDsc;
                            }
                            if (b){
                                BE.Event_Calculation_Helper__c = VrNr + '_' + LvlDsc + '_' + WkNr + '_' + GeoLocDsc;
                                b = false;
                            }
                        }
                    }
                }
            }

Please let me know for any queries.

All Answers

Anshul Kapoor 13Anshul Kapoor 13
Please someone provide the solution to achieve to the desired result.
GauravGargGauravGarg

Hi Anshul,

I have no idea about Cartian product, but if you need any coding related help. Please explain your scenario and error's / challenges you are facing. 

Thanks,

Gaurav
Skype: gaurav62990

Anshul Kapoor 13Anshul Kapoor 13
Hi Gaurav,

For my query the solution is:

for (String VrNr : lstVrNr){
                for (String LvlDsc : lstLvlDsc){
                    for (String WkNr : lstWkNr){
                        for (String GeoLocDsc : lstGeoLocDsc){
                            if (!b){
                                BE.Event_Calculation_Helper__c = BE.Event_Calculation_Helper__c + ', ' + VrNr + '_' + LvlDsc + '_' + WkNr + '_' + GeoLocDsc;
                            }
                            if (b){
                                BE.Event_Calculation_Helper__c = VrNr + '_' + LvlDsc + '_' + WkNr + '_' + GeoLocDsc;
                                b = false;
                            }
                        }
                    }
                }
            }

Please let me know for any queries.
This was selected as the best answer