What is the best way to get data from embedded widget into a catalog item variable?

Michael96
Tera Expert

What is the best way to get data from embedded widget into a catalog item variable? I am very new to Servicenow and feel like this is an easy question, but I haven't been able to find any easily understandable answer for a beginner. Thanks in advance.

1 ACCEPTED SOLUTION

SatheeshKumar
Kilo Sage

There is no direct way to get the values from the widget but you can use the below way to do that.

Create a hidden staging variable  in your catalog item.

In your widget use the below code to get and set value from widget.

$scope.page.g_form.getValue('u_name'); 

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

 where  u_name is the hidden variable you created.

View solution in original post

3 REPLIES 3

phantom7
Giga Expert

 

Hi Michael,

First you need to create your widget. Then create a catalog item variable. Set the type to micro. Go to the default value tab. Go to the widget field and select the widget you created. You should now see the widget when you view the catalog item in Service Portal.

thank you,

let me know if you have any further questions

SatheeshKumar
Kilo Sage

There is no direct way to get the values from the widget but you can use the below way to do that.

Create a hidden staging variable  in your catalog item.

In your widget use the below code to get and set value from widget.

$scope.page.g_form.getValue('u_name'); 

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

 where  u_name is the hidden variable you created.

I was able to do it without having to call g_form.getValue(). Maybe this code snippet will help someone in the future. Thanks for the help. 🙂

function($scope)
{
	var c = this;
	
	
	var catalogItemVariableName = 'test_widget_variables';
	var widgetArray = [];
	
	c.add = function(){
		widgetArray.push($scope.c.data.message);
		replaceCatalogItemVariables(catalogItemVariableName, widgetArray);
		c.data.action = 'addMessage';
		c.server.update().then(function(){
			c.data.action = undefined;
			c.data.message = '';
		})
	}
	}
	
	//only checks if input value is array or string
	function replaceCatalogItemVariables(fieldName, userValue)
	{
		if(Array.isArray(userValue))
		{
			$scope.page.g_form.setValue(fieldName, userValue.join('\n'));
		}
		else
		{
			$scope.page.g_form.setValue(fieldName, userValue);
		}
	}
}