The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to pass array from widget client script to server script after receiving broadcast

davilu
Mega Sage

Our team has a widget that broadcasts a number of data points and arrays stored in an object:

var obj = {
  catSelected: c.selected.cat_active,
  catName: c.selected.hro_category_name,
  catAppArr: c.selected.hro_category_apps,
  catDesc: c.selected.hro_category_desc,
  catSysID: c.selected.hro_category_sysID
}
$rootScope.$broadcast('changeCategories', obj);

On the other widget, we receive the broadcast:

$rootScope.$on('changeCategories', function(event, data) {
  c.server.get({
    action: 'retrieve_data'
  }).then(function() {
    c.data.appArr = data.catAppArr;
  });
})

and would like to send it over to the server side, how do we do this?  So far we have something like the below, but does not show up in the console:

if (input && input.action == 'retrieve_data') {
  data.test = input.appArr;
}

Do we even need c.server.get to pass that array over to the server?  Thanks for your help!

12 REPLIES 12

but don't we need to bring to bring that object array into the server side in order to do that?  that's why we're trying to get data.test to show up, but it's currently still undefined.  

Community Alums
Not applicable

Yes, that is correct, you would do the GlideRecord queries in your server script. 

I wish I could be more help, but I still don't know exactly what you are trying to do.  Here's an example you could use on the server side  but you will need to replace my code with the correct the table, field, etc. for your GlideRecord.  You may even be trying to do something completely different. 

if (input && input.action == 'retrieve_data') {
 data.test = [];
for (var i = 0; i < input.appArr.length; i++) {
    var catApp = input.appArr[i];
    var myGr = new GlideRecord('cat_app_table');
    if (myGr.get('cat_app', catApp)) {
        data.test.push(myGr.name.toString())
    }
}
}

 

Also, in your two screenshots above, you have two different things - data.appArr and r.data.test, so they will be different.  Is this what you are wanting to do there?

$rootScope.$on('changeCategories', function(event, data) {
  c.server.get({
	action: 'retrieve_data',
	appArr: data.catAppArr
  }).then(function(r) {
	console.log("r.data.test: " + r.data.test)
	c.data.appArr = r.data.test;
  });
})

Got sidetracked yesterday with another request, but will try this today.  Thanks for your help @Dustin Watkins - will report back.