Deploying Scripts in Power Automate
Tags: powerautomate, powerplatform, officescripts, rpa
Published: 2023-02-08 10:15:13

Office Scripts are incredibly useful, often stretching beyond use cases of just Excel (if you want to know more check out [5 Scripts every power automate developer should know](https://dev.to/wyattdave/5-scripts-every-power-automate-developer-should-know-nep)). But there is one big problem with them, and its all Microsofts fault (isn't it always). 

The 2 problems are

1. Office Scripts can only be ran from your OneDrive (In Excel you can now run from SharePoint sites, ideal for Shared Scripts, but this functionality is missing from Power Automate)
2. The connector uses the Office Scripts ID, which is not a standard OneDrive ID (and not just the path)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iyvbctt1fr85cou7fcjz.png)

So this means everything is fine when you run the flow, but if you want to deploy a service account it all falls apart. First you need to copy the script file to the service accounts OneDrive, second when you deploy you have to create a copy (as there is a error due to the connector being unable to find the script). The script maybe there, but it has a different ID.

Fortunately there is a work around.

---

## Dynamically Select Script

There is an option to dynamically select script in the connector, but as I said, it requires the script ID, which is unique to the script.

When comparing the ID from OneDrive and the script ID I noticed a pattern:

**OneDrive**
`b!ZRVA09xUnUOCwKEiI7XMdx-ebslnsblIsT-999999-QxOv11S4vtZ6rj3Pwo.01GFTE5MRTJOVG5R2X559999DLHCQP3CWGSSX`

**Script ID**
`ms-officescript%3A%2F%2Fonedrive_business_itemlink%2F01GFTE5MRTJOVG5R2X5599999DLHCQP3CWGSSXI
`
As you can see after the '.' in the OneDrive ID it matches the end of the script ID after `officescript%3A%2F%2Fonedrive_business_itemlink%2F`


~~b!ZRVA09xUnUOCwKEiI7XMdx-ebslnsblIsT-999999-QxOv11S4vtZ6rj3Pwo.~~01GFTE5MRTJOVG5R2X559999DLHCQP3CWGSSX

`ms-officescript%3A%2F%2Fonedrive_business_itemlink%2F`
+
`01GFTE5MRTJOVG5R2X559999DLHCQP3CWGSSX`

So we can now use the OneDrive 'Get file metadata using path' action to get the ID, then use below expression to convert it to our script ID

```
concat('ms-officescript%3A%2F%2Fonedrive_business_itemlink%2F',split(outputs('Get_file_metadata_using_path')?['body/Id'],'.')[1])
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xl08n9wl651tkhx3ueoe.png)

## Parameters

Now we can dynamically select the script, the next problem we have is passing script parameters.

Peaking the code we can see the connector pulls the schema and generates keys based on inputs: `ScriptParameters/key`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0z459at1h58wqm4fdbo.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xo9chq5pgcu4kw54l2n.png)

But when we have dynamic script selection the connector cant get the schema, so it just has a ScriptParameters key. To fix this we just pass the inputs in as a json object:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ppjiq1vxxsdztp2zmdi7.png)

So our connector changes from:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddwrevjke0sj6nod421m.png)

to:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pd7j5jv410d21gj03sl3.png)

---

And thats it, the script will now work with any account, as long as it has the script saved to its OneDrive/Documents/Office Script folder.

There is an additional workaround you could add to make it even easier, and that would be to have a central SharePoint library holding all of the scripts. Then in the flow add an exception handler to download the file from SharePoint if it isn't found in the accounts OneDrive.

---

Hopefully Microsoft will create a proper fix, as I said its already added Scripts from SharePoint to Excel, so I expect we will soon be able to select scripts from SharePoint, which will be so much easier.