---
title: "Lusid.Portfolio.Writer"
slug: "lusidportfoliowriter"
updated: 2024-10-11T16:07:35Z
published: 2024-10-11T16:07:35Z
canonical: "support.lusid.com/lusidportfoliowriter"
---

> ## Documentation Index
> Fetch the complete documentation index at: https://support.lusid.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lusid.Portfolio.Writer

| **Type** | **Read/write** | **Author** | **Availability** |
| --- | --- | --- | --- |
| [Data provider](/v1/docs/what-are-a-data-provider-and-a-direct-provider) | Write | Finbourne | Provided with LUSID |

Providing you have sufficient [access control permissions](/v1/docs/creating-policies-to-control-access-to-providers-for-different-luminesce-end-users), the `Lusid.Portfolio.Writer` provider enables you to write a [Luminesce SQL query](/v1/docs/understanding-the-luminesce-sql-query-syntax) that either creates, updates or deletes one or more LUSID [portfolios](/v1/docs/portfolios).

> **Note**: By default, `Lusid.Portfolio.Writer` cannot add properties to portfolios. To do this, you must first configure `Lusid.Portfolio.Writer` to 'inline' properties. [See how to do this](/v1/docs/how-do-i-configure-luminesce-entity-providers-to-readwrite-properties).

You must construct a valid table of data to write, one portfolio per record. `Lusid.Portfolio.Writer` lists the fields (columns) available to populate with values for each record, and has a set of parameters to help you construct a valid table.

Your query can use the `WriteAction` field to perform one of the following operations:

- Upsert a portfolio; that is, create a portfolio if it does not exist, and update it if it does. This is the default operation if you omit `WriteAction`. You must specify the `PortfolioType` field to declare either a [transaction](/v1/docs/what-is-a-transaction-portfolio), [derived transaction](/v1/docs/what-is-a-derived-transaction-portfolio) or [reference](/v1/docs/what-is-a-reference-portfolio) portfolio.
- Insert (create) a portfolio. This operation fails if the portfolio already exists.
- Update a portfolio. Note it is only possible to change the `DisplayName` and `Description` fields, or add or change properties. This operation fails if the portfolio does not already exist.
- Delete a portfolio.

**See also:** [Lusid.Portfolio](/v1/docs/lusidportfolio)

## Basic usage

```sql
@table_of_data = <select-statement>;
select * from Lusid.Portfolio.Writer where toWrite = @table_of_data;
```

## Query parameters

`Lusid.Portfolio.Writer` has parameters that help you construct a valid table of data to write.

> **Note:** The toWrite parameter is mandatory and used to actually write the table of data into LUSID.

To list available parameters, their data types, default values, and an explanation for each, run the following query using a [suitable tool](/v1/docs/what-tools-are-available-to-write-luminesce-queries):

```sql
select FieldName, DataType, ParamDefaultValue, Description from Sys.Field where TableName = 'Lusid.Portfolio.Writer' and FieldType = 'Parameter';
```

## Data fields

`Lusid.Portfolio.Writer` lists the fields you can populate in your table of data to write.

Depending on the operation you want to perform and the type of portfolio, the following fields are mandatory to include in the table of data:

| **Operation** | **Mandatory fields in table of data to write for all portfolios** | **Mandatory fields for transaction portfolios** | **Mandatory fields for derived transaction portfolios** | **Mandatory fields for reference portfolios** |
| --- | --- | --- | --- | --- |
| Upsert, Insert | `PortfolioScope` `PortfolioCode` `DisplayName` `Created` `'Insert' as WriteAction` (for insert operations) | `'Transaction' as PortfolioType` `BaseCurrency` `SubHoldingKeys` (can be empty string) | `'DerivedTransaction' as PortfolioType` `SubHoldingKeys` (can be empty string) | `'Reference' as PortfolioType` |
| Update | `PortfolioScope` `PortfolioCode` `'Update' as WriteAction` | `'Transaction' as PortfolioType` | `'DerivedTransaction' as PortfolioType` | `'Reference' as PortfolioType` |
| Delete | `PortfolioScope` `PortfolioCode` `'Delete' as WriteAction` | `'Transaction' as PortfolioType` | `'DerivedTransaction' as PortfolioType` | `'Reference' as PortfolioType` |

To list all available fields, their data types, whether fields are considered 'main', and an explanation for each, run the following query using a [suitable tool](/v1/docs/what-tools-are-available-to-write-luminesce-queries):

```sql
select FieldName, DataType, IsMain, IsPrimaryKey, SampleValues, Description from Sys.Field where TableName = 'Lusid.Portfolio.Writer' and FieldType = 'Column';
```

## Write errors

We recommend examining the results of every write query using one or more of the `WriteError`, `WriteErrorCode` and `WriteErrorDetail` fields.

For each record in the table of data to write, `Lusid.Portfolio.Writer` returns an error code. If the operation is successful, the error code is 0. If unsuccessful, a positive error code and explanation help you discover why LUSID considers the operation invalid.

For example, the query:

```sql
@table_of_data = select 'Finbourne-Examples' as PortfolioScope, 'UK-Benchmark' as PortfolioCode, 'UK Benchmark Index' as DisplayName, #2020-01-01# as Created;
select WriteErrorCode, WriteError, WriteErrorDetail from Lusid.Portfolio.Writer where toWrite = @table_of_data;
```

...fails because a `PortfolioType` is not specified.

## Examples

> **Note:** For more example Luminesce SQL queries, visit our [Github repo](https://github.com/finbourne/luminesce-examples/tree/master/examples).

### Example 1: Create a transaction portfolio

If you omit the `WriteAction` field, an upsert operation is performed.

```sql
@table_of_data = select 'Transaction' as PortfolioType, 'Finbourne-Examples' as PortfolioScope, 'FR-Equities' as PortfolioCode,
'French Equities' as DisplayName, 'Portfolio containing French equities' as Description, #2020-01-01# as Created, 'EUR' as BaseCurrency, '' as SubHoldingKeys;
select * from Lusid.Portfolio.Writer where ToWrite = @table_of_data;
```

### Example 2: Create a derived transaction portfolio with a sub-holding key

```sql
@table_of_data = select 'DerivedTransaction' as PortfolioType, 'Finbourne-Examples' as PortfolioScope, 'Derived-UK-Equities' as PortfolioCode,
'Finbourne-Examples' as ParentPortfolioScope, 'UK-Equities' as ParentPortfolioCode, 'Derived UK Equities' as DisplayName,
'Derived portfolio containing UK equities' as Description, #2020-01-01# as Created, 'Transaction/Vendor/Strategy' as SubHoldingKeys;
select * from Lusid.Portfolio.Writer where ToWrite = @table_of_data;
```

### Example 3: Create a reference portfolio with particular instrument scopes

```sql
@table_of_data = select 'Reference' as PortfolioType, 'Finbourne-Examples' as PortfolioScope, 'UK-Benchmark' as PortfolioCode, 'UK Benchmark' as DisplayName, 
'Benchmark for UK equities' as Description, #2020-01-01# as Created, 'reference-instruments-1,reference-instruments-2' as InstrumentScopes;
select * from Lusid.Portfolio.Writer where ToWrite = @table_of_data;
```

### Example 4: Update the name, description and a property on a transaction portfolio

Note that to update properties for portfolios, `Lusid.Portfolio.Writer` [must have been configured](/v1/docs/how-do-i-configure-luminesce-entity-providers-to-readwrite-properties) to 'inline' the chosen properties (in this case `PortfolioManager`) into the standard set of portfolio fields.

```sql
@table_of_data = select 'Transaction' as PortfolioType, 'Finbourne-Examples' as PortfolioScope, 'FR-Equities' as PortfolioCode,
'Equities from France' as DisplayName, 'Portfolio containing equities from France' as Description, 'Rene Duclerc' as PortfolioManager, 'Update' as WriteAction;
select * from Lusid.Portfolio.Writer where ToWrite = @table_of_data;
```

### Example 5: Delete a transaction portfolio

```sql
@table_of_data = select 'Transaction' as PortfolioType, 'Finbourne-Examples' as PortfolioScope, 'FR-Equities' as PortfolioCode, 'Delete' as WriteAction;
select * from Lusid.Portfolio.Writer where ToWrite = @table_of_data;
```
