ServicePortal custom widget(scoped) - client script logs [object Object]

tantony
Mega Guru

I am in the process of learning widget scripting.I want to display a record's 'Group' name from the server script on to widget HTML template on page loading. There is no 'input'  for server script. The server script works fine and has the 'data.group_name' logged correctly. But the client script logs it as '[object Object]'. The preview displays an empty JSON {} on the page instead of Group name.What am I doing wrong?

HTML Template:

<div class="panel panel-default">
<!-- your widget template -->

<div class="panel-body">
<p >
Division of Information Technology - {{ c.data.group_name }}
</p>
</div>
</div>

Client Script:

function() {
/* widget controller */
var c = this;

console.log("c.data.group_name: " + c.data.group_name);  //Logs as c.data.group_name: [object Object]
}

Server script:

(function() {
/* populate the 'data' object */
var potEventGR = new GlideRecord('x_15865_potluck_group_potluck_event');
var got = potEventGR.get('5c8a3fae4f421300bae4e321a310c716');
console.log('got' + ": " + got);// Logs as expected 'true'
data.group_name = potEventGR.event_group.name;
console.log('Server data.group_name' + ": " + data.group_name);// Logs as expected

})();

Any help is appreciated.....

5 REPLIES 5

tantony
Mega Guru

I could get this working by using Widget Server side API $sp.getRecordDisplayValues(Object data, GlideRecord from, String names). I am still not sure why the logs in server script logged the event_group name correctly misleading me to think the client script had issues.

Now my server script is modified as below:

(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */

var potEventGR = new GlideRecord('x_15865_potluck_group_potluck_event');
var got = potEventGR.get('5c8a3fae4f421300bae4e321a310c716');
console.log('got' + ": " + got);
var gr = {};
$sp.getRecordDisplayValues(gr, potEventGR, 'event_group');
data.group_name = gr.event_group;
console.log('Server data.group_name' + ": " + data.group_name);

})();