|
| 1 | +import app from "../../salesforce_rest_api.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "salesforce_rest_api-get-knowledge-articles", |
| 5 | + name: "Get Knowledge Articles", |
| 6 | + description: "Get a page of online articles for the given language and category through either search or query. [See the documentation](https://developer.salesforce.com/docs/atlas.en-us.knowledge_dev.meta/knowledge_dev/sforce_api_rest_retrieve_article_list.htm)", |
| 7 | + version: "0.0.6", |
| 8 | + type: "action", |
| 9 | + props: { |
| 10 | + app, |
| 11 | + q: { |
| 12 | + type: "string", |
| 13 | + label: "Search Term", |
| 14 | + description: "Performs an SOSL search. If this property is not set, an SOQL query runs. The characters `?` and `*` are used for wildcard searches. The characters `(`, `)`, and `\"` are used for complex search terms. [See the documentation](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_find.htm).", |
| 15 | + optional: true, |
| 16 | + }, |
| 17 | + channel: { |
| 18 | + type: "string", |
| 19 | + label: "Channel", |
| 20 | + description: "Where articles are visible (App, Pkb, Csp, Prm).", |
| 21 | + optional: true, |
| 22 | + options: [ |
| 23 | + { |
| 24 | + label: "Internal Knowledge App", |
| 25 | + value: "App", |
| 26 | + }, |
| 27 | + { |
| 28 | + label: "Public Knowledge Base", |
| 29 | + value: "Pkb", |
| 30 | + }, |
| 31 | + { |
| 32 | + label: "Customer Portal", |
| 33 | + value: "Csp", |
| 34 | + }, |
| 35 | + { |
| 36 | + label: "Partner Portal", |
| 37 | + value: "Prm", |
| 38 | + }, |
| 39 | + ], |
| 40 | + }, |
| 41 | + language: { |
| 42 | + type: "string", |
| 43 | + label: "Language", |
| 44 | + description: "The language code. Defaults to `en-US`.", |
| 45 | + optional: true, |
| 46 | + }, |
| 47 | + categories: { |
| 48 | + type: "string", |
| 49 | + label: "Categories", |
| 50 | + description: "This should be a map in json format `{\"group1\": \"category1\", \"group2\": \"category2\", ...}`. It must be unique in each group:category pair, otherwise you get `ARGUMENT_OBJECT_PARSE_ERROR`. There is a limit of three data category conditions, otherwise you get `INVALID_FILTER_VALUE`.", |
| 51 | + optional: true, |
| 52 | + }, |
| 53 | + queryMethod: { |
| 54 | + type: "string", |
| 55 | + label: "Query Method", |
| 56 | + description: "Only valid when categories are specified, defaults to `ABOVE_OR_BELOW`.", |
| 57 | + optional: true, |
| 58 | + options: [ |
| 59 | + "AT", |
| 60 | + "BELOW", |
| 61 | + "ABOVE", |
| 62 | + "ABOVE_OR_BELOW", |
| 63 | + ], |
| 64 | + }, |
| 65 | + sort: { |
| 66 | + type: "string", |
| 67 | + label: "Sort By", |
| 68 | + description: "Field to sort results by. Defaults to `LastPublishedDate` for query and relevance for search", |
| 69 | + optional: true, |
| 70 | + options: [ |
| 71 | + "LastPublishedDate", |
| 72 | + "CreatedDate", |
| 73 | + "Title", |
| 74 | + "ViewScore", |
| 75 | + ], |
| 76 | + }, |
| 77 | + }, |
| 78 | + methods: { |
| 79 | + getKnowledgeArticles(args = {}) { |
| 80 | + const { app } = this; |
| 81 | + return app._makeRequest({ |
| 82 | + url: `${app._baseApiVersionUrl()}/support/knowledgeArticles`, |
| 83 | + ...args, |
| 84 | + }); |
| 85 | + }, |
| 86 | + async paginate({ |
| 87 | + requester, |
| 88 | + requesterArgs, |
| 89 | + resultsKey = "articles", |
| 90 | + maxRequests = 3, |
| 91 | + pageSize = 100, |
| 92 | + } = {}) { |
| 93 | + let allItems = []; |
| 94 | + let currentPage = 1; |
| 95 | + let hasMore = true; |
| 96 | + |
| 97 | + while (hasMore && currentPage <= maxRequests) { |
| 98 | + const response = await requester({ |
| 99 | + ...requesterArgs, |
| 100 | + params: { |
| 101 | + ...requesterArgs?.params, |
| 102 | + pageSize, |
| 103 | + pageNumber: currentPage, |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + const items = response[resultsKey]; |
| 108 | + if (items?.length) { |
| 109 | + allItems = [ |
| 110 | + ...allItems, |
| 111 | + ...items, |
| 112 | + ]; |
| 113 | + } |
| 114 | + |
| 115 | + hasMore = !!response.nextPageUrl; |
| 116 | + currentPage++; |
| 117 | + } |
| 118 | + |
| 119 | + return allItems; |
| 120 | + }, |
| 121 | + }, |
| 122 | + async run({ $ }) { |
| 123 | + const { |
| 124 | + app, |
| 125 | + paginate, |
| 126 | + getKnowledgeArticles, |
| 127 | + q, |
| 128 | + channel, |
| 129 | + language, |
| 130 | + categories, |
| 131 | + queryMethod, |
| 132 | + sort, |
| 133 | + } = this; |
| 134 | + |
| 135 | + const items = await paginate({ |
| 136 | + resultsKey: "articles", |
| 137 | + requester: getKnowledgeArticles, |
| 138 | + requesterArgs: { |
| 139 | + $, |
| 140 | + headers: { |
| 141 | + ...app._makeRequestHeaders(), |
| 142 | + "Accept": "application/json", |
| 143 | + "Accept-Language": language || "en-US", |
| 144 | + }, |
| 145 | + params: { |
| 146 | + q, |
| 147 | + channel, |
| 148 | + categories, |
| 149 | + queryMethod, |
| 150 | + sort, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }); |
| 154 | + |
| 155 | + $.export("$summary", `Successfully fetched \`${items.length}\` articles`); |
| 156 | + |
| 157 | + return items; |
| 158 | + }, |
| 159 | +}; |
0 commit comments