# Terraform Plugin Framework : ListNestedAttribute Value Conversion Error

**URL:** <https://discuss.hashicorp.com/t/terraform-plugin-framework-listnestedattribute-value-conversion-error/61879>\
**Category:** Terraform Providers\
**Created:** [January 18, 2024, 8:42pm UTC](https://discuss.hashicorp.com/t/terraform-plugin-framework-listnestedattribute-value-conversion-error/61879 "2024-01-18T20:42:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sikaleel87](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/sikaleel87/32/38268_2.png) [@sikaleel87](https://discuss.hashicorp.com/u/sikaleel87)\
**Post date:** [January 18, 2024, 8:42pm UTC](https://discuss.hashicorp.com/t/terraform-plugin-framework-listnestedattribute-value-conversion-error/61879/1 "2024-01-18T20:42:09Z")

</div>

Hi

I have created a sample code by following exact foot steps given on the below article for using ListNestedAttributes

[Custom Framework Providers](https://developer.hashicorp.com/terraform/tutorials/providers-plugin-framework/providers-plugin-framework-resource-create)

```auto
{
"subusers": [
        {
            "user_id": 12345678,
            "username": "chida.test"
        }
    ]
//other values to
}

```

My struct are like this

```auto
type Listsubusers struct {
	Username types.String `tfsdk:"username"`
	UserID types.Int64 `tfsdk:"user_id"`
}

type DomainsubuserResourceModel struct {
	ID types.Int64 `tfsdk:"id"`
	Username types.String `tfsdk:"username"`
	Subusers []Listsubusers `tfsdk:"subusers"`
}

```

My schema will look like this

```auto
"subusers": schema.ListNestedAttribute{
				Computed: true,
				PlanModifiers: []planmodifier.List{
					listplanmodifier.UseStateForUnknown(),
				},
				NestedObject: schema.NestedAttributeObject{
					Attributes: map[string]schema.Attribute{
						"username": schema.StringAttribute{
							Description: "The subuser username to be associate with the domain",
							Computed: true,
							PlanModifiers: []planmodifier.String{
								stringplanmodifier.UseStateForUnknown(),
							},
						},
						"user_id": schema.Int64Attribute{
							Description: "The subuser id to be associate with the domain",
							Computed: true,
							PlanModifiers: []planmodifier.Int64{
								int64planmodifier.UseStateForUnknown(),
							},
						},
					},
				},
			},

```

my create function will have code like this to set the state file

```auto
for orderItemIndex, orderItem := range domainsubuser.Subusers {
		domainsubnewstate.Subusers[orderItemIndex] = Listsubusers{
			Username: types.StringValue(orderItem.Username),
			UserID: types.Int64Value(orderItem.UserID),
		}
	}

```

but getting below error message ☹

```auto
Error: Value Conversion Error

An unexpected error was encountered trying to build a value. This is always an error in the provider. Please      
│ report the following to the provider developer:
│
│ Received unknown value, however the target type cannot handle unknown values. Use the corresponding `types`       
│ package type or a custom type that handles unknown values.
│
│ Path: subusers
│ Target Type: []sendgrid.Listsubusers
│ Suggested Type: basetypes.ListValue

```

Alternatively tried using Block and it update state file but that ends up an error as below

```auto
block count has changed from 0 to 1.

```

Please any suggestion would be really helpful.

---

<div class="post-metadata">

**Author:** ![sikaleel87](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/sikaleel87/32/38268_2.png) [@sikaleel87](https://discuss.hashicorp.com/u/sikaleel87)\
**Post date:** [January 19, 2024, 12:36pm UTC](https://discuss.hashicorp.com/t/terraform-plugin-framework-listnestedattribute-value-conversion-error/61879/2 "2024-01-19T12:36:24Z")

</div>

> [@sikaleel87](#):
>
> ```auto
> {
> "subusers": [
> {
> "user_id": 12345678,
> "username": "chida.test"
> }
> ]
> //other values to
> }
> 
> ```

I have solved it by my selft.

schema changed to like this

```auto
			"subusers": schema.ListNestedAttribute{
				Computed: true,
				NestedObject: schema.NestedAttributeObject{
					Attributes: map[string]schema.Attribute{
						"username": schema.StringAttribute{
							Description: "The subuser username to be associate with the domain",
							Computed: true,
						},
						"user_id": schema.Int64Attribute{
							Description: "The subuser id to be associate with the domain",
							Computed: true,
						},
					},
				},
			},

```

and Create/Read section used like this

```auto
elements := []attr.Value{}
	for _, subuser := range domainsubuser.Subusers {
		elements = append(elements, types.ObjectValueMust(
			map[string]attr.Type{
				"username": types.StringType,
				"user_id": types.Int64Type,
			},
			map[string]attr.Value{
				"username": types.StringValue(subuser.Username),
				"user_id": types.Int64Value(subuser.UserID),
			},
		))
	}

	domainsubuserstate.Subusers = types.ListValueMust(
		types.ObjectType{
			AttrTypes: map[string]attr.Type{
				"username": types.StringType,
				"user_id": types.Int64Type,
			},
		}, elements,
	)

```

A big thank you to @bendbennett you [reference](https://discuss.hashicorp.com/t/optional-computed-block-handling-in-plugin-framework/56337/2) helped me to achieve this.
