How to trigger a component script from Perspective page startup

Hello,

I have a use case where i have a script running on the button's action Performed event as you can see below

I want to trigger this script the moment the window is opened. I imagine it has to be a script on the 'Onstartup' system event
image

But how do i access the function from here? thanks in advance

You don't; that action will only be executed when the corresponding Event (in this case a click event of the Button while it is enabled) occurs.

If you need to perform the same logic on both Component start-up and in the event of a click, then you should move the logic into a Project Script, and both Events should then execute the Project Script.

1 Like

There is also a difference that you might want to keep in mind. One is a session startup and the other is the page startup. So the button onStartup script will trigger when the session starts and session ID is assigned. This will be the case when the page is opened for the first time or after session expiry time. Once opened, then any subsequent page refreshes will not trigger the button startup script.

If that is the need then please use Project > Session Events > Page Startup script area. This will result in the script to be executed even when the page is refreshed.

2 Likes

you mean this?

But how can i access the components of my view from here?

@rahman.tarek if you have something in your view that you want to be accessible from many places within that view, one thing you can do is create a Boolean custom parameter with a change script that activates it, something like this:

def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if currentValue.value:
		logger = system.util.getLogger("loggerName")
		try:
			# do the action here
			self.custom.result = test.add(
				self.custom.num1, 
				self.custom.num2
			)
		except Exception as e:
			logger.error(str(e))
			
		# always set self back to false
		self.custom.doSomething = False

I would never have any major script defined there, but rather use it as a convenient way to call it from several different places as needed. To activate it all you have to do is write that value to True.

image

Rgds,

Nick

If you need to call something from multiple locations why not put a script in the project level library and call that? If you need it to be able to put values in a session property then have the script accept the session object as a parameter.

Then all your calls are a single line call to the project level script.

3 Likes

how would i access the custom parameter from the page startup script?

@rahman.tarek all you'd have to do in the page startup is this:

self.custom.doSomething = True

Cheers,

Nick

didnt work unfortunately

i created a custom parameter for my page and then had my script on the parameter and tried to trigger it from the pagestartup

currentValue is the full object and will not work. You'll need to use currentValue.value

Good thing about that is if you ever need the timestamp you can also do currentValue.timestamp

Nick

yea i noticed but even after i fix it, it does not work. For sure the script is working but it does not execute when the page refreshes

A page refresh does not equate to a Page Startup Event. On the backend, the page has already been created and assigned an ID. During a refresh of the page, that ID is not overwritten, and even the View still exists - you just get a forced re-evaluation of any bindings/appearances.

We call this out in the Docs, albeit in the navigation context.

Note: Navigating to a page configuration in a tab that's already opened (such as using system.perspective.navigate) will not trigger this event.

Edit: I'm actually not as sure about this now that I look at it a bit closer... It looks like we do have an automated test to verify a browser refresh DOES trigger the event. Perhaps something else is happening here. Have you verified the value in question is being changed to something other than 1 AFTER the original change?

1 Like

I think if he uses the page startup event on the view rather than the session events it will help.

Nick

1 Like

no this does not work either. i guess perspective doesnt really it as easily. on vision you just have script on visionwindowopened and it works

Could you share all of the code which operates on that custom.update property? I still haven't seen any code which changes it to anything other than 1.


this is a snippet of the code. basically depending on the value of the update property i update my pie chart and table slightly differently

it absolutely does work, but you probably have something else wrong that you're not yet seeing.

Example:

Then open the screen and look at the logs:

Nick

hmmm interesting. i will have to troubleshoot it. But i do have a follow up question. So really the main use case is i want the script when update is 0 to run periodically - say every 1 min. this then would update my table and piechart with the live values.

Here you can see i have a 'live overview' button which triggers the script on the update custom property. But ofcourse i have to click this. What is the best way to ensure that the script on the update property when the value equals to 0 runs periodically at a fixed rate of 1 min?

I think there are multiple disconnects going on within this thread.

  1. Your use of a Page Startup script is going to result in the update request being sent every time a user opens a new tab for their session.
  2. The code you supplied show you using a Page Startup script to set self.custom.update to a value of 1, but the Page Startup script has no concept of self - only page. I suspect this code is failing to complete and logging errors on your Gateway.
  3. @nicholas.robinson appears to be using a View onStartup Event instead of the Page Startup Event.
  4. I still see no code which ever sets the custom.update property to anything other than 1. As a result - even if the Page Startup Event succeeds in writing a 1 to the prop - subsequent attempts will not trigger the update because they will be setting the property to have a value it already has, and so the change script will not execute.
  1. That makes sense. And yes i did get errors

  2. you can see in the screenshot above i set the update propery to 0 when the user clicks the live overview button