-2

Dear Friends the below code is working fine as expected but since it is not the best practice to do array indexing kindly guide me in this regard.

Map<Id,Order> ord = new Map<Id,Order>([SELECT Id,AccountId,(SELECT Id, ebMobile__Product2__c,ebMobile__LineAmount__c,Account__c FROM OrderItems) FROM Order where Accountid = 'XXXXXXXXX']);
        Map<String,Decimal> myresult = new Map<String,Decimal>();
        Set<Id> idss = new Set<Id>();
        Set<Id> myid = new Set<Id>();

        for(Order o : ord.values())
        {
            idss.add(o.id);
            for(OrderItem s : o.OrderItems)
            {
                myresult.put(s.ebMobile__Product2__c,s.ebMobile__LineAmount__c);

            }
        }
        List<Order> orderss = [SELECT Id,AccountId,(SELECT Id, ebMobile__Product2__c,ebMobile__LineAmount__c FROM OrderItems where ebMobile__Product2__c in : myresult.keySet()) FROM Order where id in : idss and AccountId != null];
        for(Order o : orderss)
        {
            myId.add(o.id);
        }   
        List<AccountMetricsSKU__c> oppList  = new List<AccountMetricsSKU__c>();
        List<AggregateResult> Rslt2 = [SELECT ebMobile__Product2__c,SUM(ebMobile__LineAmount__c) amt,count(id) tot from OrderItem where orderId in : myId group by ebMobile__Product2__c ORDER BY SUM(ebMobile__LineAmount__c) DESC LIMIT 10];

        AccountMetricsSKU__c am = new AccountMetricsSKU__c();
        am.Name = 'Am-SKU';
        am.Account__c = orderss[0].AccountId;
        am.Period__c = System.today();
        am.amt1__c =(Decimal) Rslt2[0].get('amt'); 
        am.amt2__c = (Decimal) Rslt2[1].get('amt');
        am.amt3__c = (Decimal) Rslt2[2].get('amt');
        am.amt4__c = (Decimal) Rslt2[3].get('amt');
        am.amt5__c = (Decimal) Rslt2[4].get('amt');
        am.amt6__c = (Decimal) Rslt2[5].get('amt');
        am.amt7__c = (Decimal) Rslt2[6].get('amt');
        am.amt8__c = (Decimal) Rslt2[7].get('amt');
        am.amt9__c = (Decimal) Rslt2[8].get('amt');
        am.amt10__c = (Decimal) Rslt2[9].get('amt');
        am.sku1__c = (Id) Rslt2[0].get('ebMobile__Product2__c');
        am.sku2__c = (Id) Rslt2[1].get('ebMobile__Product2__c');
        am.sku3__c = (Id) Rslt2[2].get('ebMobile__Product2__c');
        am.sku4__c = (Id) Rslt2[3].get('ebMobile__Product2__c');
        am.sku5__c = (Id) Rslt2[4].get('ebMobile__Product2__c');
        am.sku6__c = (Id) Rslt2[5].get('ebMobile__Product2__c');
        am.sku7__c = (Id) Rslt2[6].get('ebMobile__Product2__c');
        am.sku8__c = (Id) Rslt2[7].get('ebMobile__Product2__c');
        am.sku9__c = (Id) Rslt2[8].get('ebMobile__Product2__c');
        am.sku10__c =(Id) Rslt2[9].get('ebMobile__Product2__c');
        oppList.add(am);

        insert oppList;  
3
  • 1
    Array indexing is not a bad practice. Array indexing without bounds checking is very bad practice. You should either write code to check the boundary of the array, or write a loop over your AggregateResults and use put() to dynamically assign to the sObject fields.
    – David Reed
    Commented Jan 9, 2020 at 20:49
  • I'd suggest you give that a try and edit your question to show us where you are stuck.
    – David Reed
    Commented Jan 9, 2020 at 20:54
  • @David thanks for your valuable time and for your response i would make a note of it concern to array index.
    – vishal
    Commented Jan 10, 2020 at 5:57

1 Answer 1

1

To reduce repeated code, and be a little clearer about what is going on, you could use this code instead:

AccountMetricsSKU__c am = new AccountMetricsSKU__c(
    Name = 'Am-SKU',
    Account__c = orderss[0].AccountId,
    Period__c = System.today()
);

Integer i = 1;
for (AggregateResult r : [
        SELECT ebMobile__Product2__c,SUM(ebMobile__LineAmount__c) amt,count(id) tot
        from OrderItem
        where orderId in : myId
        group by ebMobile__Product2__c
        ORDER BY SUM(ebMobile__LineAmount__c) DESC LIMIT 10
        ]) {
    am.put('amt' + i + '__c', r.get('amt'));
    am.put('sku' + i + '__c', r.get('ebMobile__Product2__c'));
    i++;
}

insert am;

but if there are only ever 2 fields and 10 values it doesn't make much difference.

1
  • Humbly thank you for your assistance your strategy of code resolved my issue.
    – vishal
    Commented Jan 10, 2020 at 5:55

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