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

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Call Service on Platform from Extension Widget

jburke11
12-Amethyst

Call Service on Platform from Extension Widget

I am looking to make a call from within an extension widget to a service on the platform. Namely, I am looking to get the current user identity and some other tasks (which are handled by MyService) and use that internally in the widget to control certain parts of the html (such as hiding or showing buttons). So far I have referenced these two posts as well some of the SDK.

I have had minor success with the ThingworxInvoker at least returning a table as opposed to the second method of using processServiceRequest (causes the widget to fail). The difficulty I am having is accessing the data in the table. 

 

https://community.ptc.com/t5/ThingWorx-Developers/Thingworx-UI-Widget-development-calling-standard-thingworx/m-p/587862#M33332

 

https://community.ptc.com/t5/ThingWorx-Developers/Calling-Thingworx-Service-Using-Java-Extension-SDK/m-p/592096#M34028

My current call is like so

var invoker;
var success = function (invoker) {
     var dataFromInvoke = invoker.result;
     var rows = invoker.result.rows;
     var nRows = rows.length;
     TW.Runtime.showStatusText('testmessage','Number of Rows: ' + nRows); 
};
var fail = function (invoker, xhr) {
    TW.log.error('GetComments failed');
    TW.Runtime.showStatusText('error', 'Error"' + xhr.responseText + '"');
};		
invoker = new ThingworxInvoker({ 
    entityType: 'Things',
    entityName: 'MyThing',
    characteristic: 'Services',
    target: 'MyService',
    apiMethod: 'POST',
});
invoker.invokeService(success, fail);

 

I see the correct number of rows being returned from the message. I have tried using the typical rows[index] and row[index].MyDataShapeProperty and neither method has worked.

 

Has anyone had success with the ThingworxInvoker and returning data and how?

 

Additionally, I am aware I could use bindable properties to bring in data but it is undesirable to require additional setup for widget for something should be a part of the basic functionality.

 

 

ACCEPTED SOLUTION

Accepted Solutions
jburke11
12-Amethyst
(To:slangley)

var success = function (invoker) {
     var dataFromInvoke = invoker.result;
     var rows = invoker.result.rows;
     var row = rows[0];
//Experience crash here and widget does not appear
var message = row.MyDataShapeProperty; TW.log.info("MyDataShapeProperty: " + message); };

I will add that after some testing, I was able to get this to work provided the data shape was something already defined on the platform and not one that I created through the composer. 

For instance if I used an already defined shape such as the 'WikiPageWithComments', 'EntityList', or 'GenericEntry' and do the same type of call but using a non-user defined shape, it works.

 

var success = function (invoker) {
     var dataFromInvoke = invoker.result;
     var rows = invoker.result.rows;
     var row = rows[0];
//Using WikiPageWithComments Datashape (referenced widget code)
var message = row.parentId; TW.log.info("Message: " + message); };

 

 

View solution in original post

7 REPLIES 7
slangley
23-Emerald II
(To:jburke11)

Hi @jburke11.

 

What are you trying to populate when the service runs?  Have you checked the Script or Application log for any errors?  Does the script run successfully when you execute it directly within the platform?  Can you provide the script for the service.

 

Regards.

 

--Sharon

jburke11
12-Amethyst
(To:slangley)

The service runs correctly and returns the expected result. There are no errors in the log either.

 

The service is currently just a call to CurrentSessionInfo and GetCurrentUser (limited for testing purposes).

 

The adding of multiple rows is just an indicator I used identify if I am getting the proper amount of rows returned which I have been successful in.

 

var params = {
    infoTableName : "InfoTable",
    dataShapeName : "MyDataShape"
};
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(table)
var table = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var newEntry = new Object();
newEntry.MyDataShapeProperty= Resources["CurrentSessionInfo"].GetCurrentUser();
table.AddRow(newEntry);
table.AddRow(newEntry);
table.AddRow(newEntry);
var result = table;

The result is returned as an infotable with "MyDataShape" that has a field of "MyDataShapeProperty" which is a string.

slangley
23-Emerald II
(To:jburke11)

Hi @jburke11.

 

 

You indicated in your first post that you had tried "using the typical rows[index] and row[index].MyDataShapeProperty and neither method has worked."   Can you provide your code?

 

Regards.

 

--Sharon

jburke11
12-Amethyst
(To:slangley)

var success = function (invoker) {
     var dataFromInvoke = invoker.result;
     var rows = invoker.result.rows;
     var row = rows[0];
//Experience crash here and widget does not appear
var message = row.MyDataShapeProperty; TW.log.info("MyDataShapeProperty: " + message); };

I will add that after some testing, I was able to get this to work provided the data shape was something already defined on the platform and not one that I created through the composer. 

For instance if I used an already defined shape such as the 'WikiPageWithComments', 'EntityList', or 'GenericEntry' and do the same type of call but using a non-user defined shape, it works.

 

var success = function (invoker) {
     var dataFromInvoke = invoker.result;
     var rows = invoker.result.rows;
     var row = rows[0];
//Using WikiPageWithComments Datashape (referenced widget code)
var message = row.parentId; TW.log.info("Message: " + message); };

 

 

jburke11
12-Amethyst
(To:slangley)

I didn't realize the posts just went in time order and didn't continue down the tree

Hello,

 

Try that:

_.each(invoker.result.rows, function(row) {
  console.log(row.MyDataShapeProperty);
});

Even if it works, it probably makes more sense to get session parameters via TW.Runtime.Session instead of invoking the REST call twice.

 

/ Constantine

slangley
23-Emerald II
(To:Constantine)

Hi @jburke11.

 

If you are satisfied with the solution provided by @Constantine, please mark it as the Accepted Solution for the benefit of others with the same question.  If you still have outstanding questions, please let us know.

 

Regards.

 

--Sharon

Announcements


Top Tags