-
Notifications
You must be signed in to change notification settings - Fork 0
/
EinsteinChatbotsSearchArticles.cls
40 lines (31 loc) · 1.59 KB
/
EinsteinChatbotsSearchArticles.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
global class EinsteinChatbotsSearchArticles {
global class KnowledgeArticleTitleRequest {
@InvocableVariable
global string category;
@InvocableVariable
global String queryString;
}
global class KnowledgeArticleTitleResult {
@InvocableVariable
global String KnowledgeTitle;
@InvocableVariable
global String ChatAnswer;
}
@InvocableMethod(label='KnowledgeArticlesSearchAndFilter')
global static List<List<Knowledge__kav>> fetchKnowledgeArticleSearchAndFilter(List<KnowledgeArticleTitleRequest> queryParams) {
List<List<Knowledge__kav>> results = new List<List<Knowledge__kav>>();
for(KnowledgeArticleTitleRequest param : queryParams) {
results.add(fetchKnowledgeArticle(param.category, param.queryString));
}
return results;
}
public static List<SObject> fetchKnowledgeArticle(string category, string searchQuery) {
KnowledgeArticleTitleResult result = new KnowledgeArticleTitleResult();
category = String.join(category.split(' '), '_') + '__c';
category = category == 'All__c' ? 'below ' + category : 'above ' + category;
string searchString = 'FIND \'' + searchQuery + '*\' IN ALL FIELDS RETURNING Knowledge__kav (ID, Title,Chat_Answer__c, UrlName, KnowledgeArticleId) WITH DATA CATEGORY Product__c '+ category +' limit 3';
system.debug(searchString);
List<List<SObject>> catdata = Search.query(searchString);
return catdata[0];
}
}