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;
put()
to dynamically assign to the sObject fields.