# Netsuite -> raia Agent

As a user of Raia Agent, I want to automatically sync our NetSuite Knowledge Base content—including topics, solutions, and relevant cases—into a Raia Agent as a single Markdown file. This will provide the agent with our comprehensive support and product information, enabling it to assist customers and internal teams effectively.

**Important Note on NetSuite API Limitations:**

NetSuite's REST API provides access to individual knowledge base records (`topic`, `solution`, `supportcase`), but it **does not** directly expose the relationships between them. For example, the REST API does not provide a straightforward way to get a list of all solutions attached to a specific topic. This limitation makes it challenging to reconstruct the knowledge base hierarchy programmatically.

This task requires a developer who can navigate these limitations, potentially by using alternative query methods or by making multiple API calls and linking the data within the workflow.

**Proposed Workflow (Primary Approach):**

The n8n workflow should run on a nightly schedule and perform the following actions:

1. **Fetch All Topics:** Retrieve all knowledge base topics from NetSuite.
2. **Fetch All Solutions:** Retrieve all knowledge base solutions from NetSuite. Each solution record should contain a reference to the topic(s) it belongs to.
3. **Fetch Relevant Cases (Optional):** Retrieve support cases that have been marked as knowledge base articles or are linked to solutions, if possible via the API.
4. **Consolidate and Structure Data:** In the n8n workflow, process the fetched records to reconstruct the knowledge base hierarchy. This will involve mapping solutions to their parent topics.
5. **Generate Markdown:** Create a single, well-structured Markdown file that organizes the content by topic, with solutions nested underneath. Each entry should include the title, description/body, and any relevant metadata.
6. **Upload to Raia Agent:** Upload the generated Markdown file to the Raia Agent using the API.

**Acceptance Criteria:**

* An n8n workflow is created that successfully fetches `topic` and `solution` records from NetSuite.
* The workflow correctly maps solutions to their respective topics to build a hierarchical structure.
* The workflow consolidates the data into a single Markdown file.
* The workflow successfully uploads the Markdown file to the Raia Agent via the API.
* The workflow is scheduled to run every night.
* The workflow includes robust error handling and notifications.

**Technical Details:**

* **NetSuite API (SuiteTalk REST):**
  * **Authentication:** Token-Based Authentication (TBA) or OAuth 1.0.
  * **Topic Endpoint:** `GET /services/rest/record/v1/topic`
  * **Solution Endpoint:** `GET /services/rest/record/v1/solution`
  * **Case Endpoint:** `GET /services/rest/record/v1/supportCase`
* **Raia Agent API:**
  * **Endpoint:** `POST /external/agent-files/upload`
  * **Request Type:** `multipart/form-data`
  * **Authentication:** API Key in the header.
  * **Request Body:**
    * `file`: The Markdown file to upload.
    * `autoAddToVectorStore`: `true`.

**Alternative Approaches (to be investigated if the primary approach is blocked):**

* **SuiteQL:** Use NetSuite's SuiteQL via the REST API to perform more complex queries that can join `topic` and `solution` records. This is likely the most viable alternative.
* **SOAP API:** The SOAP API may expose the relationships between records more directly than the REST API. This would require using the SOAP endpoint and XML-based requests.

**Implementation Notes:**

* The primary challenge will be linking solutions to topics. The developer should first investigate the fields returned by the `solution` record endpoint to see if a `parentTopic` or similar field is available.
* If the relationship is not directly available, the developer must implement one of the alternative approaches (SuiteQL is recommended).
* The developer will need access to the NetSuite REST API Browser and potentially the SOAP Schema Browser to understand the full data model of each record.
