# Problem with creating reference to a resource

**URL:** <https://forum.flow.com/t/problem-with-creating-reference-to-a-resource/2348>\
**Category:** Flow Playground\
**Created:** [August 29, 2021, 11:56am UTC](https://forum.flow.com/t/problem-with-creating-reference-to-a-resource/2348 "2021-08-29T11:56:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![YBBlockDev](https://avatars.discourse-cdn.com/v4/letter/y/f1d935/32.png) [@YBBlockDev](https://forum.flow.com/u/YBBlockDev)\
**Post date:** [August 29, 2021, 11:56am UTC](https://forum.flow.com/t/problem-with-creating-reference-to-a-resource/2348/1 "2021-08-29T11:56:42Z")

</div>

Hi everyone! I’m new to Cadence and I’m facing a problem with resources & references.  
I have a resource for certain withdrawing restrictions store in /storage/Restrictions, then I have a transaction that changes the Withdrawing date on the resource and checks if the reference to the resource was created.  
I thought that the path is right and the capability should be created correctly but I always get the error that the reference was not created!  
Here is the code for the transaction;

transaction {

var tempVault: &EducationFund.Rules{EducationFund.WithdrawRestrictions}

var account: AuthAccount

prepare(acct: AuthAccount){

```
   self.account = acct

   let fundAccount = getAccount(0x03)

   let refVault = fundAccount.getCapability(/public/Restrictions)

   .borrow<&EducationFund.Rules{EducationFund.WithdrawRestrictions}>()

   ?? panic("Could not create reference to resource!")

   self.tempVault = refVault

```

}

execute {

```
   self.tempVault.setWithdrawDate(_withdrawDate: 10000.0, account: self.account)

   log("Withraw date changed!")

```

}

}

---

<div class="post-metadata">

**Author:** ![flowjosh](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/flowjosh/32/362_2.png) [@flowjosh](https://forum.flow.com/u/flowjosh)\
**Post date:** [September 1, 2021, 7:18pm UTC](https://forum.flow.com/t/problem-with-creating-reference-to-a-resource/2348/2 "2021-09-01T19:18:23Z")

</div>

Welcome to the Flow community! It is good to have you here. 😃

`getCapability` never fails, but if the capability is invalid, the borrow could fail. This could be for many reasons:

- You are trying to get the capability from a different account than what you saved it to
- You are trying to get the capability from a different path than what you saved it to
- The type of the reference you are trying to borrow is different than what you saved it as
- The type of the capability you created doesn’t match what it is actually targeting in storage

I would recommend looking into those potential issues first, then if you still can’t solve it, post the transaction that you used to create the capability and I can take a look. 🙂

---

<div class="post-metadata">

**Author:** ![YBBlockDev](https://avatars.discourse-cdn.com/v4/letter/y/f1d935/32.png) [@YBBlockDev](https://forum.flow.com/u/YBBlockDev)\
**Post date:** [September 6, 2021, 1:34pm UTC](https://forum.flow.com/t/problem-with-creating-reference-to-a-resource/2348/3 "2021-09-06T13:34:58Z")

</div>

Hi there flowjosh, really thankful for the response!  
I’m still lost and couldn’t figure out the issue but I also didn’t understand the last two possible issues.  
Would it be okay if I send you the code?

---

<div class="post-metadata">

**Author:** ![flowjosh](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/flowjosh/32/362_2.png) [@flowjosh](https://forum.flow.com/u/flowjosh)\
**Post date:** [September 7, 2021, 2:49pm UTC](https://forum.flow.com/t/problem-with-creating-reference-to-a-resource/2348/4 "2021-09-07T14:49:36Z")

</div>

Can you please post the code here? @YBBlockDev

---

<div class="post-metadata">

**Author:** ![YBBlockDev](https://avatars.discourse-cdn.com/v4/letter/y/f1d935/32.png) [@YBBlockDev](https://forum.flow.com/u/YBBlockDev)\
**Post date:** [September 8, 2021, 11:21am UTC](https://forum.flow.com/t/problem-with-creating-reference-to-a-resource/2348/5 "2021-09-08T11:21:18Z")

</div>

Sure! here is the link for the code;

> **[Try out this Playground project](https://play.onflow.org/4d01157f-ea74-462a-a2c6-e5a5137171bf?type=tx&id=4f4afa12-1f7b-4ea8-92e4-701a23b5c172)**
>
> Learn Cadence, a revolutionary new smart contract programming language written for the Flow blockchain.

---

<div class="post-metadata">

**Author:** ![NasirAliShah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/nasiralishah/32/519_2.png) [@NasirAliShah](https://forum.flow.com/u/NasirAliShah)\
**Post date:** [September 10, 2021, 4:09pm UTC](https://forum.flow.com/t/problem-with-creating-reference-to-a-resource/2348/6 "2021-09-10T16:09:51Z")

</div>

@YBBlockDev  
you have created the path self.account.save(\<- create Rules(), to: /storage/Restrictions) in the education fund contract but you did not made it public that all contract can get it’s data. When you are getting it in the transaction.  
let vaultRef = fundAccount.getCapability(/public/Restrictions)  
.borrow\<&EducationFund.Rules{EducationFund.WithdrawRestrictions}\>()  
actually you didn’t make it public anywhere in the contract, meaning you didn’t link it.  
You also have more issue related this linking.

---

<div class="post-metadata">

**Author:** ![YBBlockDev](https://avatars.discourse-cdn.com/v4/letter/y/f1d935/32.png) [@YBBlockDev](https://forum.flow.com/u/YBBlockDev)\
**Post date:** [September 13, 2021, 6:45pm UTC](https://forum.flow.com/t/problem-with-creating-reference-to-a-resource/2348/7 "2021-09-13T18:45:55Z")

</div>

Hello I really appreciate the feedback!  
Can you please provide me with a little bit more instructions on how this can be fixed?

---

<div class="post-metadata">

**Author:** ![NasirAliShah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/nasiralishah/32/519_2.png) [@NasirAliShah](https://forum.flow.com/u/NasirAliShah)\
**Post date:** [September 14, 2021, 3:59am UTC](https://forum.flow.com/t/problem-with-creating-reference-to-a-resource/2348/8 "2021-09-14T03:59:14Z")

</div>

there may be multipe issues but one I solved is to make the capability public in the Education Fund contract, I updated the code in the init of this contract.

> **[Try out this Playground project](https://play.onflow.org/8a900c9b-89cd-4c2b-9923-8ba0bd916cab?type=account&id=20dc8952-28b4-4f37-9799-a62879127882)**
>
> Learn Cadence, a revolutionary new smart contract programming language written for the Flow blockchain.

self.account.link\<&EducationFund.Rules{WithdrawRestrictions}\>(/public/Restrictions, target: /storage/Restrictions)  
Now with this link you can call the methods and variables available in Education Fund contract.
