【Publisher】Try executing the scheduled task on Sharperlight instance B from Sharperlight instance A

I will try to run a scheduled task on [Sharperlight](https://www.sharperlight.com/) instance B from [Sharperlight](https://www.sharperlight.com/) instance A.

Create a custom report using the [Sharperlight](https://www.sharperlight.com/) publisher and execute a pre-defined [Sharperlight](https://www.sharperlight.com/) scheduled task on another Sharperlight instance.
The user token for the Admin user to be used is prepared in advance at Sharperlight instance B side, where the target scheduled task is on.
## Prepare the HTML code for the custom report ##
Create a custom report here. Create a little script and a button with your text editor.
Now let's write the HTML code.
This is the header part. Since JQuery commands are used in the script part below, the JQuery library is referenced. Sharperlight comes with several JavaScript libraries, which can be referenced by such descriptions.
The rest is a CSS definition for decorating the button.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/74862600-22e9-5352-0c28-9f85371899e5.png)
I will introduce the Body part by dividing it into a script part and an HTML tag part.
Let's start with the script part.
Here we use JQuery's ajax command to post the task execution request.
You should see three global variables. One uses the control tag `{*Global.Value.AdminToken}` to extract the value of the token stored in the Sharperlight system property beforehand. `AdminToken` is the code name of the system property that stores the token.
The name of the task to be executed is defined as the query output item of the report that is the container for this HTML code, and is obtained with the control tag `{*Row.TaskName.text}`.
The service URL of Sharperlight instance B, where the task to be executed is defined, is also defined in the system property and extracted with the control tag `{*Global.Value.TargetService}`.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/adb6d829-7be6-d421-d187-62c4442e2479.png)
The HTML tag in the body is a simple button layout like this.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/5c2eafed-5c82-fb00-345c-86169264db56.png)
## Preparing a report to be a container for custom code ##
Create a published report that will be a container to hold the HTML code you created earlier.
Every published report must have a query defined.
This time, we don't need to define a query to display the data, but we must define a query. Therefore, I define the task name with the query and pass it to the custom HTML code at runtime. By doing so, even if the task you want to execute changes, you can respond by simply changing the query instead of editing the HTML code.
### Creating a new published report ###
Start Sharperlight Publisher from Sharperlight Application Menu.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/1946bde9-0acd-f4c7-1879-c858a3c0d3c7.png)
Once the publisher window opens, start creating a new published report with the `New` button.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/5465928c-8bc3-135c-c36f-454f6d89f64c.png)
With the published report dialog, fill in **Code**, **Group** and **Title**.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/9863fac2-13a8-3a93-5052-1dc0eaa552ad.png)
### Creating query ###
Start editing the query with the `Edit Query` button.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/52e2b0a2-4448-003a-5d61-dd23f5890022.png)
When Query Builder starts, set `Product` to **System**, `Table` to **User Accounts**, and `Mode` to **CU** as shown below. With this setting, the query will return only one record.
In this case, we just want to pass the name of the task we want to run to the HTML code, so one record is enough.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/a982114b-518d-9081-bc1b-37e25de5bfb2.png)
Now, use the expression to define the name of the task you want to execute as an output item.
Select `Add Expression` from the right-click menu in the output area.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/ebab4162-9e5b-c66b-fc4e-7f6084c753eb.png)
The editor will start, so define it as shown below. Save with the OK button.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/ee16216b-efb0-3624-cbc9-3d7d206a8d2a.png)
After saving, change the name to an easy-to-understand name as shown below. The name enclosed in a red frame is the code for reference.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/476096c4-385b-fefa-75a9-705906c699f6.png)
Validate your query with the preview feature. If you get a result like this, you're done. Save the query with the OK button.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/cccfb0c0-1a5e-0b7f-acc4-8559644bb3f9.png)
### Embedding HTML code ###
After saving the query and returning to the Publish Report dialog, it's time to embed the HTML code we prepared earlier.
Go to Options tab. Use the custom HTML option on this tab.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/f014785e-3a62-dfcd-d3ef-d7ab136e1194.png)
Tick the checkbox and launch the editor with the `Edit` button.
Paste the HTML code prepared earlier into the text field of the editor. Save with the OK button.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/7f254cf3-6d42-652e-2312-7ce68d142c1f.png)
This completes the creation of the published report. Save with the OK button.
## Verification ##
Start the Sharperlight service.
Paste the link address of the published report you created into your browser's address bar.
`QA1Rest` is the service code of the Sharperlight instance where this published report resides.
```
http://localhost/QA1Rest/Report/?query=QiitaSample.TriggerTaskVisible
```
You will see a simple page with just one button like this.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/8fd07f05-3db3-d624-4fe1-d5ae50f24a32.png)
Click the button. the target task on the specified Sharperlight service should have run in the background. Let's confirm it.
Start the scheduler of the Sharperlight instance where the specified task is defined and open the desired task.
Go to the history tab and click the delete icon to clear all history.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/db1af38e-428c-8cc9-631b-0b9ac26b7a16.png)
Go back to your browser and click the button. Immediately return to the task history tab and click the refresh icon.
You can check the history of what has just been executed.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/def40306-4bb0-3736-56c6-1c7c3f5fc4a3.png)
However, there is one security issue here.
When you view the source code with your browser's debug tool, the user token is completely visible.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/1d38c5c1-454e-85c4-16a4-2aac9e3c0e5f.png)
I'll cover how to deal with this in another article, so that's all for now.
[【Publisher】Thinking about invisible ways of tokens](https://qiita.com/Miki_Yokohata/items/8fa9d92dcad625bb1093)