2

I Have to lists with different records but using only one object. Like Example Apex Controller:

List<Commission_Setting__c> minChargeOTC = [Select Id, Commission_for__c, Rate__c From Commission_Setting__c where Commission_for__c like '%OTC%' and RecordType.Name = 'Minimum charge'];
List<Commission_Setting__c> OTC = [Select Id, Commission_for__c, Rate__c From Commission_Setting__c where Commission_for__c like '%OTC%' and RecordType.Name = 'Execution (Flat)'];

Now I want to merge them as Followed:

OTCCommission_for | OTCRate | minChargeOTCRate
OTCvalue          | 0.1     | 0.2

I want to merge them by Commission_for__c field value. How can I do this?

1 Answer 1

5

Create a wrapper class to represent the object they share in common. That seems to be the Commission__c field.

public class CommissionWrapper {

    // May need to be lists 
    public Commission_Setting__c min { get; set; }
    public Commission_Setting__c otc { get; set; }

    public CommissionWrapper(Commission_Setting__c min, Commission_Setting__c otc) {
        this.min = min;
        this.otc = otc; 
    }
}

Create these records in your controller, first by sorting each one by their Commission__c, then matching any records into a single wrapper:

public Map<Id, Commission_Setting__c> WrapToMap(List<Commission_Setting__c> settings) {
    Map<Id, Commission_Setting__c> settingsByCommission = new Map<Id, Commission_Setting__c>();

    for (Commission_Setting__c setting:settings) {
        settingsByCommission.put(setting.Commission__c, setting);
    }

    return settingsByCommission;
}

// ... in your code 

List<CommissionWrapper> wrappers = new List<Wrapper>();

Map<Id, Commission_Setting__c> minOtcByCommission = WrapToMap(minChargeOtc);
Map<Id, Commission_Setting__c> otcByCommission = WrapToMap(otc);

// May have trouble with key matching between records .. 

Set<Id> keys = new Set<Id>();

keys.AddAll(minOtcByCommission.keySet());
keys.AddAll(otcByCommission.keySet());

for (Id i:keys) {
    wrappers.add(new CommissionWrapper(minOtcByCommission.get(i), otcByCommission.get(i)));
}

Iterate over this object in your table:

<apex:repeat var="commission" value="{!wrappers}">
    <apex:column value="commission.min.Rate__c" header="Min Rate" />
    <apex:column value="commission.min.Rate__c" header="Min Rate" />
</apex:repeat>
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .