The CreatorCon Call for Content is officially open! Get started here.

How to set custom variable value from widget

avid
Kilo Expert

Hello.

In order to simplify my question I've created a new ui macro variable that contain only a single input field:

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<input type="text" id="test" name="test"></input>

</j:jelly>

In order to view the variable the catalog form I've connected a new widget to the macro variable that contain the same field:

Body HTML template:

<div>

<input type="text" id="test" name="test" />

</div>

And indeed the new variable is shown on the catalog form as input field in the new service portal, but when i submit the catalog with some string inside the input field the variable value stay empty:

find_real_file.png

I tried several methods in order to set the value in the variable but unfortunately i didn't succeed.   my only concern is how to set the value in the widget, not in the macro.

Thank you/

1 ACCEPTED SOLUTION

larstange
Mega Sage

Hi



Values entered in widget variables are NOT saved when you submit the item.


You need to add additional regular variables (eg. a single line text) and then on an onSubmit script (only for ui macros on the old service catalog) copy the value from your widget to the regular variable. You can hide the regular variable using a UI policy.



Making your own widget for SP, you will not be able to access the content with an onSubmit script, as you can't accces the DOM. Instead you build an onChange logic into your widget to copy the value from the input field to your regular variable everytime a change happens.



You can access variables on the catalog item from the widget via:



$scope.page.g_form.setValue('var_name');



Also see how to monitor for changes in catalog variables here: Embedding widgets in Service Catalog - ServicePortal.io - Service Portal, CMS, and Custom Apps


View solution in original post

13 REPLIES 13

larstange
Mega Sage

Hi



Values entered in widget variables are NOT saved when you submit the item.


You need to add additional regular variables (eg. a single line text) and then on an onSubmit script (only for ui macros on the old service catalog) copy the value from your widget to the regular variable. You can hide the regular variable using a UI policy.



Making your own widget for SP, you will not be able to access the content with an onSubmit script, as you can't accces the DOM. Instead you build an onChange logic into your widget to copy the value from the input field to your regular variable everytime a change happens.



You can access variables on the catalog item from the widget via:



$scope.page.g_form.setValue('var_name');



Also see how to monitor for changes in catalog variables here: Embedding widgets in Service Catalog - ServicePortal.io - Service Portal, CMS, and Custom Apps


Great answer.



The command : $scope.page.g_form.setValue('var_name');   that's what i looked for.



Thank you.




Hi Lars,



The above command doesn't work for me. I get this error when i try to set the variable value from widget. "TypeError: Cannot read property 'setValue' of undefined". I am working on Helsinki patch 11. Can you help me?



regards,


Divya Lakshmi.


larstange
Mega Sage

Is $scope declared as a service in the client script function?



function($scope) {


  /* widget controller */


  var c = this;


.


.


.


}