Post

2 followers Follow
0
Avatar

Help with Custom Panel, assigning data to a variable

I am trying to get some data to show up on a custom panel for related Items.

 

Description:

     We have, at any time, a request that can be related to other requests - usually no more than 5.  On those requests we want to see who the owners are of the other related requests.

 

To do this I have created a custom object - this object holds all the related requests.

 

It is setup like this.

request_a

     -> related_requests (custom object)

            * request_b

            * request_c

            * ...

Right now this works great, and on that related_requests object it has a nice panel/table that shows all related requests:

 

The issue is, at the request level - I want to show that information, but it lives on the related item custom object.

 

What I have tried:

I have spend some time picking apart custom panels and looking at examples and trying things.

 

Currently I have a custom panel with this in the Data (JSON Format) = field:

```

{
"target_request_id": {{$ExternalID},
"other_requests": {{JsonObjects("$R_related_requests","Name")}
}

```

 

And in the Script = field I have this:

```

var this_request_id = API.Context.getData().target_request_id;
var this_other_request = API.Context.getData().other_requests;


queryStringRelated = "Select ExternalID, (select AssignedTo.Name, C_Region.Name, C_FunctionalGroup, ExternalID from R_Requests1) from C_related_request where ExternalID = '"+this_other_request[0]['id'].substring(19, 49)+"'" //+ the_related_request_object;

API.Objects.query(queryStringRelated, function(qResults) {
if (qResults != null && qResults.length > 0) {
console.log(qResults[0].R_Requests1.entities)
}
});

```

 

So far this is printing in the console what I need:

 

So it is giving me a list of key/value pairs.

 

Issues:

If anyone has pointers, these are the issues I am running into,

  • How to I assign that to a variable, I seem to only be able to print it to the console, I have tried assigning to variables (caring for scope) but I am always getting undefined.
  • Where in the panel would I retrieve the data stored in this variable to show it in a table on this request's panel?

Any ideas/help would be appreciated.

Jared Thompson Answered

Please sign in to leave a comment.

2 comments

0
Avatar

Hello Jared,

I think this is because the API.Objects.query() is asynchronous. I.e. JS does not wait for it to complete and continues execution. As a result, if you assign its output to a variable, you get undefined, because at the point in time when it was evaluated it was actually undefined just yet (waiting for API response).

Just to confirm this, you could put the assignment in a setTimeout() function let's say with a 10 second delay. I think then it would not be undefined anymore. However, this is not a good solution.

Possible solutions:

1) Continue all of your code within API.Objects.query(). This is a simple way to do it, but not a good solution when building complex programs.

2) Use JS Promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

3) Use JS async/await: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Option 3 is by far my favorite.

Please let me know if this helps.

Roland

Roland Pumputis 0 votes
Comment actions Permalink
0
Avatar

Thanks Roland - I will read through that.

I am definitely more comfortable with my python wrapper I wrote for the api - but I wanted to try some custom panels so I am going the js route now.

 

Thanks again for the tips.

Jared Thompson 0 votes
Comment actions Permalink