cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

.js Function Exection on Service Completion

Ike@ACE
13-Aquamarine

.js Function Exection on Service Completion

How would I code a function to execute every time a service is completed? I would like to use js instead of bindings.

1 ACCEPTED SOLUTION

Accepted Solutions
ailko
5-Regular Member
(To:Ike@ACE)

To execute javascript code in studio home.js when a TWX service added in the External Data would trigger a "ServiceInvokedComplete" event you can add the following in the home.js to intercept that event and run code that doesn't require leveraging the bindings in studio:

 

$scope.$on('myTWXService.serviceInvokeComplete', function(evt, arg)
{
// do code after myTWXService executes
});

View solution in original post

5 REPLIES 5
ailko
5-Regular Member
(To:Ike@ACE)

To execute javascript code in studio home.js when a TWX service added in the External Data would trigger a "ServiceInvokedComplete" event you can add the following in the home.js to intercept that event and run code that doesn't require leveraging the bindings in studio:

 

$scope.$on('myTWXService.serviceInvokeComplete', function(evt, arg)
{
// do code after myTWXService executes
});

Ike@ACE
13-Aquamarine
(To:ailko)

What are the two parameters that are passed into the function (i.e. evt and arg)?

ailko
5-Regular Member
(To:Ike@ACE)

This is just the OOTB function definition for the serviceInvokedComplete trigger in Studio, as far as the values I've never needed to leverage them for my usecases or know what their values are but you can use console.log() in the home.js and the values will show up in the Console tab of dev tools. ie console.log(evt)

Ike@ACE
13-Aquamarine
(To:ailko)

Do you not need the name of the thing that the service is being pulled from either?

ailko
5-Regular Member
(To:Ike@ACE)

I'm not sure on that, I've never had a duplicate service name to test what happens if there are two services of the same name on different things from TWX. However you do leverage the name of the Thing to access the data of the service below is a snippet of a real implementation -> TWX Service completes, its a query so it has data from TWX the result being of type Infotable, I take what the service returns and populate some widgets and present a popup where those widgets live. SESSION_DATA is a global I declare to more easily leverage the data queried went programmatically setting widget values from other UX workflow/screens in the experience. This use case is a user scans a QR code, code value is used to query a TWX table for related data, data is presented to the user in a modal popup.

 

$scope.$on('queryDataTableForSerial.serviceInvokeComplete', function(evt, arg)
{
SESSION_DATA = $scope.app.mdl['myTWX_Thing.HelperThing'].svc['queryDataTableForSerial'].data;

$scope.app.params['path'] = '/Thingworx/FileRepositories/SystemRepository/Photos/MGZ11BSJAAQ.png';
$scope.view.wdg['valueDisplay-3']['value'] = SESSION_DATA[0].PartNumber;
$scope.view.wdg['valueDisplay-4']['value'] = SESSION_DATA[0].ModelNumber;
$scope.view.wdg['valueDisplay-5']['value'] = SESSION_DATA[0].BatchNumber;
$scope.view.wdg['valueDisplay-6']['value'] = SESSION_DATA[0].PackagedOn;

twx.app.fn.triggerWidgetService('popup-1', 'showpopup');
$scope.view.wdg['popup-1']['visible'] = true;
$scope.view.wdg['captureBtn']['visible'] = true;
});

Top Tags