Widget returning [object Object]

Winston
Tera Guru

Hey everyone, 

I'm a little at my wits end here. I've trying to use the OOB Slush bucket widget on a catalog form to have it pull multiple values from a table and then have it fill to a variable (which will be hidden) on the same form.

Everything is working perfect, BUT (there's always a but) the values are passing from the widget as [object object].

find_real_file.png

Here's how i'm filling the selected values from the client script of the widget and filling into the form variable named, test.

$scope.$watch('c.selected.toString()', function(newVal, oldVal) {
	$scope.page.g_form.setValue('test', newVal);
      });

Here is the full Client Script:

// sample use of ng-sortable
function($scope) {
	var c = this;
	c.selected = [];
	
	// ng-sortable clone means cannot reorder
	c.availableAs = {
		itemMoved: function (event) {
			event.source.itemScope.item.used = true;
		},
		clone: true // clone this side
	}

	c.selectedAs = {
		itemMoved: function (event) {
			// moved back to available
			var item = event.source.itemScope.item;
			moveToAvailable(item);
			removeItem(c.data.all, item);
		},
		dragStart: function () {
			c.availableAs.clone = false;
		},
		dragEnd: function () {
			c.availableAs.clone = true;
		}
	}

	// double moves from Available to Selected
	c.onDblClick = function(item) {
		var t = angular.copy(item);
		item.used = true; // original is now used
		c.selected.push(t);
	}

	// double on selected removes and unsets Available used
	c.onSelDblClick = function(item) {
		moveToAvailable(item);
		removeItem(c.selected, item);
	}

	function removeItem(array, item) {
		var n = array.indexOf(item);
		if (n !== -1)
			array.splice(n, 1);		
	}
	
	function moveToAvailable(item) {
		var t = item.sys_id.value;
		angular.forEach(c.data.all, function(target) {
			if (target.sys_id.value == t)
				target.used = false;
		})		
	}
	
		$scope.$watch('c.selected.toString()', function(newVal, oldVal) {
			$scope.page.g_form.setValue('test', newVal);
      });
}

Here is the server script:

(function () {
	// grab names of available dependencies
	data.all = [];
	var fieldnames = "sys_id,label";
	var gr = new GlideRecord('sys_choice');
	gr.orderBy(fieldnames);
	gr.query();
	while (gr.next()) {
		var t = $sp.getRecordElements(gr, fieldnames);
		data.all.push(t);
	}
})();
11 REPLIES 11

Mike Patel
Tera Sage

Can you try changing below

$scope.page.g_form.setValue('test', newVal);

to

$scope.page.g_form.setValue('test', newVal.toString());

I've tried putting the .toString() on the c.selected and the newVal, but both are still giving me the [object Object] when I submit the form. The weird thing is only the .toString() behind the c.selected makes the test field update with the [object Object] on the form.

$scope.$watch('c.selected.toString()', function(newVal, oldVal) {
$scope.page.g_form.setValue('test', newVal);
});

$scope.$watch('c.selected', function(newVal, oldVal) {
$scope.page.g_form.setValue('test', newVal.toString());
});

Look at below and it will help you since you are also trying to do something similar.

https://community.servicenow.com/community?id=community_question&sys_id=01880f72db7523001089e15b8a96...

So I was able to resolve the [object Object] by using both .toString() that you suggested and JSON.stringify().

$scope.$watch('c.selected.toString()', function(newVal, oldVal) {
$scope.page.g_form.setValue('test', JSON.stringify(c.selected));
});

Now it's filling with the correct field info, but including the display_value, character limit, label, field type, and value. 

I only want the display_value to copy over. 

find_real_file.png

I believe this has something to do with the $sp.getRecordElements in the server script.

I tried replacing it with $sp.getRecordDisplayValues(gr, "label"); but then the values in the widget don't show.

Full Server Script:

(function () {
	// grab names of available dependencies
	data.all = [];
	var fieldname = "label";
	var gr = new GlideRecord('sys_choice');
	gr.orderBy("label");
	gr.query();
	while (gr.next()) {
		var t = $sp.getRecordElements(gr, "label");
		data.all.push(t);
	}
})();

 Any tips on how to just pull the display_value for the values?