Question


Optum Technologies
IN
Last activity: 24 Apr 2025 2:12 EDT
Cannot assign a value of a Data Page from getDataPage JS API to a JS variable
var options = { name: "dataPageName", parameters: [{name: "param1", value: "Page1.prop1", isProperty: true}, {name: "param2", value: 123, isProperty: false}], callback: callbackFunc }; function callbackFunc(resultJSON){ console.log(resultJSON) } pega.api.ui.actions.getDataPage(options);
can anyone please help me understand how to access the resultJSON value. I tried mapping to some global variable declared before option variable and tried to print it in console. but it didn't work.
-
Reply
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!


DPIE
AU
@SATEESH9 you can assign the value resultJSON to other variable in a callback function itself, declaring outside won’t work as this datapge js api asynchronously called and loaded.


Optum Technologies
IN
@Kumar here is the code. It might have some unnecessary code, but basic intention is if i can access the data of "resultJSON" outside of the callback function.
var mainResultJSON = "dummy"; function generateToken() { var newToken = ""; var options = { name: "D_PSPToken", parameters: [ { name: "accessToken", value: "<%= tools.findPage("pyWorkPage").getString("ActionID") %>", isProperty: false } ], callback: function(resultJSON) { newToken = resultJSON; console.log(resultJSON); console.log("resultJSON.access_token : " + resultJSON.access_token); return resultJSON; }
@Kumar here is the code. It might have some unnecessary code, but basic intention is if i can access the data of "resultJSON" outside of the callback function.
var mainResultJSON = "dummy"; function generateToken() { var newToken = ""; var options = { name: "D_PSPToken", parameters: [ { name: "accessToken", value: "<%= tools.findPage("pyWorkPage").getString("ActionID") %>", isProperty: false } ], callback: function(resultJSON) { newToken = resultJSON; console.log(resultJSON); console.log("resultJSON.access_token : " + resultJSON.access_token); return resultJSON; }
mainResultJSON = resultJSON; }; pega.api.ui.actions.getDataPage(options); console.log("Main Result JSON inside : " + mainResultJSON); console.log("options.callback.newToken : " + options.callback.newToken); return mainResultJSON; } document.getElementById('<%=SCaseID%>').refreshPspToken = generateToken()


DPIE
AU
@SATEESH9 thanks mate for sharing the code, there are some modifications required to fix the issue
Follow below steps you should be fine.
- Whitelist your data page by adding it to pyPublicDataPageWhiteList html rule.
- Pass the event in your function generateToken
- Remove the jsp tag to pass the value, if needed dynamic value pass it through your wrapper js function
- In the callback function call another function and pass the value of resultJSON as param. This way you should be able to use the value outside of callback. You can even call other js apis to publish the manipulated data back to the clipboard.
There are other ways as well to use the data page through js call.
Hope this helps.


Optum Technologies
IN
@Kumar thanks for the reply,
- Whitelist your data page by adding it to pyPublicDataPageWhiteList html rule.
- i have tried this option
- Pass the event in your function generateToken
- there is no event that the source application going to send. They will call document.getElementByID('id').xxxx when the token gets expired. and by the time they call this document.getElementByID('id').xxxx, we have to load the fresh token by flushing the DP
- Remove the jsp tag to pass the value, if needed dynamic value pass it through your wrapper js function
- ok sure
- In the callback function call another function and pass the value of resultJSON as param. This way you should be able to use the value outside of callback. You can even call other js apis to publish the manipulated data back to the clipboard.
- ok i will try and see


DPIE
AU
@SATEESH9 Without whitelisting, it won’t work. I’m attaching a working code sample for your reference.
Also, could you please clarify why you want to use the JS API? Is there a specific reason you can’t use the REST APIs to achieve your requirement?


Optum Technologies
IN
@Kumar thanks for sharing the sample working code. The reason going for this JS API is the client application developed a react widget and embedded in the application. And we also have to go with some custom java script coding along with react.