-
Notifications
You must be signed in to change notification settings - Fork 0
/
EinsteinChatbotsFindOrders.cls
43 lines (38 loc) · 1.48 KB
/
EinsteinChatbotsFindOrders.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public with sharing class EinsteinChatbotsFindOrders
{
@InvocableMethod(label='Einstein Chatbots - Find Orders For Contact' description='Returns orders for the specified Contact')
public static List<List<Order>> findOrdersForContact(List<Contact> thiscontact)
{
if(thiscontact == null || thiscontact.size() == 0)
{
return null;
}
Set<Id> contactIds = new Set<Id>();
for(Contact c : thiscontact)
{
contactIds.add(c.Id);
}
Map<Id, List<Order>> contactOrdersMap = new Map<Id, List<Order>>();
for(Order o : [SELECT Id, Name, TotalAmount, Description, Status, BillToContactId, OrderNumber FROM Order WHERE BillToContactId in :contactIds ORDER by CreatedDate desc LIMIT 3])
{
if(!contactOrdersMap.containsKey(o.BillToContactId))
{
contactOrdersMap.put(o.BillToContactId, new List<Order>());
}
contactOrdersMap.get(o.BillToContactId).add(o);
}
List<List<Order>> results = new List<List<Order>>();
for(Contact c : thiscontact)
{
List<Order> orders = contactOrdersMap.get(c.Id);
if(orders == null)
{
results.add(new List<Order>());
continue;
}
EinsteinChatbotsOrdersHelper.addOrderSummaries(orders);
results.add(orders);
}
return results;
}
}