1

I am new to Apex and am trying to build a Map object which contains all the order information. Ideally, I would like to serialize this into JSON and send to a webservice. I have got to the point where I can query the different part of the order and fetch them, but I cannot create the final map.

My code upto this point is -

Order order = [SELECT Id, TotalAmount, AccountId, BillToContactId FROM Order];

Id orderId = order.Id;
Decimal amount = order.TotalAmount;
Id accountId = order.AccountId;
Id contactId = order.BillToContactId;

Account account = [select Name from Account where Id=:accountId];
//i understand that this should be Contact contact but i wanted to try and convert to map when fetching to make things simpler if possible?
Map<string, string> contact = (Map<string, string>)[select Name, Email, MobilePhone from Contact where Id=:contactId];

List<OrderItem> items = [select OrderItemNumber, Product2Id, Quantity, TotalPrice, UnitPrice from OrderItem where OrderId=:orderId];

Map<String, Map<String, Object>> data = new Map<String, Map<String, Object>>();
data.put('contact', contact);    
data.put('account', account);     //tried this too but errors out since map expects the second variable to be map

system.debug(data);

I have left the error parts as well as comments on where I am getting stuck as part of the code. Any help is appreciated.

Please let me know if you need any details and point out any rookie mistakes.

3 Answers 3

7

You could do this :

Map<String, Object> data = new Map<String, Object>();
Map<String, Contact> contact = [...];
List<OrderItem> items = [...];
Account account = [select Name from Account where Id=:accountId];

data.put('contact', contact);    
data.put('account', account);
data.put('items', items);

then

Map<String, Contact> mct = (Map<String, Contact>) data.get('contact');
0
4

This

Map<String, Map<String, Object>> data = new Map<String, Map<String, Object>>();

Should be

Map<String, Object> data = new Map<String, Object>();
2
  • Thanks for the answer. Both yours and @Emmanuel-bruno 's answers were correct but I am accepting his since it is more detailed and anyone coming through later will get a better idea. Much appreciate the help though!
    – pinaki
    Oct 11, 2017 at 7:46
  • Sure, thanks for the comment and upvote; appreciated :-) Oct 11, 2017 at 11:35
0

You put list of account data into a map of data. And also use List for assigning Object. please try this. hope it will help you.

List<Order> orderList = [SELECT Id, TotalAmount, AccountId, BillToContactId FROM Order];

Decimal amount = orderList[0].TotalAmount;

Map<string, Contact> contactMap = new Map<string, Contact>();
for(Contact con : [select Name, Email, MobilePhone from Contact where Id=:orderList[0].BillToContactId]){
    contactMap.put('contact', con);
}

Map<string, Account> accountMap = new Map<string, Account>();
for(Account acc : [select Name from Account where Id=:orderList[0].AccountId]){
    accountMap.put('account', acc);
}

List<OrderItem> items = [select OrderItemNumber, Product2Id, Quantity, TotalPrice, UnitPrice from OrderItem where OrderId=:orderList[0].Id];

Map<String, Map<String, Object>> data = new Map<String, Map<String, Object>>();
data.put('contact', contactMap);    
data.put('account', accountMap);

You must log in to answer this question.

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